建立redis的连接池
1、在配置文件中添加参数(application-dev.yml)
1 2 3 4 5 6 7 | redis: # *** 邮件发送服务器地址 host: 127.0 . 0.1 # *** 邮件发送服务器端口 port: 6379 # *** 提醒邮件发件人邮箱 timeout: 10000 |
2、建一个获取redis连接的工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | @Component public class JTRedisUtils { protected static final Logger logger = Logger.getLogger(JTRedisUtils. class ); private JedisPool jedisPool; @Autowired private JTRedisConfig redisConfig; @Value ( "${redis.host}" ) private String host; @Value ( "${redis.port}" ) private int port; @Value ( "${redis.timeout}" ) private int timeout; public JedisPool redisPoolFactory() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, null ); logger.info( "JedisPool注入成功!!" ); logger.info( "redis地址:" + host + ":" + port); return jedisPool; } public Jedis getResource() { if (jedisPool == null ){ jedisPool = redisPoolFactory(); } return jedisPool.getResource(); } } |
3、测试获取redis连接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @Autowired private JTRedisUtils redisUtils; @Autowired private JTNoteClassificationMapper noteClassificationMapper; @Test public void testImportData() { Jedis jedis = redisUtils.getResource(); JSONObject jsonObject = new JSONObject(); jsonObject.put( "userId" , "e92d0309e0df4ef798a3d3f8e75eff6d" ); List<JTNoteClassificationEntity> noteClassificationEntityList = noteClassificationMapper.getJTNoteClassificationEntityListByUserId( "e92d0309e0df4ef798a3d3f8e75eff6d" ); for (JTNoteClassificationEntity noteClassificationEntity : noteClassificationEntityList){ jedis.lpush( "item_id_list" ,noteClassificationEntity.getId()); jedis.hset( "itemlist" +noteClassificationEntity.getId(), "id" ,noteClassificationEntity.getId()+ "" ); jedis.hset( "itemlist" +noteClassificationEntity.getId(), "title" ,noteClassificationEntity.getTitle()); } System.out.println( "操作完成" ); jedis.close(); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话