Jedis应用
Jedis
配置类
package com.example.demo.configure; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @Configuration @Slf4j public class JedisConfig extends CachingConfigurerSupport { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Bean public JedisPool redisPoolFactory(){ JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host,port); log.info("JedisPool注入成功!"); log.info("redis地址:" + host + ":" + port); return jedisPool; } }
schema.sql

drop table users if exists; create table users ( id bigint auto_increment, name varchar(255), age int, create_time timestamp, primary key (id) ); insert into users (name, create_time,age) values ('Lili', now(),29); insert into users (name, create_time,age) values ('Fiona', now(),30); insert into users (name, create_time,age) values ('xyz', now(),30); insert into users (name, create_time,age) values ('zbc', now(),16); insert into users (name, create_time,age) values ('Nana', now(),18);
实体类Users.java

package com.example.demo.model; import lombok.*; import org.hibernate.annotations.CreationTimestamp; import javax.persistence.*; import java.util.Date; @Entity @Table(name = "users") @Data @Builder @ToString(callSuper = true) @NoArgsConstructor @AllArgsConstructor public class Users { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String name; private int age; @Column(updatable = false) @CreationTimestamp private Date createTime; }
数据操作接口UsersRepository.java

package com.example.demo.repository; import com.example.demo.model.Users; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.stereotype.Repository; import java.util.List; @RepositoryRestResource(collectionResourceRel = "user", path = "user") public interface UsersRepository extends JpaRepository<Users,Integer> { List<Users> findByName(@Param("name") String name); List<Users> findByNameContaining(@Param("name") String name); }
控制器
package com.example.demo.controller; import com.example.demo.repository.UsersRepository; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import java.util.Map; @Slf4j @RestController public class DemoController { @Autowired UsersRepository usersRepository; @Autowired private JedisPool jedisPool; @RequestMapping("/redis") public String redis() { try (Jedis jedis = jedisPool.getResource()) { usersRepository.findAll().forEach(c -> { jedis.hset("users", c.getName(), Integer.toString(c.getAge())); }); Map<String, String> users = jedis.hgetAll("users"); log.info("Users: {}", users); String name = jedis.hget("users", "Fiona"); log.info("Fiona - {}", name); } return "success"; } }
启动项目
GET http://127.0.0.1:8080/redis
查看redis里保存的值
>redis-cli -h 192.168.99.100 -p 6379 192.168.99.100:6379> keys * 1) "users" 2) "key1" 192.168.99.100:6379> type users hash 192.168.99.100:6379> hget users Fiona "30"
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2018-04-08 基于thinkphp的API日志
2016-04-08 查看mysql语句运行时间