亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

SpringBoot整合SpringDataRedis的示例代碼

瀏覽:78日期:2023-03-12 13:25:20

  本文介紹下SpringBoot如何整合SpringDataRedis框架的,SpringDataRedis具體的內容在前面已經介紹過了,可自行參考。

1.創建項目添加依賴

  創建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>2.設置application.properties文件

spring.redis.jedis.pool.max-idle=10spring.redis.jedis.pool.min-idle=5spring.redis.pool.max-total=20spring.redis.hostName=192.168.88.120spring.redis.port=63793.添加Redis的配置類

  添加Redis的java配置類,設置相關的信息。

/** * @program: springboot-redis-demo * @description: Redis的配置類 * @author: 波波烤鴨 * @create: 2019-05-20 23:40 */@Configurationpublic 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; }}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; /** * 添加一個字符串 */ @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的示例代碼的文章就介紹到這了,更多相關SpringBoot整合SpringDataRedis內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 91三级视频| 国产免费91视频 | 欧美日韩中文字幕一区二区高清 | 五月天综合婷婷 | 自拍激情视频 | 久久夏同学国产免费观看 | 97超级碰碰碰久久久观看 | 香蕉视频在线观看网站 | 久久最新网址 | 免费人成观看在线网 | 成免费网站 | 麻豆影视在线 | 国产一区二区三区四区在线 | 亚洲国产精品久久人人爱 | 国产免费69成人精品视频 | 免费观看污污视频 | 性高清| 麻豆影视在线播放 | 大香萑75久久精品免费 | 亚色最新网址 | 午夜视频偷拍在线观看免费 | 在线观看黄色影视 | 日本大片免a费观看视频+播放器 | 久久五月女厕所一区二区 | 亚洲视频免费在线 | 美日韩精品 | 成人综合婷婷国产精品久久免费 | 麻豆果冻精品一区二区 | 亚洲一区二区三区视频 | 一级一级特黄女人精品毛片 | 香蕉香蕉国产片一级一级毛片 | 久久毛片网 | 色综合图片 | 国产成人啪精品视频免费网 | 久久久久国产精品免费免费不卡 | 五月天综合婷婷 | 影音先锋5566中文源资源 | 韩国特黄色免费 | 国产精品酒店 | 伊人tv| 亚洲美女视频网 |