SpringBoot整合SpringDataRedis的示例代碼
本文介紹下SpringBoot如何整合SpringDataRedis框架的,SpringDataRedis具體的內(nèi)容在前面已經(jīng)介紹過了,可自行參考。
1.創(chuàng)建項(xiàng)目添加依賴
創(chuàng)建SpringBoot項(xiàng)目,并添加如下依賴:
<dependencies> <!-- springBoot 的啟動(dòng)器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Data Redis 的啟動(dòng)器 --> <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>
2.設(shè)置application.properties文件
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
3.添加Redis的配置類
添加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對(duì)象。在該對(duì)象中完成一些鏈接池配置 * @ConfigurationProperties:會(huì)將前綴相同的內(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)鏈接池的配置對(duì)象 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; } }
4.添加pojo
/** * @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 + "]"; } }
5.單元測試
@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對(duì)象 */ @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對(duì)象 */ @Test public void testGetUsers(){ //重新設(shè)置序列化器 this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); Users users = (Users)this.redisTemplate.opsForValue().get("users"); System.out.println(users); } /** * 基于JSON格式存Users對(duì)象 */ @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對(duì)象 */ @Test public void testGetUseJSON(){ this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class)); Users users = (Users)this.redisTemplate.opsForValue().get("users_json"); System.out.println(users); } }
到此這篇關(guān)于SpringBoot整合SpringDataRedis的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot整合SpringDataRedis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)圖像轉(zhuǎn)碼為字符畫的方法
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖像轉(zhuǎn)碼為字符畫的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03詳解SpringBoot初始教程之Tomcat、Https配置以及Jetty優(yōu)化
本篇文章主要介紹了詳解SpringBoot初始教程之Tomcat、Https配置以及Jetty優(yōu)化,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09SpringMVC九大組件之HandlerMapping詳解
這篇文章主要介紹了SpringMVC九大組件之HandlerMapping詳解,HandlerMapping 叫做處理器映射器,它的作用就是根據(jù)當(dāng)前 request 找到對(duì)應(yīng)的 Handler 和 Interceptor,然后封裝成一個(gè) HandlerExecutionChain 對(duì)象返回,需要的朋友可以參考下2023-09-09Jackson處理Optional時(shí)遇到問題的解決與分析
Optional是Java實(shí)現(xiàn)函數(shù)式編程的強(qiáng)勁一步,并且?guī)椭诜妒街袑?shí)現(xiàn),但是Optional的意義顯然不止于此,下面這篇文章主要給大家介紹了關(guān)于Jackson處理Optional時(shí)遇到問題的解決與分析的相關(guān)資料,需要的朋友可以參考下2022-02-02簡單了解JAVA中類、實(shí)例與Class對(duì)象
這篇文章主要介紹了簡單了解JAVA中類、實(shí)例與Class對(duì)象,類是面向?qū)ο缶幊陶Z言的一個(gè)重要概念,它是對(duì)一項(xiàng)事物的抽象概括,可以包含該事物的一些屬性定義,以及操作屬性的方法,需要的朋友可以參考下2019-06-06java設(shè)計(jì)模式之單例模式學(xué)習(xí)
單例對(duì)象(Singleton)是一種常用的設(shè)計(jì)模式。在Java應(yīng)用中,單例對(duì)象能保證在一個(gè)JVM中,該對(duì)象只有一個(gè)實(shí)例存在2014-01-01