springboot操作redis存取(jedis)
1.pom.xml
<dependency> <groupId>redis.clientsgroupId> <artifactId>jedisartifactId> dependency>
2.application.properties
spring.redis.host=192.168.2.73
spring.redis.port=6379
//密码默认null
spring.redis.password=
spring.redis.timeout=6000ms
//控制连接池最多可以分配多少个jedis实例
spring.redis.lettuce.pool.max-active=8
//连接池最大阻塞时间
spring.redis.lettuce.pool.max-wait=15000
//连接池最大空闲连接数
spring.redis.lettuce.pool.max-idle=8
//连接池最小空闲连接数,小于这个数会自动创建连接
spring.redis.lettuce.pool.min-idle=0
spring.redis.lettuce.database=0
3.配置redis连接
package com.example.demo.util; /** * @Title: * @ProjectName * @Description: TODO * @author: TongSiYu * @date 2021-8-12 15:43 */ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @Configuration //标注@Configuration的类,相当于一个xml配置文件 public class JedisPoolUtil { //redis配置属性读取 @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.lettuce.database}") private int database; @Value("${spring.redis.lettuce.pool.max-active}") private int maxActive; @Value("${spring.redis.lettuce.pool.max-idle}") private int maxIdle; @Value("${spring.redis.lettuce.pool.min-idle}") private int minIdle; @Value("${spring.redis.lettuce.pool.max-wait}") private long maxWaitMillis; @Autowired private JedisPoolConfig jedisPoolConfig; @Autowired private JedisPool jedisPool; @Bean //标注@Bean后表明返回对象会被当做一个Bean注册到Spring IoC容器 public JedisPoolConfig createJedisPoolConfig() { JedisPoolConfig jedisPoolConfig = null; try { //创建连接池配置类 jedisPoolConfig = new JedisPoolConfig(); //控制连接池最多可以分配多少个jedis实例 jedisPoolConfig.setMaxTotal(maxActive); //连接池最大空闲连接数 jedisPoolConfig.setMaxIdle(maxIdle); //连接池最小空闲连接数,小于这个数会自动创建连接 jedisPoolConfig.setMinIdle(minIdle); //连接池最大阻塞时间 jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); } catch (Exception e) { e.printStackTrace(); } return jedisPoolConfig; } @Bean public JedisPool createJedisPool() { JedisPool jedisPool = null; try { //创建jedisPool连接池 jedisPool = new JedisPool(jedisPoolConfig, host, port, database); } catch (Exception e) { e.printStackTrace(); } return jedisPool; } @Bean public Jedis getJedis() { //获取jedis连接对象 return jedisPool.getResource(); } }
4.封装Jedis工具类
package com.example.demo.util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.util.SafeEncoder; import java.util.Set; /** * @Title: * @ProjectName * @Description: TODO * @author: TongSiYu * @date 2021-8-12 15:46 */ @Component //标注@Component,表示将该类注册到Spring IoC容器 public class JedisUtil { @Autowired private Jedis jedis; //操作key的方法 (用于判断某个key是否存在,需不需要清空key里的数据),这是对key的操作 /** * 判断某个Key是否存在 * * @param key * @return boolean */ public boolean exists(String key){ boolean exists = false; try{ //调用jedis的exists()方法,判断某个Key是否存在于Redis服务器中 exists = jedis.exists(key); }catch (Exception e){ e.printStackTrace(); }finally { //关闭jedis连接,避免资源占用 jedis.close(); } return exists; } /** * 查找所有给定模式的key * * @param pattern key表达式:*表示多个 ?表示一个 (粒如:shop*表示所有以shop开头的key) * @return */ public Setkeys(String pattern){ Set set = null; try{ //调用jedis的keys()方法,获取匹配的key,并保存到Set集合中 set = jedis.keys(pattern); }catch (Exception e){ e.printStackTrace(); }finally { //关闭jedis连接,避免资源占用 jedis.close(); } return set; } /** * 删除key对应的记录,可以是多个记录 * * @param keys * @return 删除的记录数 */ public long del(String... keys){ long count = 0; try{ //调用jedis的del()方法将相关的Keys删除,并返回删除的记录数 count = jedis.del(keys); }catch (Exception e){ e.printStackTrace(); }finally { //关闭jedis连接,避免资源占用 jedis.close(); } return count; } /** * 清除所有的key * @return 状态码 */ public String flushAll(){ String state = null; try{ //调用jedis的flushAll()方法,清空所有的key state = jedis.flushAll(); }catch (Exception e){ e.printStackTrace(); }finally { //关闭jedis连接,避免资源占用 jedis.close(); } return state; } /** * 修改key名,如果新key已存在,则旧key的值会覆盖新key的值 * * @param oldKey * @param newKey * @return 状态码 */ public String rename(String oldKey,String newKey){ String state = null; try{ state = jedis.rename(SafeEncoder.encode(oldKey),SafeEncoder.encode(newKey)); }catch (Exception e){ e.printStackTrace(); }finally { //关闭jedis连接,避免资源占用 jedis.close(); } return state; } /** * 在新key不存在时,将旧key修改成该新key的名称 * * @param oldKey * @param newKey * @return 状态码 */ public Long renamenx(String oldKey,String newKey){ long status = 0; try{ //重命名key status = jedis.renamenx(oldKey,newKey); }catch (Exception e){ //关闭jedis连接,避免资源占用 jedis.close(); } return status; } //对存储结构为String的操作(Redis中,字符串类型的value最大可容纳的数据为512M),这是对value的操作 /** * 根据key名,获取其存储数据 * @param key * @return */ public String get(String key){ String value = null; try{ //获取指定key的存储数据 value = jedis.get(key); }catch (Exception e){ e.printStackTrace(); }finally { //关闭jedis连接,避免资源占用 jedis.close(); } return value; } /** * 添加记录,如果记录已存在则覆盖原有的value * @param key * @param value * @return */ public String set(String key,String value){ String state = null; try{ //设置指定key的存储数据 state = jedis.set(key,value); }catch (Exception e){ e.printStackTrace(); }finally { //关闭jedis连接,避免资源占用 jedis.close(); } return state; } }
5.获取列表的方式
package com.example.demo.service.impl; import com.example.demo.service.RedisDemoService; import com.example.demo.util.JedisUtil; import com.example.demo.util.Result; import com.example.demo.util.RowsVo; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Title: * @ProjectName * @Description: TODO * @author: TongSiYu * @date 2021-8-12 16:07 */ @Service public class RedisDemoServiceImpl implements RedisDemoService { @Autowired private JedisUtil jedisUtil; //指定key前缀 private static final String AREALIST = "arealist"; @Override @Transactional(propagation = Propagation.REQUIRED) public Result getList() { RowsVo vo = new RowsVo(); //定义Redis的key名 String key = AREALIST ; //定义接收对象 List
6.测试
package com.example.demo.contoller; import com.example.demo.service.RedisDemoService; import com.example.demo.util.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * @Title: * @ProjectName * @Description: TODO * @author: TongSiYu * @date 2021-8-12 16:13 */ @Controller @RequestMapping("demo") public class RedisDemoContoller { @Autowired private RedisDemoService redisDemoService; @RequestMapping("get") @ResponseBody public Result get() { return redisDemoService.getList(); } }