zoukankan      html  css  js  c++  java
  • spring-boot集成mybatis,用redis做缓存

    网上有很多例子了,执行源码起码有3个,都是各种各样的小问题。

    现在做了个小demo,实现spring-boot 用redis做缓存的实例,简单记录下思路,分享下源码。

    缓存的实现,分担了数据库的压力,在CRUD中

    C:需要同时更新redis和mysql中的数据。

    R:看redis中 有无数据,有,就从缓存中取,没有就从数据库中取,同时更新缓存。

    U:删除redis中的数据,并update数据库。

    D:删除redis和mysql的数据。

    1、基本步骤

      (1)redis安装  http://download.csdn.net/download/xsx531588644/9565831

      (2)配置pom.xml

      (3)添加工具类

      (4)添加项目启动扫描类

      (5)修改controller

    2、实现

      (1)安装redis

                     有一个redis图形管理工具很好用RedisManager,可以一起装上。

          引入操作redis的客户端包jedis,见下方的总结。

      (2)配置pom.xml

        添加代码

      

    <!-- 热部署 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
            
            <!-- redis cache related.....start -->  
            <dependency>  
                <groupId>org.springframework.data</groupId>  
                <artifactId>spring-data-redis</artifactId>  
            </dependency>  
            <dependency>  
                <groupId>redis.clients</groupId>  
                <artifactId>jedis</artifactId>  
            </dependency>

    完整pom.xml代码

    <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.sun</groupId>
      <artifactId>spring-boot-test</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
          <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
        
        <dependencies>
              <!-- web -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            
            <!-- mybatis -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.2.0</version>
            </dependency>
            
            
            <!-- mysql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            
            
            <!-- 热部署 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
            
            <!-- redis cache related.....start -->  
            <dependency>  
                <groupId>org.springframework.data</groupId>  
                <artifactId>spring-data-redis</artifactId>  
            </dependency>  
            <dependency>  
                <groupId>redis.clients</groupId>  
                <artifactId>jedis</artifactId>  
            </dependency>
        </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    View Code

        (3)添加redis工具类

        

    package com.sun.beans;
    
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.data.redis.core.BoundSetOperations;
    import org.springframework.data.redis.core.HashOperations;
    import org.springframework.data.redis.core.ListOperations;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.stereotype.Service;
    
    @Service
    public class RedisCacheUtil<T>
    {
    
        
        @Autowired @Qualifier("jedisTemplate")
        public RedisTemplate redisTemplate;
        
    
        
        /**
         * 缓存基本的对象,Integer、String、实体类等
         * @param key    缓存的键值
         * @param value    缓存的值
         * @return        缓存的对象
         */
        public <T> ValueOperations<String,T> setCacheObject(String key,T value)
        {
            
            ValueOperations<String,T> operation = redisTemplate.opsForValue(); 
            operation.set(key,value);
            return operation;
        }
        
        /**
         * 获得缓存的基本对象。
         * @param key        缓存键值
         * @param operation
         * @return            缓存键值对应的数据
         */
        public <T> T getCacheObject(String key/*,ValueOperations<String,T> operation*/)
        {
            ValueOperations<String,T> operation = redisTemplate.opsForValue(); 
            return operation.get(key);
        }
        
        /**
         * 缓存List数据
         * @param key        缓存的键值
         * @param dataList    待缓存的List数据
         * @return            缓存的对象
         */
        public <T> ListOperations<String, T> setCacheList(String key,List<T> dataList)
        {
            ListOperations listOperation = redisTemplate.opsForList();
            if(null != dataList)
            {
                int size = dataList.size();
                for(int i = 0; i < size ; i ++)
                {
                    
                    listOperation.rightPush(key,dataList.get(i));
                }
            }
            
            return listOperation;
        }
        
        /**
         * 获得缓存的list对象
         * @param key    缓存的键值
         * @return        缓存键值对应的数据
         */
        @SuppressWarnings({ "unchecked", "hiding" })
        public <T> List<T> getCacheList(String key)
        {
            List<T> dataList = new ArrayList<T>();
            ListOperations<String,T> listOperation = redisTemplate.opsForList();
            Long size = listOperation.size(key);
            
            for(int i = 0 ; i < size ; i ++)
            {
                dataList.add((T) listOperation.leftPop(key));
            }
            
            return dataList;
        }
        
        /**
         * 缓存Set
         * @param key        缓存键值
         * @param dataSet    缓存的数据
         * @return            缓存数据的对象
         */
        @SuppressWarnings({ "unchecked", "hiding" })
        public <T> BoundSetOperations<String,T> setCacheSet(String key,Set<T> dataSet)
        {
            BoundSetOperations<String,T> setOperation = redisTemplate.boundSetOps(key);    
            /*T[] t = (T[]) dataSet.toArray();
                 setOperation.add(t);*/
            
            
            Iterator<T> it = dataSet.iterator();
            while(it.hasNext())
            {
                setOperation.add(it.next());
            }
            
            return setOperation;
        }
        
        /**
         * 获得缓存的set
         * @param key
         * @param operation
         * @return
         */
        public Set<T> getCacheSet(String key/*,BoundSetOperations<String,T> operation*/)
        {
            Set<T> dataSet = new HashSet<T>();
            BoundSetOperations<String,T> operation = redisTemplate.boundSetOps(key);    
            
            Long size = operation.size();
            for(int i = 0 ; i < size ; i++)
            {
                dataSet.add(operation.pop());
            }
            return dataSet;
        }
        
        /**
         * 缓存Map
         * @param key
         * @param dataMap
         * @return
         */
        public <T> HashOperations<String,String,T> setCacheMap(String key,Map<String,T> dataMap)
        {
            
            HashOperations hashOperations = redisTemplate.opsForHash();
            if(null != dataMap)
            {
                
                for (Map.Entry<String, T> entry : dataMap.entrySet()) {  
                      
                    /*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());  */
                    hashOperations.put(key,entry.getKey(),entry.getValue());
                } 
                
            }
            
            return hashOperations;
        }
        
        /**
         * 获得缓存的Map
         * @param key
         * @param hashOperation
         * @return
         */
        public <T> Map<String,T> getCacheMap(String key/*,HashOperations<String,String,T> hashOperation*/)
        {
            Map<String, T> map = redisTemplate.opsForHash().entries(key);
            /*Map<String, T> map = hashOperation.entries(key);*/
            return map;
        }
        
        
        
        
        
        
        
        /**
         * 缓存Map
         * @param key
         * @param dataMap
         * @return
         */
        public <T> HashOperations<String,Integer,T> setCacheIntegerMap(String key,Map<Integer,T> dataMap)
        {
            HashOperations hashOperations = redisTemplate.opsForHash();
            if(null != dataMap)
            {
                
                for (Map.Entry<Integer, T> entry : dataMap.entrySet()) {  
                      
                    /*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());  */
                    hashOperations.put(key,entry.getKey(),entry.getValue());
                } 
                
            }
            
            return hashOperations;
        }
        
        /**
         * 获得缓存的Map
         * @param key
         * @param hashOperation
         * @return
         */
        public <T> Map<Integer,T> getCacheIntegerMap(String key/*,HashOperations<String,String,T> hashOperation*/)
        {
            Map<Integer, T> map = redisTemplate.opsForHash().entries(key);
            /*Map<String, T> map = hashOperation.entries(key);*/
            return map;
        }
    }
    View Code

        (4)添加redis监听,项目启动时自动缓存数据

    package com.sun.beans;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.annotation.Resource;
    
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.event.ContextRefreshedEvent;
    import org.springframework.stereotype.Service;
    
    import com.sun.dao.UserMapper;
    import com.sun.model.User;
    
    /*
     * 监听器,用于项目启动的时候初始化信息
     */
    @Service
    public class StartAddCacheListener implements ApplicationListener<ContextRefreshedEvent>
    {
        @Resource
        private RedisCacheUtil<Object> redisCache;
        
    
        @Resource
        private UserMapper userMapper;
        
        @Override
        public void onApplicationEvent(ContextRefreshedEvent  event) 
        { 
            //spring 启动的时候缓存user等信息
            if(event.getApplicationContext().getParent() == null)
            {
                System.out.println("
    
    
    _________
    
    缓存数据 
    
     ________
    
    
    
    ");
                List<User> userList = userMapper.selectAll();
                
                Map<Integer,User> userMap = new HashMap<Integer,User>();
                
                int cityListSize = userList.size();
                
                for(int i = 0 ; i < cityListSize ; i ++ )
                {
                    userMap.put(userList.get(i).getId(), userList.get(i));
                }
                
                
                redisCache.setCacheIntegerMap("userMap", userMap);
            }
        }
        
    }
    View Code

        

        (5)修改TestController添加如下代码:

        

    @Autowired
        private RedisCacheUtil<User> redisCache;
    
        @RequestMapping("/testGetCache")
        public String testGetCache()
        {
    
            System.out.println("------------user");
            Map<Integer,User> userMap = redisCache.getCacheIntegerMap("userMap");
            for(int key : userMap.keySet())
            {
                System.out.println("key = " + key + ",value=" + userMap.get(key));
            }
            return userMap.toString();
        }

    注意:这里要序列化User实体类,也就是public class User  implements Serializable{

    3、总结

      (1)redis是用客户端jedis操作的。所以jedis包我都是手动添加,工程根节点building path-》

      

    这样添加的。如果你有更好的方式,请留言。版本号是2.9.0。

    而且做这个redis的时候我发现版本号的问题十分讨厌,在网上下了redis的windows安装版本,但是安装了却没有jar包,

    所以又在网上下了redis的3.2.100版本,但是却在工程里不知道版本号怎么写,后来使用的例子代码多了,发现maven管理的jar包中最高有1.8的,

    就一直这么写了,有大神可以分析下为什么吗?

    (2)redis的Windows版是自启动的,这样我改了redis-server.conf密码后,在项目中仍然设置了密码,提示auth错误。

    注释掉项目中的密码设置就可以了。

    (3)在applicationContext.xml中设置redis的配置文件,查资料说2种方式都可以,但是发现一种是报错的,只有

    <context:property-placeholder location="classpath*:redis.properties" ignore-unresolvable="true" /> 这样可行,不知道为什么?有大神解答么?

    (4)添加了几个配置文件applicationContext.xml和redis.properties和spring-redis.xml。

    需要注意在启动类中的扫描代码:

    @ImportResource(locations={"classpath:applicationContext.xml"})

    最后给一下源码https://github.com/sunfengjiajia/spring-boot-test

    欢迎大家加我qq:309620263探讨技术问题。
  • 相关阅读:
    mac上python3安装HTMLTestRunner
    双目深度估计传统算法流程及OpenCV的编译注意事项
    深度学习梯度反向传播出现Nan值的原因归类
    1394. Find Lucky Integer in an Array
    1399. Count Largest Group
    1200. Minimum Absolute Difference
    999. Available Captures for Rook
    509. Fibonacci Number
    1160. Find Words That Can Be Formed by Characters
    1122. Relative Sort Array
  • 原文地址:https://www.cnblogs.com/PPBoy/p/7126681.html
Copyright © 2011-2022 走看看