• springboot使用redis的配置application.yml连接池以及存取


    第一步:添加pom文件依赖:

    <!-- redis -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>

    第二步:配置application文件

    jedis :  
      pool :  
        host : 127.0.0.1  
        port : 6379  
        config :  
          maxTotal: 100  
          maxIdle: 10  
          maxWaitMillis : 100000 

    第三步:读取配置:

    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.beans.factory.annotation.Qualifier;  
    import org.springframework.beans.factory.annotation.Value;  
    import org.springframework.context.annotation.Bean;  
    import org.springframework.context.annotation.Configuration;  
      
    import redis.clients.jedis.JedisPool;  
    import redis.clients.jedis.JedisPoolConfig;  
      
    @Configuration  
    public class RedisConfiguration {  
          
        @Bean(name= "jedis.pool")  
        @Autowired  
        public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,   
                    @Value("${jedis.pool.host}")String host,   
                    @Value("${jedis.pool.port}")int port) {  
            return new JedisPool(config, host, port);  
        }  
          
        @Bean(name= "jedis.pool.config")  
        public JedisPoolConfig jedisPoolConfig (@Value("${jedis.pool.config.maxTotal}")int maxTotal,  
                                    @Value("${jedis.pool.config.maxIdle}")int maxIdle,  
                                    @Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis) {  
            JedisPoolConfig config = new JedisPoolConfig();  
            config.setMaxTotal(maxTotal);  
            config.setMaxIdle(maxIdle);  
            config.setMaxWaitMillis(maxWaitMillis);  
            return config;  
        }  
          
    }

    第四步:调用工具类demo

    import java.util.HashSet;
    import java.util.Set;
    
    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.stereotype.Component;  
      
    import redis.clients.jedis.Jedis;  
    import redis.clients.jedis.JedisPool;  
      
    /** 
     *  
     * @author Zhangyf
     * @param <T>
     * 
     */  
    @Component  
    public class RedisClient<T> {  
      
        @Autowired  
        private JedisPool jedisPool;  
          
        public void set(String key, String value) throws Exception {  
            Jedis jedis = null;  
            try {  
                jedis = jedisPool.getResource();  
                jedis.set(key, value);  
            } finally {  
                //返还到连接池  
                jedis.close();  
            }  
        }  
          
        public String get(String key) throws Exception  {  
      
            Jedis jedis = null;  
            try {  
                jedis = jedisPool.getResource();  
                return jedis.get(key);  
            } finally {  
                //返还到连接池  
                jedis.close();  
            }  
        }  
          
        public void setobj(String key, T value) throws Exception {  
            Jedis jedis = null;  
            try {  
                Set<T> set = new HashSet<T>();
                set.add(value);
                jedis = jedisPool.getResource();  
                jedis.sadd(key, String.valueOf(set));
            } finally {  
                //返还到连接池  
                jedis.close();  
            }  
        }
    }

    第五步:存取demo:

    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.log4j.Logger;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.alibaba.fastjson.JSON;
    
    import yxm.zyf.love.domian.RoomDO;
    import yxm.zyf.love.mapper.RoomMapper;
    import yxm.zyf.love.redistool.RedisClient;
    import yxm.zyf.love.utils.LoggerUtil;
    import yxm.zyf.love.utils.StringUtil;
    import yxm.zyf.love.utils.URLencord;
    import yxm.zyf.love.vo.payVo;
    
    @RestController
    public class hospitalpay {
        private final static Logger logger = Logger.getLogger(hospitalpay.class);//给类初始化日志对象
    
        @Autowired
        private RoomMapper roomMapper;
    
        @Autowired
        private RedisClient redisClinet;
    
        @ResponseBody
        @RequestMapping(value = "room")
        public RoomDO room() {
            RoomDO roomdo = null;
            try {
                roomdo = JSON.parseObject(redisClinet.get("room"), RoomDO.class);
                if (roomdo == null) {
                    roomdo = roomMapper.selectByPrimaryKey(1);
                    redisClinet.set("room", JSON.toJSONString(roomdo));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            LoggerUtil.info(logger, "出参roomdo:{0}", roomdo);//打印info级别的日志
            System.out.println(roomdo);
            return roomdo;
        }
    }
  • 相关阅读:
    MOSS 2010:安装和使用Office Web Apps
    MOSS 2010:Visual Studio 2010开发体验(29)——工作流开发最佳实践(三)
    VS 2010 : 如何开发和部署Outlook 2010插件(Addin)
    MOSS 2010:Visual Studio 2010开发体验(33)——工作流开发最佳实践(五):全局可重用工作流
    《实践与思考》一书的概述和随笔连载说明
    MOSS 2010:Visual Studio 2010开发体验(21)——使用Business Connectivity Service(BCS)集成业务系统
    用于 Web 应用程序项目部署的 Web.config 转换语法 【转载】
    《实践与思考》系列连载(2)—— 第一部分 我们走在.NET的实践征途上 序言
    MOSS 2010:Visual Studio 2010开发体验(16)——客户端对象模型
    “人在旅途”之随想以及旅游指南(travel.msra.cn)简介
  • 原文地址:https://www.cnblogs.com/zyf-yxm/p/9231129.html
Copyright © 2020-2023  润新知