本文介绍下SpringBoot如何整合SpringDataRedis框架的,SpringDataRedis具体的内容在前面已经介绍过了,可自行参考。
创建SpringBoot项目,并添加如下依赖:
<dependencies> <!-- springBoot 的启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Data Redis 的启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies>
spring.redis.jedis.pool.max-idle=10 spring.redis.jedis.pool.min-idle=5 spring.redis.pool.max-total=20 spring.redis.hostName=192.168.88.120 spring.redis.port=6379
添加Redis的java配置类,设置相关的信息。
/** * @program: springboot-redis-demo * @description: Redis的配置类 * @author: 波波烤鸭 * @create: 2019-05-20 23:40 */ @Configuration public class RedisConfig { /** * 1.创建JedisPoolConfig对象。在该对象中完成一些链接池配置 * @ConfigurationProperties:会将前缀相同的内容创建一个实体。 */ @Bean @ConfigurationProperties(prefix="spring.redis.pool") public JedisPoolConfig jedisPoolConfig(){ JedisPoolConfig config = new JedisPoolConfig(); /*//最大空闲数 config.setMaxIdle(10); //最小空闲数 config.setMinIdle(5); //最大链接数 config.setMaxTotal(20);*/ System.out.println("默认值:"+config.getMaxIdle()); System.out.println("默认值:"+config.getMinIdle()); System.out.println("默认值:"+config.getMaxTotal()); return config; } /** * 2.创建JedisConnectionFactory:配置redis链接信息 */ @Bean @ConfigurationProperties(prefix="spring.redis") public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){ System.out.println("配置完毕:"+config.getMaxIdle()); System.out.println("配置完毕:"+config.getMinIdle()); System.out.println("配置完毕:"+config.getMaxTotal()); JedisConnectionFactory factory = new JedisConnectionFactory(); //关联链接池的配置对象 factory.setPoolConfig(config); //配置链接Redis的信息 //主机地址 /*factory.setHostName("192.168.70.128"); //端口 factory.setPort(6379);*/ return factory; } /** * 3.创建RedisTemplate:用于执行Redis操作的方法 */ @Bean public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory factory){ RedisTemplate<String, Object> template = new RedisTemplate<>(); //关联 template.setConnectionFactory(factory); //为key设置序列化器 template.setKeySerializer(new StringRedisSerializer()); //为value设置序列化器 template.setValueSerializer(new StringRedisSerializer()); return template; } }
/** * @program: springboot-redis-demo * @description: Users * @author: 波波烤鸭 * @create: 2019-05-20 23:47 */ public class Users implements Serializable { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Users [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringbootRedisDemoApplication.class) public class SpringbootRedisDemoApplicationTests { @Autowired private RedisTemplate<String, Object> redisTemplate; /** * 添加一个字符串 */ @Test public void testSet(){ this.redisTemplate.opsForValue().set("key", "bobokaoya..."); } /** * 获取一个字符串 */ @Test public void testGet(){ String value = (String)this.redisTemplate.opsForValue().get("key"); System.out.println(value); } /** * 添加Users对象 */ @Test public void testSetUesrs(){ Users users = new Users(); users.setAge(20); users.setName("张三丰"); users.setId(1); //重新设置序列化器 this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); this.redisTemplate.opsForValue().set("users", users); } /** * 取Users对象 */ @Test public void testGetUsers(){ //重新设置序列化器 this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); Users users = (Users)this.redisTemplate.opsForValue().get("users"); System.out.println(users); } /** * 基于JSON格式存Users对象 */ @Test public void testSetUsersUseJSON(){ Users users = new Users(); users.setAge(20); users.setName("李四丰"); users.setId(1); this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class)); this.redisTemplate.opsForValue().set("users_json", users); } /** * 基于JSON格式取Users对象 */ @Test public void testGetUseJSON(){ this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class)); Users users = (Users)this.redisTemplate.opsForValue().get("users_json"); System.out.println(users); } }
到此这篇关于SpringBoot整合SpringDataRedis的示例代码的文章就介绍到这了,更多相关SpringBoot整合SpringDataRedis内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
免责声明:本站发布的内容(图片、视频和文字)以原创、来自本网站内容采集于网络互联网转载等其它媒体和分享为主,内容观点不代表本网站立场,如侵犯了原作者的版权,请告知一经查实,将立刻删除涉嫌侵权内容,联系我们QQ:712375056,同时欢迎投稿传递力量。
Copyright © 2009-2022 56dr.com. All Rights Reserved. 特网科技 特网云 版权所有 特网科技 粤ICP备16109289号
域名注册服务机构:阿里云计算有限公司(万网) 域名服务机构:烟台帝思普网络科技有限公司(DNSPod) CDN服务:阿里云计算有限公司 百度云 中国互联网举报中心 增值电信业务经营许可证B2
建议您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流浏览器浏览本网站