Docker安装Redis
一、启动docker容器
systemctl start docker
二、拉取redis镜像
docker pull redis
三、端口映射
docker run --name redis -p 6379:6379 -d redis
四、启动redis
docker start redis
五、查询ip
ip addr
六、连接redis
Spring boot 整合 redis
一、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、配置yml文件
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/studentdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: root
redis:
host:
port: 6379
database: 0
logging:
level:
com.nebula.springboot_redis.mapper: debug
三、启动类中加入注解
@MapperScan(value = "com.nebula.springboot_redis.mapper")
@SpringBootApplication
public class SpringbootRedisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootRedisApplication.class, args);
}
}
项目结构如下:
四、加入RedisConfig文件
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
实体类
@Data
public class Student implements Serializable {
private Integer id;
private String name;
private String sex;
}
Mapper
@Mapper
public interface StudentMapper {
@Select("select * from student")
List<Student> findAll();
@Select("select * from student where id=#{id}")
Student findById(Integer id);
@Delete("delete from student where id =#{id}")
int del(Integer id);
@Update("update student set name =#{name},sex=#{sex} where id=#{id}")
int modify(Student student);
}
Service
public interface StudentService {
List<Student> findAll();
Student findById(Integer id);
int del(Integer id);
int modify(Student student);
}
实现层
@Service
public class StudentServiceImpl implements StudentService {
@Resource
StudentMapper studentMapper;
@Resource
RedisTemplate redisTemplate;
@Override
public List<Student> findAll() {
String key = "student";
ListOperations<String, Student> operations = redisTemplate.opsForList();
if (redisTemplate.hasKey(key)) {
return operations.range(key, 0, -1);
} else {
List<Student> list = studentMapper.findAll();
operations.leftPushAll(key, list);
return list;
}
}
@Override
public Student findById(Integer id) {
String key = "student_" + id;
ValueOperations<String, Student> operations = redisTemplate.opsForValue();
if (redisTemplate.hasKey(key)) {
return operations.get(key);
} else {
Student student = studentMapper.findById(id);
operations.set(key, student);
return student;
}
}
@Override
public int del(Integer id) {
int count = studentMapper.del(id);
String key = "student_" + id;
if (redisTemplate.hasKey(key)) {
redisTemplate.delete(key);
}
return count;
}
@Override
public int modify(Student student) {
int count = studentMapper.modify(student);
ValueOperations operations = redisTemplate.opsForValue();
String key = "student_" + student.getId();
if (redisTemplate.hasKey(key)) {
Student stu = studentMapper.findById(student.getId());
operations.set(key, stu);
}
return count;
}
}
控制层
@RestController
public class StudentController {
@Resource
StudentService studentService;
@GetMapping("/student")
public List<Student> findAll() {
return studentService.findAll();
}
@GetMapping("/student/{id}")
public Student findById(@PathVariable("id") Integer id) {
return studentService.findById(id);
}
@PostMapping("/student/{id}")
public int del(@PathVariable("id") Integer id) {
return studentService.del(id);
}
@PutMapping("/student")
public int modify(Student student) {
return studentService.modify(student);
}
}
运行,使用 Postman 请求查询所有方法
请求了一次数据库
查看 Redis 可视化工具
完成!!!下次再查询所有时将无需再重新请求数据库,而是直接从缓存中取数据。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)