参考:Redis:使用Java与Redis交互_哔哩哔哩_bilibili
添加依赖
<!--springboot操作redis依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
在application.yml中做配置
spring.redis.host=ip
spring.redis.port=端口
spring.redis.database=数据库
spring.redis.password=密码
在test中测试
package com.example.demo; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.StringRedisTemplate; import javax.annotation.Resource; @SpringBootTest class DemoApplicationTests { @Resource private StringRedisTemplate template; @Test void contextLoads() { template.opsForValue().set("a","888"); String result=template.opsForValue().get("a"); System.out.println(result); template.delete("a"); System.out.println(template.hasKey("a")); } }
由于spring没有专门的redis事务管理器,所以只能用jdbc提供的
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
写个redisService.java
redisService.java
package com.example.demo.service; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.PostConstruct; import javax.annotation.Resource; /** * @Date 2023/7/1 - 10:10 */ @Service public class redisService { @Resource StringRedisTemplate template; //@PostConstruct注解的方法在项目启动的时候执行这个方法,也可以理解为在spring容器启动的时候执行 @PostConstruct public void init(){ template.setEnableTransactionSupport(true); //需要开启事务 System.out.println("Redis事务开启"); } @Transactional //需要添加此注解 public void test(){ template.multi(); //标记事务开始 template.opsForValue().set("d","xxxx"); template.exec(); //执行 } }
redis的事务操作:redis事务操作_好好学习,天天上当!的博客-CSDN博客
在application.yml中,还需要配置datasource
测试:在test中测试,就调用redisService.java中的test方法
package com.example.demo; import com.example.demo.service.redisService; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.StringRedisTemplate; import javax.annotation.Resource; @SpringBootTest class DemoApplicationTests { @Resource private StringRedisTemplate template; @Resource private redisService redisService; @Test void contextLoads() { redisService.test(); } }
运行test
使用json序列化,默认是用jdk序列化
加个jackson依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency>
redisService.java
package com.example.demo.service; import com.example.demo.po.dept; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.PostConstruct; import javax.annotation.Resource; /** * @Date 2023/7/1 - 10:10 */ @Service public class redisService { @Resource RedisTemplate<Object,Object> template; //@PostConstruct注解的方法在项目启动的时候执行这个方法,也可以理解为在spring容器启动的时候执行 @PostConstruct public void init(){ template.setEnableTransactionSupport(true); //需要开启事务 //设置序列化器,把对象转换成json类型 template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); System.out.println("Redis事务开启"); } @Transactional //需要添加此注解 public void test(){ template.multi(); //标记事务开始 dept dept=new dept(); dept.setDeptno(1); dept.setDname("lisi"); template.opsForValue().set("d",dept); template.exec(); //执行 } }
test.java,没改动
package com.example.demo; import com.example.demo.service.redisService; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; @SpringBootTest class DemoApplicationTests { @Resource private redisService redisService; @Test void contextLoads() { redisService.test(); } }
测试:这里好像数据类型不对
不使用json序列化,用默认的jdk序列化
需要让实体类实现序列化接口,就不用在redisService.java中设置序列化器了
设置序列化器注释掉
测试:启动test