1,导入redis和mybatis相关依赖
<!-- redis依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- mysql连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2,开启扫描(也可在每一个Mapper接口上使用@Mapper注解)
@MapperScan("com.example.mapper")
@SpringBootApplication
public class RedisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(RedisDemoApplication.class, args);
}
}
3,编写application.properties 配置文件,配置mysql和redis连接的相关数据
#Redis连接
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.84.128
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=200
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
#数据库连接
spring.redis.timeout=10000
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://192.168.84.128:3309/jdbc
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#在控制台输出mapper包下执行的sql语句,不写默认没输出
logging.level.com.example.mapper=debug
4,编写、配置mapper(@select是导入mybatis依赖才有)
public interface StudentMapper {
@Select("select id,sname from student")
public List<Student> queryAll();
}
4.1,编写pojo
public class Student implements Serializable {
private Integer id;
private String sname;
get...
set..
}
4.2,编写service
@Service
public class StudentService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private RedisTemplate sturedisTemplate;
@Autowired
private StudentMapper studentMapper;
public List<Student> queryAll(){
ValueOperations value = sturedisTemplate.opsForValue();
if(value.get("csredis")==null){
System.out.println("kong");
List<Student> list = studentMapper.queryAll();
value.set("csredis",list);
return list;
}else{
System.out.println("not kong");
List<Student> csredis = (List<Student>)value.get("csredis");
return csredis;
}
}
}
说明:(需导入redis 依赖。)
StringRedisTemplate stringRedisTemplate :redis用来操作String类型数据,类似JDBC Template
RedisTemplate redisTemplate :redis用来操作对象类型数据,类似JDBC Template
RedisTemplate sturedisTemplate:自定义的RedisTemplate,改变原来redis对象中key-value的类型
操作redis的 5种方法类型:
stringRedisTemplate.opsForValue();//操作字符串类型
stringRedisTemplate.opsForHash();//操作Hash类型
stringRedisTemplate.opsForList();//操作List类型
stringRedisTemplate.opsForSet();//操作Set类型
stringRedisTemplate.opsForZSet();//操作Zset类型
4.3,编写controller
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/queryAll")
public List<Student> queryAll(){
return studentService.queryAll();
}
}
5,编写一个myconfig
@Configuration
public class Myconfig {
@Bean
public RedisTemplate<Object, Object> sturedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer json=new Jackson2JsonRedisSerializer(Object.class);
template.setDefaultSerializer(json);
return template;
}
}
编写配置myconfig原因说明:
因为使用RedisTemplate<Object,Object> 缓存对象数据时候,存进去的数据在redis管理工具界面看到的是所谓的“乱码”,主要是redis使用了默认配置的序列化JdkSerializationRedisSerializer。所以为了“避免”这种情况当我们按规则自己去写一个自定义的,这样就不会去使用redis默认的了。原因是,在RedisAutoConfiguration类中有一个注解 @ConditionalOnMissingBean(name = {"redisTemplate"} ),表示当找不到redisTemplate Bean的时候,才会执行下面方法,否则不执行,因此我们可以修改下面方法,使用自己的序列化Jackson2JsonRedisSerializer 。
@Bean
@ConditionalOnMissingBean(name = {"redisTemplate"} )
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
使用默认的RedisTemplate<Object,Object>时看到:
使用自定义RedisTemplate<Object,Object>后看到:
当然无论是使用默认的还是自定义的都可以向redis中存/取数据。
6,测试。