springboot缓存基础配置
首先连接数据库
一个实体类,employee.java(属性1加get,set方法等等)
然后EmployeeMapper类
@Mapper
public interface EmployeeMapper{
@Select("select * from employee where id =#{id}")
public Employee getEmployee(Integer id);
@Update("update employee set lastName=#{lastName},email=#{email},gender=#{gender},d_id=#{dId} where id =#{id}")
public void updateEmployee(Employee employee);
@Delete("delete from employee where id=#{id}")
public void deleteEmployee(Integer id);
@Insert("insert into employee(lastName,email,gender,d_id) values(#{lastName},#{email},#{gender},#{dId})")
public void insertEmployee(Employee employee);
@Select("select * from employee where lastName=#{lastName}")
public Employee getEmpByLastName(String lastName);
}
application.yml:
spring:
datasource:
url: jdbc:mysql://localhost:3306/spring_cache?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
configuration:
map-underscore-to-camel-case: true # 驼峰命名法
以上加上自动注入@autowired,在controller里调用下方法就可以连接并操作数据库
定义的内容了。
然后是配置缓存
主要是
EmployeeService.class
@CacheConfig(cacheNames="emp")//能够全局定义,下面的都可以不写values="emp"
@Service
public class EmployeeService {
@Autowired
EmployeeMapper employeeMapper;
/**
* cacheName/values:指定缓存的组件名字
* key:
* keyGenerator
* cacheManager
* Condition
* unless
* sync
*/
@Cacheable(cacheNames = ("emp")) //cacheNames==value
// ,keyGenerator = "myKeyGenerator", condition = "#a0>1", unless = "#a0==2")
//将方法运行的结果进行缓存,再访问该数据,就不用调用该方法,也不用方法数据库
public Employee getEmp(Integer id) {
System.out.println("查询员工:" + id);
Employee employee = employeeMapper.getEmployee(id);
return employee;
}
@CachePut(value = "emp",key = "#employee.id"/*=#result.id*/)//这里的key要与查询的是key绑定,不然更新的key不绑定一个的话,只能数据库更新,而不能更新缓存
public Employee updateEmp(Employee employee) { //默认跟参数绑定即(Employee 的employee),上面那个参数是(Integer id) ,
System.out.println("updateEmp:"+employee); // 所以这个key要改成key = "#employee.id"或者key="#result.id",才能绑定上面那个key
employeeMapper.updateEmployee(employee);
return employee;
}
// @CacheEvict(value = "emp",key = "#id")//默认是传参的值Integer id
// @CacheEvict(value = "emp",allEntries = true)//allEntries效果是不再按照key的值来删除,而是删除所有的"emp“里的缓存
@CacheEvict(value = "emp",beforeInvocation =true )//beforeInvocation默认是在方法之后执行清除缓存,如果方法有错误则无法清除缓
//加了true以后则在方法之前执行清除缓存,无论方法有无错误都能清除缓存
public void deleteEmp(Integer id){
// employeeMapper.deleteEmployee(id);
// int a=10/0;
System.out.println("打印已经删除了");
}
//定义复杂的缓存规则
@Caching(
cacheable = {
@Cacheable(value = "emp",key="#lastName")
},
put = {
@CachePut(value = "emp",key = "#result.id"),
@CachePut(value = "emp",key = "#result.email")//直接绑定在一起
}
)
public Employee getEmployByLastName(String lastName){
System.out.println("资金");
return employeeMapper.getEmpByLastName(lastName);
}
}
还可以自定义key:
```javascript
@Configuration
public class MyCacheConfig {
@Bean("myKeyGenerator")
public KeyGenerator keyGenerator(){
return new KeyGenerator(){
@Override
public Object generate(Object o, Method method, Object... objects) {
return method.getName()+"["+ Arrays.asList(objects).toString()+"]";
}
};
}
}
main方法要加@EnableCaching
@MapperScan("com.lbl.springboot.cache.mapper")
@SpringBootApplication
//三个缓存标签
//@Cacheable
//@CacheEvict
//@CachePut
@EnableCaching//开启注解缓存
public class Springboot07CacheApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot07CacheApplication.class, args);
}
}
最后controller用页面访问
@RestController
public class EmployeeController {
@Autowired
EmployeeService employeeService;
@GetMapping("/emp/{id}")
public Employee getEmployee(@PathVariable("id") Integer id){
Employee emp = employeeService.getEmp(id);
return emp;
}
}
@CacheConfig:
@CacheConfig(cacheNames="emp")//能够全局定义,下面的都可以不写values="emp"
@Cacheable:
@Cacheable(cacheNames = ("emp"))
//cacheNames==value
// ,keyGenerator = "myKeyGenerator", condition = "#a0>1", unless = "#a0==2")
//将方法运行的结果进行缓存,再访问该数据,就不用调用该方法,也不用方法数据库
@CachePut:
@CachePut(value = "emp",key = "#employee.id"/*=#result.id*/)
//这里的key要与查询的是key绑定,不然更新的key不绑定一个的话,只能数据库更新,而不能更新缓存
public Employee updateEmp(Employee employee) { //默认跟参数绑定即(Employee 的employee),上面那个参数是(Integer id) ,
System.out.println("updateEmp:"+employee); // 所以这个key要改成key = "#employee.id"或者key="#result.id",才能绑定上面那个key
@CacheEvict:
@CacheEvict(value = "emp",key = "#id")//默认是传参的值Integer id
// @CacheEvict(value = "emp",allEntries = true)//allEntries效果是不再按照key的值来删除,而是删除所有的"emp“里的缓存
@CacheEvict(value = "emp",beforeInvocation =true )//beforeInvocation默认是在方法之后执行清除缓存,如果方法有错误则无法清除缓
//加了true以后则在方法之前执行清除缓存,无论方法有无错误都能清除缓存
@Caching:
//定义复杂的缓存规则
@Caching(
cacheable = {
@Cacheable(value = "emp",key="#lastName")
},
put = {
@CachePut(value = "emp",key = "#result.id"),
@CachePut(value = "emp",key = "#result.email")//直接绑定在一起
}
)