0

0

SpringBoot怎么整合SpringDataRedis

WBOY

WBOY

发布时间:2023-05-13 10:13:05

|

1029人浏览过

|

来源于亿速云

转载

SpringBoot整合SpringDataRedis
1.创建项目添加依赖
  创建SpringBoot项目,并添加如下依赖:


   
   
       org.springframework.boot
       spring-boot-starter-web
   

   
   
       org.springframework.boot
       spring-boot-starter-data-redis
   


       org.springframework.boot
       spring-boot-starter-test
       test
   

   
       redis.clients
       jedis
       2.9.0
   


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2.设置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
1
2
3
4
5
3.添加Redis的配置类
  添加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 redisTemplate(JedisConnectionFactory factory){
       RedisTemplate template = new RedisTemplate();
       //关联
       template.setConnectionFactory(factory);

//为key设置序列化器
       template.setKeySerializer(new StringRedisSerializer());
       //为value设置序列化器
       template.setValueSerializer(new StringRedisSerializer());

return template;
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 + “]”;
   }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
5.单元测试

塔可商城
塔可商城

塔可商城, 一个基于springboot+uniapp+vue3技术栈开发的开源跨平台小程序、管理后台,后端服务的项目,它内置提供了会员分销, 区域代理, 商品零售等功能的新零售电商系统。强大弹性的架构设计,简洁的代码,最新的技术栈,全方面适合不同需求的前端,后端,架构的同学,同时更是企业开发需求的不二选择。 项目结构通过项目结构,你将清楚明白你即将入手的是一个怎么样的项目,你可能需要什么,如何

下载

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootRedisDemoApplication.class)
public class SpringbootRedisDemoApplicationTests {

@Autowired
   private RedisTemplate 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);
   }

}

相关专题

更多
Java编译相关教程合集
Java编译相关教程合集

本专题整合了Java编译相关教程,阅读专题下面的文章了解更多详细内容。

5

2026.01.21

C++多线程相关合集
C++多线程相关合集

本专题整合了C++多线程相关教程,阅读专题下面的的文章了解更多详细内容。

0

2026.01.21

无人机驾驶证报考 uom民用无人机综合管理平台官网
无人机驾驶证报考 uom民用无人机综合管理平台官网

无人机驾驶证(CAAC执照)报考需年满16周岁,初中以上学历,身体健康(矫正视力1.0以上,无严重疾病),且无犯罪记录。个人需通过民航局授权的训练机构报名,经理论(法规、原理)、模拟飞行、实操(GPS/姿态模式)及地面站训练后考试合格,通常15-25天拿证。

7

2026.01.21

Python多线程合集
Python多线程合集

本专题整合了Python多线程相关教程,阅读专题下面的文章了解更多详细内容。

1

2026.01.21

java多线程相关教程合集
java多线程相关教程合集

本专题整合了java多线程相关教程,阅读专题下面的文章了解更多详细内容。

2

2026.01.21

windows激活码分享 windows一键激活教程指南
windows激活码分享 windows一键激活教程指南

Windows 10/11一键激活可以通过PowerShell脚本或KMS工具实现永久或长期激活。最推荐的简便方法是打开PowerShell(管理员),运行 irm https://get.activated.win | iex 脚本,按提示选择数字激活(选项1)。其他方法包括使用HEU KMS Activator工具进行智能激活。

2

2026.01.21

excel表格操作技巧大全 表格制作excel教程
excel表格操作技巧大全 表格制作excel教程

Excel表格操作的核心技巧在于 熟练使用快捷键、数据处理函数及视图工具,如Ctrl+C/V(复制粘贴)、Alt+=(自动求和)、条件格式、数据验证及数据透视表。掌握这些可大幅提升数据分析与办公效率,实现快速录入、查找、筛选和汇总。

6

2026.01.21

毒蘑菇显卡测试网站入口 毒蘑菇测试官网volumeshader_bm
毒蘑菇显卡测试网站入口 毒蘑菇测试官网volumeshader_bm

毒蘑菇VOLUMESHADER_BM测试网站网址为https://toolwa.com/vsbm/,该平台基于WebGL技术通过渲染高复杂度三维分形图形评估设备图形处理能力,用户可通过拖动彩色物体观察画面流畅度判断GPU与CPU协同性能;测试兼容多种设备,但中低端手机易卡顿或崩溃,高端机型可能因发热降频影响表现,桌面端需启用独立显卡并使用支持WebGL的主流浏览器以确保准确结果

9

2026.01.21

github中文官网入口 github中文版官网网页进入
github中文官网入口 github中文版官网网页进入

github中文官网入口https://docs.github.com/zh/get-started,GitHub 是一种基于云的平台,可在其中存储、共享并与他人一起编写代码。 通过将代码存储在GitHub 上的“存储库”中,你可以: “展示或共享”你的工作。 持续“跟踪和管理”对代码的更改。

7

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

相关下载

更多

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Redis6入门到精通超详细教程
Redis6入门到精通超详细教程

共47课时 | 5.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号