赞
踩
SpringBoot 内默认引用了jedis版本。所以我们直接引入jedis 依赖 无需在配置 jedis的版本号了。
一、引入jar包,pom文件
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>3.2.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-pool2</artifactId>
- </dependency>
二、配置文件application,配置redis
- #设置服务启动的端口号默认8080
- server.port=8089
- #设置请求发生错误时显示的页面
- server.error.path=/error
-
- #redis
- spring.redis.database=0
- spring.redis.host=
- spring.redis.password=
- spring.redis.port=6379
- # 数据库连接超时时间,2.0 中该参数的类型为Duration,这里在配置的时候需要指明单位
- spring.redis.timeout=5000
-
- # 连接池配置,2.0中直接使用jedis或者lettuce配置连接池
- # 最大活跃连接数,负数为不限制
- spring.redis.jedis.pool.max-active=8
- # 等待可用连接的最大时间,负数为不限制
- spring.redis.jedis.pool.max-wait=-1
- # 最大空闲连接数
- spring.redis.jedis.pool.max-idle=8
- # 最小空闲连接数
- spring.redis.jedis.pool.min-idle=0
三、JedisConfig配置类
- @Configuration
- @Slf4j
- public class JedisConfig {
- //private Logger logger = LoggerFactory.getLogger(JedisConfig.class);
-
- @Value("${spring.redis.host}")
- private String host;
- @Value("${spring.redis.port}")
- private int port;
- @Value("${spring.redis.password}")
- private String password;
- @Value("${spring.redis.timeout}")
- private int timeout;
- @Value("${spring.redis.jedis.pool.max-active}")
- private int maxActive;
- @Value("${spring.redis.jedis.pool.max-idle}")
- private int maxIdle;
- @Value("${spring.redis.jedis.pool.min-idle}")
- private int minIdle;
- @Bean
- public JedisPool jedisPool(){
- JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
- jedisPoolConfig.setMaxIdle(maxIdle);
- jedisPoolConfig.setMinIdle(minIdle);
- jedisPoolConfig.setMaxTotal(maxActive);
- // 是否启用pool的jmx管理功能, 默认true
- jedisPoolConfig.setJmxEnabled(true);
-
- JedisPool jedisPool=new JedisPool(jedisPoolConfig,host,port,timeout,password);
- log.info("JedisPool连接成功:"+host+":"+port);
- return jedisPool;
- }
- }
测试JedisConfig配置成功
- @SpringBootTest
- public class JedisConfigTest {
- @Autowired
- private JedisPool jedisPool;
- @Test
- public void conextLoads() {
- System.out.println(jedisPool);
- Jedis jedis = jedisPool.getResource();
- jedis.set("name1","zjw");
- String name = jedis.get("name1");
- System.out.println(name);
- jedis.close();
- }
- }
四、工具类,JedisUtils,可以封装一些redis的操作,这里面只有获取资源和释放连接
- @Component
- public class JedisUtils {
- @Autowired
- private JedisPool jedisPool;
-
- /*获取Jedis资源*/
- public Jedis getJedis() {
- return jedisPool.getResource();
- }
- /*释放Jedis连接*/
- public void close(Jedis jedis) {
- if(jedis!=null) {
- jedis.close();
- }
- }
- }
五、具体实例JedisServiceImpl.java
- @Service
- @Log
- public class JedisServiceImpl {
- @Autowired
- private JedisUtils jedisUtils;
-
- /*
- 测试String
- */
- public String getString(String key) {
- Jedis jedis = jedisUtils.getJedis();
- String val = null;
- if (!jedis.exists(key)) {
- val = "黎明的光和影";
- log.info(key + "在MYSQL数据库中进行查询的结果是:" + val);
- jedis.set(key, val);
- log.info(key + "存入Redis中的值是:" + val);
- } else {
- val = jedis.get(key);
- log.info(key + "是在Redis中查询的数据。值是:" + val);
- }
- jedisUtils.close(jedis); //释放资源
- return val;
- }
-
- /**
- * 测试 jedis 操作hash类型
- * * 根据用户ID查询用户信息
- * * 先判断Redis中是否存在,
- * * 如果不存在,数据库中查询。并存到Redis中
- * * 如果存在,直接查询Redis 并返回
- */
- public User selectBy(String id) {
- String key = "user:"+id; //根据规则生成相同规范的key
- User user = new User();
- Jedis jedis = jedisUtils.getJedis();
- if (!jedis.exists(key)) { //数据库中查询,并进行存
- user.setId(id);
- user.setName("RedisHash");
- user.setAge(22);
- log.info("数据库中查询的用户信息是:" + user);
- Map<String, String> map = new HashMap();
- map.put("id", user.getId());
- map.put("name", user.getName());
- map.put("age",user.getAge().toString());
- jedis.hset(key, map);
- log.info(key + "成功存入Redis:" + user);
- } else {
- Map<String, String> map = jedis.hgetAll(key);
- user.setId(map.get("id"));
- user.setName(map.get("name"));
- System.out.println("map中的age"+map.get("age"));
- user.setAge(Integer.valueOf(map.get("age")));
- log.info(key + "Redis中查询出来的是:" + map);
- }
- Map<String, String> userVal = jedis.hgetAll(key);
- System.out.println(userVal+"mapUserVal");
- jedisUtils.close(jedis);
- return user;
- }
-
- /*
- redis数据库存储
- */
- public String select() {
- Jedis jedis = jedisUtils.getJedis();
- return "zhangjingwen";
- }
- }
测试:
- @SpringBootTest
- public class JedisUtilsTest {
- @Autowired
- private JedisServiceImpl jedisService;
-
- @Test
- public void t1() {
- String val = jedisService.getString("name1");
- String val2= jedisService.getString("age");
- String val3= jedisService.getString("name");
- System.out.println(val+ val2+val3);
- }
- @Test
- public void test() {
- User user = jedisService.selectBy("2");
- System.out.println(user);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。