标签:control ide 缓存 prope 文件 arc ast bind view
CacheManager:管理各种组件;
Cache:真正操作缓存的接口;
@Cacheable:可缓存的,如果标注在一个方法,则这个方法的返回结果就会被缓存起来;
@CacheEvict:清空缓存,当一个用户被删除时,在删除的方法上面标注,则这个用户的缓存也会被删除;
@CachePut:更新缓存,更新一个用户,标注在更新的方法上,此方法的特点声望,保证方法会被调用,同时结果又会被缓存;
@EnableCaching:开启基于注解的缓存;
keyGenerator:缓存数据时key的生成策略;
serialize:缓存数据是value序列化策略;
1.基本环境搭建
导入:web、Cache、mysql、MyBatis模块;
新建一个spring_cache的数据库,导入以下sql文件,直接导入即可:(注意:linux下mysql默认是要区分表名大小写的)
/* Navicat MySQL Data Transfer Source Server : 本地 Source Server Version : 50528 Source Host : 127.0.0.1:3306 Source Database : springboot_cache Target Server Type : MYSQL Target Server Version : 50528 File Encoding : 65001 Date: 2018-04-27 14:54:04 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for department -- ---------------------------- DROP TABLE IF EXISTS `department`; CREATE TABLE `department` ( `id` int(11) NOT NULL AUTO_INCREMENT, `departmentName` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for employee -- ---------------------------- DROP TABLE IF EXISTS `employee`; CREATE TABLE `employee` ( `id` int(11) NOT NULL AUTO_INCREMENT, `lastName` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `gender` int(2) DEFAULT NULL, `d_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
application.properties:
spring.datasource.url=jdbc:mysql://47.98.202.86:3306/spring_cache
spring.datasource.username=root
spring.datasource.password=root
# 下面这一句可以省略,springboot会默认从url路径读取驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#开启驼峰命名匹配规则
mybatis.configuration.map-underscore-to-camel-case=true
# 开启打印sql
logging.level.com.ning.cache.mapper=debug
bean:
package com.ning.cache.bean; public class Department { private Integer id; private String departmentName; public Department() { super(); // TODO Auto-generated constructor stub } public Department(Integer id, String departmentName) { super(); this.id = id; this.departmentName = departmentName; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } @Override public String toString() { return "Department [id=" + id + ", departmentName=" + departmentName + "]"; } }
package com.ning.cache.bean; public class Employee { private Integer id; private String lastName; private String email; private Integer gender; //性别 1男 0女 private Integer dId; public Employee() { super(); } public Employee(Integer id, String lastName, String email, Integer gender, Integer dId) { super(); this.id = id; this.lastName = lastName; this.email = email; this.gender = gender; this.dId = dId; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getGender() { return gender; } public void setGender(Integer gender) { this.gender = gender; } public Integer getdId() { return dId; } public void setdId(Integer dId) { this.dId = dId; } @Override public String toString() { return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", dId=" + dId + "]"; } }
mapper接口如下:
package com.ning.cache.mapper; import com.ning.cache.bean.Employee; import org.apache.ibatis.annotations.*; @Mapper public interface EmployeeMapper { @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); }
service如下:
package com.ning.cache.service; import com.ning.cache.bean.Employee; import com.ning.cache.mapper.EmployeeMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class EmpService { @Autowired EmployeeMapper employeeMapper; public Employee getEmp(Integer id){ Employee emp = employeeMapper.getEmpById(id); System.out.println(emp); return emp; } }
controller如下:
package com.ning.cache.controller; import com.ning.cache.bean.Employee; import com.ning.cache.service.EmpService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController // @RequestMapping和@Controller的合成注解 public class EmpController { @Autowired EmpService empService; @GetMapping("/emp/{id}") // 获取get请求的id参数 public Employee getEmp(@PathVariable("id")Integer id){//从路径变量中取出id占位符的值 Employee emp = empService.getEmp(id); return emp; } }
测试类如下:
package com.ning.cache; import com.ning.cache.bean.Employee; import com.ning.cache.mapper.EmployeeMapper; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class Springboot01CacheApplicationTests { @Autowired EmployeeMapper employeeMapper; @Test public void contextLoads() { Employee emp = employeeMapper.getEmpById(1); System.out.println("emp = " + emp); } }
备注:项目中注入
@Autowired
EmployeeMapper employeeMapper;
的时候报没有这个bean这是可以正常运行的,这是因为项目没有检测到这个bean的原因.
项目结构如图:
标签:control ide 缓存 prope 文件 arc ast bind view
原文地址:https://www.cnblogs.com/shiyun32/p/9737379.html