运行环境:spring-boot-starter-data-redis 1.5.10RELEASE,jedis:2.9
其他配置,启动器器已经添加 @EnableTransactionManagement 注解
//配置类@Configurationpublic class RedisConfig { @Bean public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){ RedisTemplate<String,Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(factory); template.setKeySerializer(new StringRedisSerializer()); //实例化字符串 template.setValueSerializer(new StringRedisSerializer()); return template; } }
测试代码
@Autowired private RedisTemplate redisTemplate; @Test @Transactional(rollbackFor = Exception.class) public void test() { redisTemplate.multi(); redisTemplate.opsForValue().setIfAbsent("pxl","wxb1"); redisTemplate.expire("pxl",100, TimeUnit.SECONDS); redisTemplate.discard(); //redisTemplate.exec(); }
测试结果
慕容3067478
江户川乱折腾