码迷,mamicode.com
首页 > 其他好文 > 详细

1. 缓存

时间:2018-10-02 20:19:44      阅读:166      评论:0      收藏:0      [点我收藏+]

标签: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;
View Code

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 
View Code

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 + "]";
    }
    
    
    
    

}
View Code
技术分享图片
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 + "]";
    }
    
    

}
View Code

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);
}
View Code

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;
    }
}
View Code

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;
    }
}
View Code

测试类如下:

技术分享图片
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);
    }


}
View Code

备注:项目中注入

@Autowired
EmployeeMapper employeeMapper;

 的时候报没有这个bean这是可以正常运行的,这是因为项目没有检测到这个bean的原因.

项目结构如图:

技术分享图片

 

1. 缓存

标签:control   ide   缓存   prope   文件   arc   ast   bind   view   

原文地址:https://www.cnblogs.com/shiyun32/p/9737379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!