• SpringBoot整合Redis


    SpringBoot整合Redis

    1、项目目录结构

    2、引入依赖

     <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-parent</artifactId>
                <version>2.0.6.RELEASE</version>
        </parent>
        <dependencies>
            <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>
            <!--mybatis起步依赖-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.1.1</version>
            </dependency>
            <!--mysql依赖-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!--aop-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
            <!--整合redis-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
        </dependencies>

    3、application.properties

    spring.datasource.password=root
    spring.datasource.username=root
    spring.datasource.url=jdbc:mysql://localhost:3306/study
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    mybatis.mapper-locations=classpath:mapper/*.xml
    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    spring.redis.jedis.pool.max-active=8
    spring.redis.jedis.pool.max-idle=8
    spring.redis.jedis.pool.max-wait=-1
    spring.redis.jedis.pool.min-idle=0

    4、Redis配置类

    @Configuration
    public class RedisAutoConfiguration {
    
        /**
         * 注入 RedisConnectionFactory
         */
        @Autowired
        RedisConnectionFactory redisConnectionFactory;
    
        /**
         * 实例化 RedisTemplate 对象
         *
         * @return
         */
        @Bean
        public RedisTemplate<String, Object> functionDomainRedisTemplate() {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
            return redisTemplate;
        }
    
        /**
         * 设置数据存入 redis 的序列化方式
         *
         * @param redisTemplate
         * @param factory
         */
        private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer(Object.class));
            redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class));
            redisTemplate.setConnectionFactory(factory);
        }
    
        /**
         * 实例化 HashOperations 对象,可以使用 Hash 类型操作
         *
         * @param redisTemplate
         * @return
         */
        @Bean
        public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForHash();
        }
    
        /**
         * 实例化 ValueOperations 对象,可以使用 String 操作
         *
         * @param redisTemplate
         * @return
         */
        @Bean
        public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForValue();
        }
    
        /**
         * 实例化 ListOperations 对象,可以使用 List 操作
         *
         * @param redisTemplate
         * @return
         */
        @Bean
        public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForList();
        }
    
        /**
         * 实例化 SetOperations 对象,可以使用 Set 操作
         *
         * @param redisTemplate
         * @return
         */
        @Bean
        public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForSet();
        }
    
        /**
         * 实例化 ZSetOperations 对象,可以使用 ZSet 操作
         *
         * @param redisTemplate
         * @return
         */
        @Bean
        public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForZSet();
        }
    
    
    }

    5、mapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.yehui.mapper.TbUserMappers">
        <select id="findAll" resultType="com.yehui.entity.TbUser">
          select  * from tb_user
        </select>
    </mapper>

    6、mapper接口

    @Mapper
    public interface TbUserMappers {
        public List<TbUser> findAll();
    }

    7、实体类

    public class TbUser implements Serializable {
        private Integer id;
        private String username;
        private String age;

    8、接口

    public interface UserService {
    
        public List<TbUser> findAll();
    }

    9、实现类

    @Service
    public class UserServiceImpl implements UserService {
        @Autowired
        private TbUserMappers tbUserMappers;
        @Autowired
        private RedisTemplate redisTemplate;
        @Override
        public List<TbUser> findAll() {
            List<TbUser> list = (List<TbUser>) redisTemplate.opsForValue().get("tbUser");
            if (list==null){
                System.out.println("从数据中取出");
                list = tbUserMappers.findAll();
                redisTemplate.opsForValue().set("tbUser",list);
            }else{
                System.out.println("从缓存中取出");
            }
            return list;
        }
    }

    10、controller类

    @RestController
    public class UserController {
    
        @Autowired
        private UserService userService;
        @RequestMapping("/findAll")
        public List<TbUser> findAll(){
            return userService.findAll();
        }
    }

    数据存入redis后的截图

  • 相关阅读:
    1059 C语言竞赛
    1058 选择题
    1057 数零壹
    1056 组合数的和
    1055 集体照
    Mysql--分库分表
    Mysql--改表结构
    Mysql--开始阶段
    Mysql--常用语句
    Mysql--grant授权
  • 原文地址:https://www.cnblogs.com/cxyyh/p/10633374.html
Copyright © 2020-2023  润新知