- 一
- 新建project
选中Web,cache,mysql,mybatis模块
- 创建数据库表和Javabean(字段\构造器\getter and setter\tostring)
- 配置数据源信息
application.properties中
spring.datasource.url=jdbc:mysql://localhost:3306/cache
spring.datasource.username=root
spring.datasource.password=123456
# spring.datasource.driver-class-name=com.mysql.jdbc.Driver
-
@MapperScan指定需要扫描的mapper接口所在的包
主程序上
@MapperScan("com.example.springdatacache.mapper")
@SpringBootApplication
public class SpringdataCacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringdataCacheApplication.class, args);
}
}
- 新建mapper包,EmployeeMapper\DepartmentMapper接口类,添加@Mapper注解(也可以不用)
package com.example.springdatacache.mapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EmployeeMapper {
}
- 里面添加crud方法
@Select("select * from employee where id=#{id}")
public Employee getEmpById(Integer id);
@Update("update employee set lastName=#{lastName},email=#{email},gender=#{gender},d_id=#{dId} where id=#{id}")
public void updateEmp(Employee employee);
@Delete("DELETE FROM employee WHERE id=#{id}")
public void deleteEmpById(Integer id);
@Insert("insert into employee(lastName,email,gender,d_id) values(#{lastName},#{email},#{gender},#{dId}")
public void insertEmployee(Employee employee);
- 修改测试类
package com.example.springdatacache;
import com.example.springdatacache.bean.Employee;
import com.example.springdatacache.mapper.EmployeeMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringdataCacheApplicationTests {
@Autowired
EmployeeMapper employeeMapper;
@Test
void contextLoads() {
Employee empById = employeeMapper.getEmpById(1);
System.out.println(empById);
}
}
运行时报错:java.sql.SQLException: The server time zone value ‘�й���ʱ��’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
解决方法:在数据库配置路径上加上 ?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
- 新建service包EmployeeService类
package com.example.springdatacache;
import com.example.springdatacache.bean.Employee;
import com.example.springdatacache.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EmployeeService {
@Autowired
EmployeeMapper employeeMapper;
public Employee getEmp(Integer id){
System.out.println("查询"+id+"号员工");
Employee emp = employeeMapper.getEmpById(id);
return emp;
}
}
- 新建controller包EmployeeController
package com.example.springdatacache.controller;
import com.example.springdatacache.EmployeeService;
import com.example.springdatacache.bean.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EmployeeController {
@Autowired
EmployeeService employeeService;
@GetMapping("/emp/{id}")
public Employee getEmployee(@PathVariable("id") Integer id){
Employee emp = employeeService.getEmp(id);
return emp;
}
}
此时数据库中的字段为
javabean中的字段为dId
在application.properties中新增
#开启驼峰命名匹配规则
mybatis.configuration.map-underscore-to-camel-case=true
- 二
- 快速体验缓存
- 开启基于注解的缓存 @EnableCaching
- 标注缓存注解即可 @Cacheable ,@CacheEvict, @CachePut
- 主程序类中
@MapperScan("com.example.springdatacache.mapper")
@SpringBootApplication
@EnableCaching
public class SpringdataCacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringdataCacheApplication.class, args);
}
}
- application.properties中开启日志,打印sql语句
logging.level.com.example.springdatacache.mapper = debug
控制台打印
在service中使用@Cacheable
/**
* 将方法中的运行结果进行缓存,以后再要相同的数据,直接从缓存中获取,不用调用方法
* CacheManager管理多个Cache组件,对缓存的真正CRUD操作在Cache组件中,每一个缓存组件有自己唯一一个名字
*
* 几个属性:
* cacheNames/value :指定缓存组件的名字
* key:缓存数据使用的key可以用它来指定.默认是使用方法参数的值 1-方法的返回值
* 编写Spel #id,参数id的值 #a0 #p0 #root.arg[0]
* (key/keyGenerator二选一使用)keyGenerator:key的生成器;可以自己指定key的生成器的组件id
* cacheManager:指定缓存管理器 ;或者用cacheResolver指定获取解析器
* condition:指定符合条件的情况下才缓存
* condition="#id>0"
* unless:否定缓存;当unless指定的条件为true,方法的返回值就不会缓存,可以获取到结果进行判断
* unless="#result == null"
* sync:是否使用异步模式
*
* @param id
* @return
*/
@Cacheable(cacheNames = {"emp"})
public Employee getEmp(Integer id){
System.out.println("查询"+id+"号员工");
Employee emp = employeeMapper.getEmpById(id);
return emp;
}