91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring boot集成Redis(1)—進行增加,更新,查詢,批量刪除等操作

發布時間:2020-06-26 14:53:06 來源:網絡 閱讀:4764 作者:墨營 欄目:軟件技術

前言:最近工作中使用到了redis緩存,故分享一點自己總結的東西,這篇文章使用的是StringRedisTemplate進行學習,這里值的說的是,(1)StringRedisTemplate在進行批量刪除操作時我們需對template進行序列化,(2)更新操作與添加操作一樣,接下來上代碼:
1.建立Spring boot項目,引入Redis依賴(pom.xml如下):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>redis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>redis</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.編寫spring boot配置文件,這里我配置的內容簡單,如需要其他的配置可以進官網去查

#Redis 
spring.redis.host=主機地址
spring.redis.password=admin
spring.redis.port=6379

server.port=8081

3.接下里我們開始寫測試
(1)建立實體類:
User:

package com.test.redis.entity;

public class User {
    private Integer id;
    private String name;
    private String password;

    public User() {
        super();
    }

    public User(Integer id, String name, String password) {
        super();
        this.id = id;
        this.name = name;
        this.password = password;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
    }

}

(2)service層,主要對redis的各種操作方法進行定義
RedisService:

package com.test.redis.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.stereotype.Service;

@Service
public class RedisService {
    @Resource
    private StringRedisTemplate template;

    /**
     * 存儲數據或修改數據
     * 
     * @param modelMap
     * @param mapName
     */
    public void setKey(String mapName, Map<String, Object> modelMap) {
        HashOperations<String, Object, Object> hps = template.opsForHash();
        hps.putAll(mapName, modelMap);
    }

    /**
     * 獲取數據Map
     * 
     * @param mapName
     * @return
     */
    public Map<Object, Object> getMapValue(String mapName) {
        HashOperations<String, Object, Object> hps = this.template.opsForHash();
        return hps.entries(mapName);

    }

    /**
     * 獲取數據value
     * 
     * @param mapName
     * @param hashKey
     * @return
     */
    public Object getValue(String mapName, String hashKey) {
        HashOperations<String, Object, Object> hps = this.template.opsForHash();
        return hps.get(mapName, hashKey);

    }

    /**
     * 批量刪除緩存數據
     * 
     * @param keys
     */
    public void deleteData(List<String> keys) {
        // 執行批量刪除操作時先序列化template
        template.setKeySerializer(new JdkSerializationRedisSerializer());
        template.delete(keys);
    }
}

(3)controller層代碼,演示操作(添加與獲取值):

package com.test.redis.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.redis.entity.User;
import com.test.redis.service.RedisService;

@Controller
public class UserController {

    private static final String mapName="mapName";
    @Autowired
    private RedisService redisService;

    @GetMapping( "/add.do")
    @ResponseBody
    public Map<Object, Object> addUser(HttpServletRequest request){
        Map<String, Object> modelMap=new HashMap<String,Object>();
        User user=new User();
        user.setName("hehename");
        user.setPassword("hehePassword");
        //存放hash值
        modelMap.put("name", user.getName());
        modelMap.put("password", user.getPassword());
        redisService.setKey(mapName, modelMap);
        //獲取map集合
        Map<Object, Object> modelMap1= redisService.getMapValue(mapName);
        Object value= redisService.getValue(mapName, "name");
        System.out.println(" value : "+value);
        modelMap1.put("從緩存中根據key取到的value", value);
        return modelMap1;
    }

}

前臺展示結果:
Spring boot集成Redis(1)—進行增加,更新,查詢,批量刪除等操作

(4)刪除以及獲取值操作:

@GetMapping( "/delete.do")
    @ResponseBody
    public Map<Object, Object> deleteUser(HttpServletRequest request){
        //獲取即將刪除的key值,這里我們做的批量刪除
        List<String> keys=new ArrayList<>();
        keys.add("heheanme");
        //開始執行刪除操作
        redisService.deleteData(keys);
        //獲取map集合
        Map<Object, Object> modelMap1= redisService.getMapValue(mapName);
        Object value= redisService.getValue(mapName, "name");
        System.out.println(" value : "+value);
        modelMap1.put("從緩存中根據key取到的value", value);
        return modelMap1;
    }

前臺顯示結果:
Spring boot集成Redis(1)—進行增加,更新,查詢,批量刪除等操作

由此可見,操作成功

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

屯昌县| 临颍县| 沈阳市| 隆安县| 忻城县| 铜陵市| 龙井市| 松溪县| 屏东县| 昌黎县| 重庆市| 方城县| 巧家县| 南昌市| 贺州市| 观塘区| 黄冈市| 定西市| 卢湾区| 吉安市| 剑阁县| 金秀| 土默特右旗| 开化县| 四川省| 镇安县| 博乐市| 南郑县| 泸水县| 白玉县| 孝昌县| 永新县| 大洼县| 康平县| 岐山县| 宣恩县| 循化| 承德市| 广汉市| 青田县| 安丘市|