您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“SpringBoot整合SpringDataRedis的方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“SpringBoot整合SpringDataRedis的方法”吧!
創(chuàng)建SpringBoot項(xiàng)目,并添加如下依賴:
<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配置類,設(shè)置相關(guān)的信息。
/** * @program: springboot-redis-demo * @description: Redis的配置類 * @author: 波波烤鴨 * @create: 2019-05-20 23:40 */ @Configuration public class RedisConfig { /** * 1.創(chuàng)建JedisPoolConfig對象。在該對象中完成一些鏈接池配置 * @ConfigurationProperties:會將前綴相同的內(nèi)容創(chuàng)建一個(gè)實(shí)體。 */ @Bean @ConfigurationProperties(prefix="spring.redis.pool") public JedisPoolConfig jedisPoolConfig(){ JedisPoolConfig config = new JedisPoolConfig(); /*//最大空閑數(shù) config.setMaxIdle(10); //最小空閑數(shù) config.setMinIdle(5); //最大鏈接數(shù) config.setMaxTotal(20);*/ System.out.println("默認(rèn)值:"+config.getMaxIdle()); System.out.println("默認(rèn)值:"+config.getMinIdle()); System.out.println("默認(rèn)值:"+config.getMaxTotal()); return config; } /** * 2.創(chuàng)建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(); //關(guān)聯(lián)鏈接池的配置對象 factory.setPoolConfig(config); //配置鏈接Redis的信息 //主機(jī)地址 /*factory.setHostName("192.168.70.128"); //端口 factory.setPort(6379);*/ return factory; } /** * 3.創(chuàng)建RedisTemplate:用于執(zhí)行Redis操作的方法 */ @Bean public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory factory){ RedisTemplate<String, Object> template = new RedisTemplate<>(); //關(guān)聯(lián) template.setConnectionFactory(factory); //為key設(shè)置序列化器 template.setKeySerializer(new StringRedisSerializer()); //為value設(shè)置序列化器 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; /** * 添加一個(gè)字符串 */ @Test public void testSet(){ this.redisTemplate.opsForValue().set("key", "bobokaoya..."); } /** * 獲取一個(gè)字符串 */ @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); //重新設(shè)置序列化器 this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); this.redisTemplate.opsForValue().set("users", users); } /** * 取Users對象 */ @Test public void testGetUsers(){ //重新設(shè)置序列化器 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的方法”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。