首先启动redis
docker run --name redis -p 6379:6379 -d redis
然后连接redis
输入主机ip
yml配置文件
spring:
redis:
database: 0
host: 192.168.249.137
port: 6379
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/t72jsp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
password: root
username: root
server:
port: 8080
mybatis:
configuration:
auto-mapping-behavior: FULL
mapper-locations: classpath:mapper/*.xml
logging:
level:
com.lihu.redis.mapper: debug
Controller
package com.lihu.redis.controller;
import com.lihu.redis.biz.IStudentBiz;
import com.lihu.redis.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class StudentController {
@Autowired
private IStudentBiz biz;
@GetMapping("/findAll")
public List<Student> findAll(){
List<Student> list = biz.findAll();
return list;
}
}
序列化配置类
package com.lihu.redis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import java.net.UnknownHostException;
@Configuration
public class Config {
@Bean
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
//修改默认的序列化规则
//1.创建序列化规则对象
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);
//2.更改默认的序列化规则
template.setDefaultSerializer(jackson2JsonRedisSerializer);
return template;
}
}