• SpringBoot整合redis+mysql


    SpringBoot整合Redis

    测试连接

    添加相关依赖

    
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.2.6.RELEASEversion>
            <relativePath/> 
        parent>
        <groupId>com.singroupId>
        <artifactId>demo-springboot-redisartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>demo-springboot-redisname>
        <description>Demo project for Spring Bootdescription>
        <properties>
            <java.version>1.8java.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
            dependency>
    
    
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-redisartifactId>
            dependency>
    
            
            <dependency>
                <groupId>org.apache.commonsgroupId>
                <artifactId>commons-pool2artifactId>
            dependency>
    
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
            dependency>
    
            
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>2.2.2version>
            dependency>
            
            
            <dependency>
                <groupId>tk.mybatisgroupId>
                <artifactId>mapper-spring-boot-starterartifactId>
                <version>2.1.5version>
            dependency>
    
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <scope>testscope>
            dependency>
            
            <dependency>
                <groupId>org.yamlgroupId>
                <artifactId>snakeyamlartifactId>
                <version>1.23version>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    package com.sin;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * @author sin
     * @date 2022/11/25
     * @apiNote
     */
    @SpringBootApplication
    public class SpringRedisMySQLRun {
        public static void main(String[] args) {
            SpringApplication.run(SpringRedisMySQLRun.class,args);
            System.out.println("===============启动成功================");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    配置

    ## 配置端口
    server:
      port: 80
    
    spring:
      ## 配置数据库数据源
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/sindemo
        username: root
        password: 123456
      redis:
        # 地址
        host: 192.168.11.128
        # 端口
        port: 6379
        # 密码
        password: 123456
        # 超时时间 5000毫秒
        timeout: 5000
        jedis:	
    		连接池
          pool:
            # 连接池最小空闲连接
            min-idle: 0
            # 连接池的最大空闲连接
            max-idle: 8
            # 连接池最大阻塞等待时间(使用负数表示没有限制)
            max-wait: -1
            # 连接池最大连接数(使用负数表示没有限制)
            max-active: 8
                
    
    # mybatis配置
    mybatis:
      # 获取配置文件的地址
      mapper-locations: classpath:/mybatis/mapper/*.xml
      # 获取实体类的地址
      type-aliases-package: com.sin.pojo
      configuration:
        # 开启驼峰命名
        map-underscore-to-camel-case: true
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    测试项目

    package com.sin.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author sin
     * @date 2022/11/25
     * @apiNote
     */
    @RestController
    public class TestController {
    
        @RequestMapping("/test")
        public String testControler(){
            System.out.println("测试成功");
            return "测试成功";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    在这里插入图片描述

    开启Redis服务

    ## 启动redis
    [root@My-Server redis]# cd /usr/local/redis/bin
    [root@My-Server bin]# ./redis-server /usr/local/redis/etc/redis.conf
    ## 进入redis
    [root@My-Server bin]# cd /usr/local/redis/bin
    [root@My-Server bin]# ./redis-cli
    ## 密码登录
    127.0.0.1:6379> auth 123456
    OK
    127.0.0.1:6379>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    连接Redis测试

    在这里插入图片描述

    RedisTemplate是Spring Data Redis提供的一个强大的Redis操作模板,用于在Spring应用程序中与Redis数据库进行交互。对redis底层开发包(Jedis,JRedis,RJC)进行了一个封装,定义了五种数据结构的操作方法,异常处理,序列化,发布订阅

    • opsForValue():操作字符串。
    • opsForList():操作列表。
    • opsForHash():操作哈希。
    • opsForSet():操作集合。
    • opsForZSet():操作有序集合。
    添加数据
    package com.sin.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.web.bind.annotation.*;
    
    /**
     * @author sin
     * @date 2022/11/25
     * @apiNote
     */
    @RestController // 表示该类是一个RESTful风格的控制器,将该类下的所有的方法的返回值直接以JSON或XML的形式写入到HTTP请求体。
    public class RedisController {
    
        //注入RedisTemplate
        @Autowired
        private RedisTemplate redisTemplate;
    
        //添加数据
        // @PathVariable 将URL中的key和Value参数映射到方法的参数中
        @PostMapping("/addData/key={key}/value={value}")//动态地址
        public Object addData(@PathVariable("key") String key,
                              @PathVariable("value") String value){
            redisTemplate.opsForValue().set(key,value);
            System.out.println("数据添加成功"+redisTemplate);
            return "addData Success"; // 将字符串"addData Success"作为HTTP响应的主体返回给客户端
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述

    获取数据
    //根据key获取value
    @GetMapping("/getData/key={key}")
    // @PathVariable 将URL中的key和Value参数映射到方法的参数中
    public Object getData(@PathVariable("key") String key){
      // 根据key获取value
       Object value =  redisTemplate.opsForValue().get(key);
        System.out.println("数据为:"+value);
        return value;// 将查询到的数据作为HTTP响应的主体返回给客户端
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述在这里插入图片描述

    Redis序列化

    看到结果后是看不懂的,这个就跟它的序列化有关了。就是根据RedisTemplate将数据写入到redis中的时候会经过一个序列化操作,序列化操作后就变成了以上数据格式,这个并不影响程序,因为set写入时会有序列化操作,get获取数据时也就有着这么一个序列话操作,但是对于我们来说时很不友好的

    在这里插入图片描述

    在源码中我们能看到定义了各种数据类型的序列化的一个成员变量

    if (defaultSerializer == null) { //当defaultSerializer为null
    
        // 确保在没有明确指定默认序列化器时,会创建一个JdkSerializationRedisSerializer的实例作为默认的序列化器。
        // 默认情况下,它会使用Java对象的标准序列化方式来将对象序列化为字节流,以便存储到Redis中,或者将从Redis中读取的字节流反序列化为Java对象。
        
       defaultSerializer = new JdkSerializationRedisSerializer(	
       		// 如果存在(classLoader)类加载器,就是用classLoader来初始化JdkSerializationRedisSerializer,
       		// 否则,使用当前类的类加载器(this.getClass().getClassLoader())来初始化。
             classLoader != null ? classLoader : this.getClass().getClassLoader());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    // 通过默认的序列化来处理所有的键和值,以确保序列化和反序列化的一致性
    // 如果keySerializer为空
    if (keySerializer == null) {
    	//将keySerializer设置为defaultSerializer
    	keySerializer = defaultSerializer;
    	// 并将defaultUsed标记为true。
    	defaultUsed = true;
    }
    if (valueSerializer == null) {
    	valueSerializer = defaultSerializer;
    	defaultUsed = true;
    }
    if (hashKeySerializer == null) {
    	hashKeySerializer = defaultSerializer;
    	defaultUsed = true;
    }
    if (hashValueSerializer == null) {
    	hashValueSerializer = defaultSerializer;
    	defaultUsed = true;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    通过这种方式,即使没有明确指定特定的序列化器,也可以保证所有的键和值都会使用相同的序列化器进行处理,从而避免因序列化器不一致而引发的问题。

    我们没有定义,它就用的jdk的序列化将数据传输

    package com.sin.conf;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    
    /**
     * @author sin
     * @date 2022/11/25
     * @apiNote
     */
    @Configuration //表示该类为配置类
    public class RedisConfig {
    
        @Bean
        public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
            //实例化RedisTemplate
            RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
            //设置连接工厂
            redisTemplate.setConnectionFactory(connectionFactory);
    
            //指定k,v的序列化方式(json)
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    
            //默认序列化为json格式
            redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
    
            return redisTemplate;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    Json序列化

    在这里插入图片描述

    package com.sin.conf;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    /**
     * @author sin
     * @date 2022/11/25
     * @apiNote
     */
    @Configuration //表示该类为配置类
    public class RedisConfig {
    
        @Bean
        public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
            //实例化RedisTemplate
            RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
            //设置连接工厂
            redisTemplate.setConnectionFactory(connectionFactory);
    
            //指定k,v的序列化方式(json)
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    
    //        //默认序列化为json格式
    //        redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
            //key设置为String类型
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            //value设置为Json格式
            redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    
            return redisTemplate;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    key为String格式,value为Json格式
    在这里插入图片描述

    Springboot整合redis使用

    RedisConfig

    package com.sin.demo.config;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.*;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    /**
     * redis配置类
     *
     */
    @EnableCaching
    @Configuration//标记该类为配置类,
    public class RedisConfig {
        /**
         * 对读写的数据进行序列化操作
         * @param factory 连接工厂,
         * @return
         */
        @Bean
        public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
    
            //实例化redisTemplate
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            //设置连接工厂
            redisTemplate.setConnectionFactory(factory);
            Jackson2JsonRedisSerializer<Object> redisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
    
            //ObjectMapper以json进行读写操作
            ObjectMapper mapper = new ObjectMapper();
            //指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
            mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            redisSerializer.setObjectMapper(mapper);
    
            //设置默认的序列化
            //redisTemplate.setDefaultSerializer(new StringRedisSerializer());
    
            //实际开发中key为字符串序列化,value为json序列化
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(redisSerializer);
    
            //hash序列化
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(redisSerializer);
            redisTemplate.afterPropertiesSet();
    
            return redisTemplate;
        }
    
        /**
         * 对redis操作String数据类型
         * @param redisTemplate
         * @return
         */
        @Bean
        public ValueOperations<String,Object> valueOperations(RedisTemplate<String,Object> redisTemplate){
            return redisTemplate.opsForValue();
        }
    
        /**
         * Redis操作List数据类型
         * @param redisTemplate
         * @return
         */
        @Bean
        public ListOperations<String,Object> listOperations(RedisTemplate<String,Object> redisTemplate){
            return redisTemplate.opsForList();
        }
    
        /**
         * Redis操作Hash数据类型
         * @param redisTemplate
         * @return
         */
        @Bean
        public HashOperations<String,String,Object> hashOperations(RedisTemplate<String,Object> redisTemplate){
            return redisTemplate.opsForHash();
        }
    
        /**
         * Redis操作set数据类型
         * @param redisTemplate
         * @return
         */
        @Bean
        public SetOperations<String,Object> setOperations(RedisTemplate<String,Object> redisTemplate){
            return redisTemplate.opsForSet();
        }
    
        /**
         * Redis操作zSet数据类型
         * @param redisTemplate
         * @return
         */
        public ZSetOperations<String,Object> zSetOperations(RedisTemplate<String,Object> redisTemplate){
            return redisTemplate.opsForZSet();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    RedisTemplate封装

    package com.sin.demo.util;
    
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Component;
    import org.springframework.util.CollectionUtils;
    import sun.security.ec.point.ProjectivePoint;
    
    import java.util.Collection;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;
    
    /**
     * redisTemplate封装
     */
    @Component
    public class RedisUtil {
        @Autowired
        private RedisTemplate<String,Object> redisTemplate;
    
        public RedisUtil(RedisTemplate<String, Object> redisTemplate) {
            this.redisTemplate = redisTemplate;
        }
    
        /**
         * 指定缓存失效时间
         * @param key
         * @param time
         * @return
         */
        public boolean expire(String key,long time){
            try {
                if (time > 0) {
                    redisTemplate.expire(key, time, TimeUnit.SECONDS);
                }
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 指定key过期时间
         * @param key
         * @return
         */
        public long getExpire(String key){
            return redisTemplate.getExpire(key,TimeUnit.SECONDS);
        }
    
        /**
         * 判断key是否存在
         * @param key
         * @return
         */
        public boolean hasKey(String key){
            try {
                return redisTemplate.hasKey(key);
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 删除缓存
         * @param key
         */
        @SuppressWarnings("unchecked")
        public void del(String ... key){
            if(key!=null&&key.length>0){
                if(key.length==1){
                    redisTemplate.delete(key[0]);
                }else {
                    redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));
                }
            }
        }
    
    
        //=====================================================Redis的String数据类型操作=========================================
    
        /**
         * 普通缓存的获取
         * @param key
         * @return
         */
        public Object get(String key){
            return key==null?null:redisTemplate.opsForValue().get(key);
        }
    
        /**
         * 普通缓存的存放
         * @param key
         * @param value
         * @return
         */
        public boolean set(String key,Object value){
            try{
                redisTemplate.opsForValue().set(key,value);
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 普通缓存放入并设置时间
         * @param key
         * @param value
         * @param time
         * @return
         */
        public boolean set(String key,Object value,long time){
            try{
                if(time>0){
                    redisTemplate.opsForValue().set(key,value,time,TimeUnit.SECONDS);
                }else {
                    set(key,value);
                }
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 递增
         * @param key
         * @param delta
         * @return
         */
        public long incr(String key,long delta){
            if (delta<0){
                throw new RuntimeException("递增的条件必须大于0");
            }
            return redisTemplate.opsForValue().increment(key,delta);
        }
    
    
        /**
         * 递减
         * @param key
         * @param delta
         * @return
         */
        public long decr(String key,long delta){
            if (delta<0){
                throw new RuntimeException("递减的条件不许大于0");
    
            }
            return redisTemplate.opsForValue().decrement(key,-delta);
        }
    
        //===============================================redis的Hash数据类型操作====================================================
    
        /**
         * Hashget
         * @param key 不能为null
         * @param itme 不能为null
         * @return
         */
        public Object hget(String key,String itme ){
            return redisTemplate.opsForHash().get(key,itme);
        }
    
        /**
         * 获取hashkey对应的所有键值
         * @param key
         * @return
         */
        public Map<Object,Object> hmget(String key){
            return redisTemplate.opsForHash().entries(key);
        }
    
        /**
         * HashSet
         */
        public boolean hmset (String key,Map<String,Object> map){
            try {
                redisTemplate.opsForHash().putAll(key,map);
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * Hashset 设置时间
         * @param key
         * @param map
         * @param time
         * @return
         */
        public boolean hmset(String key,Map<String,Object>map,long time){
            try{
                redisTemplate.opsForHash().putAll(key,map);
                if(time>0){
                    expire(key,time);
                }
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
    
            }
    
        }
    
        /**
         * 向一张hash表中放入数据,如果不存在就将其创建
         * @param key
         * @param item 项
         * @param value
         * @return
         */
        public boolean hset(String key,String item,String value){
            try{
                redisTemplate.opsForHash().put(key,item,value);
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
    
        }
    
        /**
         * 向一张hash表中放入数据,如果不存在就将其创建,对时间的操作
         * @param key
         * @param item
         * @param value
         * @param time
         * @return
         */
        public boolean hset(String key,String item,String value,long time){
            try{
                redisTemplate.opsForHash().put(key,item,value);
                if (time>0){
                    expire(key,time);
                }
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
    
        }
    
        /**
         * 删除hash表中的值
         * @param key
         * @param item
         */
        public void hdel(String key,Object ... item){
            redisTemplate.opsForHash().delete(key,item);
        }
    
        /**
         * 判断Hash表中是否有该项的值
         * @param key
         * @param item
         * @return
         */
        public boolean hHasKey(String key,String item){
            return redisTemplate.opsForHash().hasKey(key,item);
        }
    
        /**
         * hash递增,如果不存在,就会创建一个,并把新增后的值返回
         * @param key
         * @param item
         * @param by  要增加几(大于0)
         * @return
         */
        public double hincr(String key,String item,double by){
            return redisTemplate.opsForHash().increment(key,item,by);
        }
    
        /**
         * 递减操作
         * @param key
         * @param item
         * @param by 要减少几(小于0)
         * @return
         */
        public double hdecr(String key,String item,double by){
            return redisTemplate.opsForHash().increment(key,item,by);
        }
    
        //===============================================redis的set数据类型操作====================================================
    
        /**
         * 根据key获取set中的所有值
         * @param key
         * @return
         */
        public Set<Object> sGet(String key){
            try {
                return redisTemplate.opsForSet().members(key);
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * 根据value从一个set中查询,是否存在
         * @param key
         * @param value
         * @return
         */
        public boolean sHasKey(String key,Object value){
            try {
                return redisTemplate.opsForSet().isMember(key,value);
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 将数据放入set缓存中
         * @param key
         * @param values
         * @return 成功个数
         */
        public long sSet(String key,Object ... values){
            try{
                return redisTemplate.opsForSet().add(key,values);
            }catch (Exception e){
                e.printStackTrace();
                return 0;
            }
        }
    
        /**
         * 将set数据放入缓存
         * @param key
         * @param time
         * @param values
         * @return 成功个数
         */
        public long sSetAndTime(String key,long time,Object ... values){
            try{
                Long count = redisTemplate.opsForSet().add(key, values);
                if(time>0){
                    expire(key,time);
                }
                return count;
            }catch (Exception e){
                e.printStackTrace();
                return 0;
            }
        }
    
        /**
         * 获取set缓存的长度
         * @param key
         * @return
         */
        public long sGetSetSize(String key){
            try {
                return redisTemplate.opsForSet().size(key);
            }catch (Exception e){
                e.printStackTrace();
                return 0;
            }
        }
    
        /**
         * 移除值为value的
         * @param key
         * @param values
         * @return
         */
        public long setRemove(String key,Object ... values){
            try {
                Long count = redisTemplate.opsForSet().remove(key, values);
                return count;
            }catch (Exception e){
                e.printStackTrace();
                return 0;
            }
        }
    
        //===============================================redis的list数据类型操作====================================================
    
        /**
         * 获取list缓存的内容
         * @param key
         * @param start 开始
         * @param end 结束(0到-1表示所有值)
         * @return
         */
        public List<Object> lGet(String key,long start,long end){
            try {
                return redisTemplate.opsForList().range(key,start,end);
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * 获取list缓存的长度
         * @param key
         * @return
         */
        public long lGetListSize(String key){
            try {
                return redisTemplate.opsForList().size(key);
            }catch (Exception e){
                e.printStackTrace();
                return 0;
            }
        }
    
    
        /**
         * 通过索引,获取list中的值
         * @param key
         * @param index 索引,index>=0时,0表示开头,1表示第二个元素;index《=0时,-1表示末尾,-2表示第二个元素,
         * @return
         */
        public Object lGetIndex(String key,long index){
            try{
                return redisTemplate.opsForList().index(key,index);
            }catch (Exception e ){
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * 将list放入缓存
         * @param key
         * @param value
         * @return
         */
        public boolean lSet(String key,Object value){
            try {
                redisTemplate.opsForList().rightPush(key,value);
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
    
    
        /**
         * 将list放入缓存中,对时间操作
         * @param key
         * @param value
         * @param time
         * @return
         */
        public boolean lSet(String key,Object value,long time){
            try {
                redisTemplate.opsForList().rightPush(key,value);
                if (time>0){
                    expire(key,time);
                }
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 将list放入缓存
         * @param key
         * @param values
         * @return
         */
        public boolean lSetList(String key,List<Object> values){
            try {
                redisTemplate.opsForList().rightPushAll(key,values);
                return true;
    
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 将list放入缓存中,对时间操作
         * @param key
         * @param values
         * @param time
         * @return
         */
        public boolean lSetList(String key,List<Object> values ,long time){
            try {
                redisTemplate.opsForList().rightPushAll(key,values);
                if(time>=0){
                    expire(key,time);
                }
                return true;
    
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 根据索引修改list中的数据
         * @param key
         * @param index
         * @param value
         * @return
         */
        public boolean lUpdateIndex(String key,long index,Object value){
            try {
                redisTemplate.opsForList().set(key,index,value);
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 移除n个值
         * @param key
         * @param count
         * @param value
         * @return
         */
        public long lRemove(String key,long count,Object value){
            try{
                return redisTemplate.opsForList().remove(key,count,value);
            }catch (Exception e){
                e.printStackTrace();
                return 0;
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 502
    • 503
    • 504
    • 505
    • 506
    • 507
    • 508
    • 509
    • 510
    • 511
    • 512
    • 513
    • 514
    • 515
    • 516
    • 517
    • 518
    • 519
    • 520
    • 521
    • 522
    • 523
    • 524
    • 525
    • 526
    • 527
    • 528
    • 529
    • 530
    • 531
    • 532
    • 533
    • 534
    • 535
    • 536
    • 537
    • 538
    • 539
    • 540
    • 541
    • 542
    • 543
    • 544
    • 545
    • 546
    • 547
    • 548
    • 549
    • 550

    编写实体类

    public class Student {
        private Integer id;
        private String name ;
        private Integer age;
        private String dept;//班级
        set,get方法
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    RedisController类

    package com.sin.controller;
    
    import com.sin.pojo.Student;
    import com.sin.util.RedisUtil;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.web.bind.annotation.*;
    
    import javax.annotation.Resource;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author sin
     * @date 2022/11/25
     * @apiNote
     */
    @RestController
    public class RedisController {
    
        //注入RedisTemplate
        @Autowired
        private RedisTemplate redisTemplate;
    
    
        @Autowired
        private RedisUtil redisUtil;
    
        //添加数据
        @PostMapping("/addData/key={key}/value={value}")//动态地址
        public Object addData(@PathVariable("key") String key,
                              @PathVariable("value") String value){
            redisTemplate.opsForValue().set(key,value);
            System.out.println("数据添加成功"+redisTemplate);
            return "addData Success";
        }
    
        //根据key获取value
        @GetMapping("/getData/key={key}")
        public Object getData(@PathVariable("key") String key){
           Object value =  redisTemplate.opsForValue().get(key);
            System.out.println("数据为:"+value);
            return redisTemplate.opsForValue().get(key);
        }
    
    
    
        /**
         * 添加list对象数据到redis中
         * @return
         */
        @RequestMapping(value = "/setList")
        public boolean setList(){
            List<Object> stuList = new ArrayList<>();
            Student student = new Student();
            student.setId(1);
            student.setAge(23);
            student.setDept("saas");
            student.setName("张三");
    
            Student student1 = new Student();
            student1.setId(2);
            student1.setAge(22);
            student1.setDept("微服务");
            student1.setName("李四");
    
            Student student2 = new Student();
            student2.setId(3);
            student2.setAge(23);
            student2.setDept("saas");
            student2.setName("王五");
    
            Student student3 = new Student();
            student3.setId(4);
            student3.setAge(24);
            student3.setDept("微服务");
            student3.setName("赵六");
    
            stuList.add(student);
            stuList.add(student1);
            stuList.add(student2);
            stuList.add(student3);
            return redisUtil.lSetList("student",stuList);
        }
    
        /**
         * 获取全部数据
         * @return
         */
        @RequestMapping(value = "getList")
        public  Object getList(){
            return redisUtil.lGet("student",0,-1);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    在这里插入图片描述
    在这里插入图片描述

    SpringBoot整合Redis+Mybatis

    ​ SpringBoot+SpringMVC+redis+mysql来实现项目中的crud操作,redis做缓存数据库,针对频繁需要查询或者解决单点问题都会把数据库到redis来分配服务器压力。

    实体类

    create table demo_user(
        id int(10) auto_increment primary key not null ,
        user_name varchar(40) not null ,
        password varchar(10) not null ,
        sex int(10) not null );
        
        alter table demo_user charset  = utf8;
    
    alter table demo_user convert to character set utf8;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    public class DemoUser implements Serializable {
    
    
      private long id;
      private String userName;
      private String password;
      private long sex;
      
        // setter getter方法
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    dao层

    package com.sin.mapper;
    
    import com.sin.pojo.DemoUser;
    import org.apache.ibatis.annotations.*;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    /**
     * dao层
     */
    @Repository
    @Mapper
    public interface DemoUserMapper {
        //用户列表
        @Select("select * from demo_user")
        @Results({
                @Result(property = "userName",column = "user_name"),
                @Result(property = "password",column = "password")
        })
        List<DemoUser> queryAll();
    
        //根据id获取demo_user
        @Select("select * from demo_user where id = #{id}")
        @Results({
                @Result(property = "userName",column = "user_name"),
                @Result(property = "password",column = "password")
        })
        DemoUser findUserById(int id);
        
        @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
        @Insert("insert into demo_user(id,user_name,password,sex)values" +
                "(#{id},#{userName},#{password},#{sex})")
        int add(DemoUser user);
    
        @Select("select * from demo_user where user_name = #{userName} ")
        @Results({
                @Result(property = "userName",column = "user_name"),
                @Result(property = "password",column = "password")
        })
        DemoUser findUserByName(String userName);
    
        //根据id修改demoUser
        @Update("update demo_user set user_name=#{userName},password=#{password}," +
                "sex=#{sex} where id=#{id}")
        int updateUser(DemoUser demoUser);
    
        //删除用户
        @Delete("delete from demo_user where id = #{id}")
        int deleteUserById(int id);
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    config

    package com.sin.config;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.boot.autoconfigure.cache.CacheProperties;
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.context.annotation.Bean;
    import org.springframework.data.redis.cache.RedisCacheConfiguration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.cache.RedisCacheWriter;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.connection.RedisInvalidSubscriptionException;
    import org.springframework.data.redis.core.*;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    import java.time.Duration;
    
    /**
     * redis 配置类
     * 配置redisTemplate序列化
     */
    public class RedisConfig extends CachingConfigurerSupport {
        /**
         *  选择redis作为默认缓存工具
         *
         */
        @Bean
        public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory){
            //设置缓存时间为一个小时
            RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                    .entryTtl(Duration.ofHours(1));
            return RedisCacheManager.builder(RedisCacheWriter.lockingRedisCacheWriter(redisConnectionFactory))
                    .cacheDefaults(redisCacheConfiguration).build();
        }
    
        /**
         * 配置redisTemplate相关配置
         *
         */
        @Bean
        public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
            RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
            //配置连接工厂
            redisTemplate.setConnectionFactory(factory);
    
            //使用Jackson2JsonRedisSerializer来序列化何反序列化redis的value值
            Jackson2JsonRedisSerializer jsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    
            ObjectMapper objectMapper = new ObjectMapper();
            //指定要序列化的域,field,get和set,以及修饰符范围,ANY都是有private和public
            objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            //指定序列化输入的类型,类必须时非final修饰的,
            objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            jsonRedisSerializer.setObjectMapper(objectMapper);
    
            //值采用json序列化
            redisTemplate.setValueSerializer(jsonRedisSerializer);
            //使用StringRedisSerrializer来序列化和反序列化redis的key值
            redisTemplate.setKeySerializer(new StringRedisSerializer());
    
            //设置hash key 和value序列化模式
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(jsonRedisSerializer);
            redisTemplate.afterPropertiesSet();
    
            return redisTemplate;
        }
    
    
        /*
         * 对hash类型的数据操作
         */
        @Bean
        public HashOperations<String,String,Object> hashOperations(RedisTemplate<String,Object> redisTemplate){
            return redisTemplate.opsForHash();
        }
    
        /**
         * 对redis 字符串String类型数据操作
         */
        @Bean
        public ValueOperations<String,Object> valueOperations(RedisTemplate<String,Object> redisTemplate){
            return redisTemplate.opsForValue();
        }
        /**
         * 对redis 链表list类型数据操作
         */
        @Bean
        public ListOperations<String,Object>listOperations(RedisTemplate<String,Object> redisTemplate){
            return redisTemplate.opsForList();
        }
        /**
         * 对redis 无序集合set类型数据操作
         */
        @Bean
        public SetOperations<String,Object> setOperations(RedisTemplate<String,Object> redisTemplate){
            return redisTemplate.opsForSet();
        }
    
        /**
         * 对redis 有序结合zset类型数据操作
         */
        @Bean
        public ZSetOperations<String,Object> zSetOperations(RedisTemplate<String,Object> redisTemplate){
            return redisTemplate.opsForZSet();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111

    service

    public interface DemoUserService {
    
        //查询全部
        List<DemoUser> queryAll();
        //根据id查询
        DemoUser findUserById(int id);
        //根据id进行修改
        int updateUser(DemoUser demoUser);
        //根据id进行删除
        int deleteUserById(int id);
        
        //添加数据
        int addUser(DemoUser user);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    impl

    package com.sin.service.impl;
    
    import com.sin.mapper.DemoUserMapper;
    import com.sin.pojo.DemoUser;
    import com.sin.service.DemoUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    @Service
    public class DemoUserServiceImpl implements DemoUserService {
    
        @Autowired
        private DemoUserMapper demoUserMapper;
        @Autowired
        private RedisTemplate redisTemplate;
    
    
        //查询全部操作
        @Override
        public List<DemoUser> queryAll() {
            return demoUserMapper.queryAll();
        }
    
        /**
         * 根据id进行查询
         * 获取用户策略,先从缓存中获取用户,没有则读取mysql数据,再将读取的数据写入缓存中
         */
        @Override
        public DemoUser findUserById(int id) {
            String key = "user_" + id;
            ValueOperations<String,DemoUser> operations = redisTemplate.opsForValue();
            //判断redis中是否有键为key的缓存
            boolean haskey = redisTemplate.hasKey(key);
            if (haskey){
                DemoUser user = operations.get(key);
                System.out.println("从缓存中获取的数据:"+user.getUserName());
                System.out.println("----------------------------------");
                return user;
            }else {
                DemoUser user = demoUserMapper.findUserById(id);
                System.out.println("查询数据库中的数据"+ user.getUserName());
                System.out.println("--------------------写入数据------------------");
                //写入数据
                operations.set(key,user,5, TimeUnit.HOURS);
                return user;
            }
        }
    
        /**
         * 更新用户
         * 先更新数据库,成功之后,删除原来的缓存,再更新缓存
         * @param demoUser
         * @return
         */
        @Override
        public int updateUser(DemoUser demoUser) {
            ValueOperations<String,DemoUser> operations = redisTemplate.opsForValue();
            int result = demoUserMapper.updateUser(demoUser);
            if (result!=0){
                String key = "user_"+demoUser.getId();
                boolean haskey = redisTemplate.hasKey(key);
                if (haskey){
                    redisTemplate.delete(key);
                    System.out.println("删除缓存中的key----"+key);
                }
                //将更新后的数据加入缓存中
                DemoUser demoUserNew = demoUserMapper.findUserById((int) demoUser.getId());
                if (demoUserNew != null){
                    operations.set(key,demoUserNew,3,TimeUnit.HOURS);
                }
            }
            return result;
        }
    
        /**
         * 根据id删除
         * 删除数据表中的数据,然后删除缓存
         * @param id
         * @return
         */
        @Override
        public int deleteUserById(int id) {
            int result = demoUserMapper.deleteUserById(id);
            String key = "user_" + id;
            if (result != 0){
                boolean haskey = redisTemplate.hasKey(key);
                if (haskey){
                    redisTemplate.delete(key);
                    System.out.println("删除了缓存中的key"+ key);
                }
            }
            return result;
        }
         @Override
        public int addUser(DemoUser user) {
            DemoUser user1 = demoUserMapper.findUserByName(user.getUserName());
            int result;
            if (user1 != null){
                result = 0;
                return result;
            }else{
                ValueOperations<String,Object> operations = redisTemplate.opsForValue();
                result = demoUserMapper.add(user);
                if (result != 0 ){
                    String key  = "user_" +user.getId();
                    boolean haskey = redisTemplate.hasKey(key);
                    if(haskey){
                        redisTemplate.delete(key);
                    }
                    if (user != null){
                        operations.set(key,user,3,TimeUnit.HOURS);
                    }
                }
                return result;
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123

    controller

    package com.sin.controller;
    
    import com.sin.pojo.DemoUser;
    import com.sin.service.DemoUserService;
    import org.apache.catalina.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.sql.Timestamp;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @RestController
    public class DemoUserController {
    
        @Autowired
        private DemoUserService demoUserService;
    
        @RequestMapping("/queryAll")
        public List<DemoUser> queryAll(){
            List<DemoUser> list = demoUserService.queryAll();
            return list;
        }
        @RequestMapping("/addUser")
        public String addUser(){
            DemoUser user = new DemoUser();
            user.setUserName("李四");
            user.setPassword("12112");
            user.setSex(1);
            user.setBirthday( new Date());
            int resutl= demoUserService.addUser(user);
            if (resutl != 0){
                return "add DemoUser success";
    
            }
            return "add demouser fail";
        }
    
    
        @RequestMapping("/findUserById")
        public Map<String,Object> findUserById(@RequestParam int id){
            DemoUser user = demoUserService.findUserById(id);
            Map<String,Object> result = new HashMap<>();
            result.put("id",user.getId());
            result.put("username",user.getUserName());
            result.put("pwd",user.getPassword());
            result.put("sex",user.getSex());
            result.put("birthday",user.getBirthday());
            return result;
        }
    
        @RequestMapping("/updateUser")
        public String updateUser(){
            DemoUser user = new DemoUser();
            user.setId(1);
            user.setUserName("张三");
            user.setPassword("123456");
            user.setSex(18);
            user.setBirthday((Timestamp) new Date());
    
            int result = demoUserService.updateUser(user);
    
            if (result != 0){
                return "update user success";
            }
            return "fall";
        }
    
    
        @RequestMapping("/deleteUserById")
        public String deleteUserById(@RequestParam int id){
            int result = demoUserService.deleteUserById(id);
            if (result != 0){
                return "delete Success";
            }
            return "delete fall";
        }
    
    
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    在这里插入图片描述
    加入QQ技术交流群(432972658)

    在这里插入图片描述

  • 相关阅读:
    Linux shell 重定向输入和输出
    C语言 if分支结构
    LQ0019 数的拆分【素数】
    国产瑞芯微RK3399 硬件设计之 HDMI2.0 的注意事项
    数据结构-----排序的概念、常见排序的实现以及排序算法的特点、非比较排序、排序相关例题
    vue之小知识罗列
    【NowCoder】左程云-设计getMin功能的栈
    Unity入门04——Unity重要组件和API(1)
    执行sql报错only_full_group_by的解决方法
    node学习基础四之express
  • 原文地址:https://blog.csdn.net/qq_44715376/article/details/128040246