话不多说,demo走起
pom文件引入
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
application.properties配置文件
#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.timeout=60s
# 数据库连接超时时间,2.0 中该参数的类型为Duration,这里在配置的时候需要指明单位
#spring.redis.timeout=60s
# 连接池配置,2.0中直接使用jedis或者lettuce配置连接池
# 最大活跃连接数,负数为不限制
spring.redis.lettuce.pool.max-active=500
# 等待可用连接的最大时间,负数为不限制
spring.redis.lettuce.pool.max-wait=-1ms
# 最大空闲连接数
spring.redis.lettuce.pool.max-idle=100
# 最小空闲连接数
spring.redis.lettuce.pool.min-idle=20
servers层的实现类
package com.hjf.services.impl;
import com.hw.entity.Student;
import com.hw.mapper.StudentsMapper;
import com.hw.services.StudentsServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentsServicesImpl implements StudentsServices {
@Autowired
private StudentsMapper studentsMapper;
@Cacheable(value = "student", key = "#root.targetClass", unless = "#result eq null")
@Override
public List<Student> getAllStudents() {
return studentsMapper.getAllStudents();
}
@CacheEvict(value = "student",key = "#root.targetClass", condition ="#result gt 0")
@Override
public int addStudent(Student student){
return studentsMapper.addStudent(student);
}
}
解释一下
注解@Cacheable标记在一个方法上时表示该方法是支持缓存的,当标记在一个类上时则表示该类所有的方法都是支持缓存的,Spring会在其被调用后将其返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而不需要再次执行该方法
@CacheEvict则是在redis删除这个缓存
import com.hw.entity.Student;
import com.hw.services.StudentsServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.UUID;
/**
* @program: Maven
* @description:
* @author: hw
**/
@RestController
public class selectStudentController {
@Autowired //注入 完成自动装配
private StudentsServices studentsServices;
@RequestMapping("/getStudents")
public List<Student> getAllStudents(){
System.out.println("集合长度:"+studentsServices.getAllStudents().size());
return studentsServices.getAllStudents();
}
@RequestMapping("/hello")
public String hello(){
return "你好,boot-ssm";
}
@RequestMapping("/add")
public String addStudent(){
Student student=new Student(UUID.randomUUID().toString(),1);
studentsServices.addStudent(student);
return "添加成功";
}
}
版权声明:本文为CSDN博主「beyond丿q:1559810637」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_41594146/article/details/85885423