Springboot--Ehcache-jpa

package com.bjsxt.dao;import org.springframework.data.jpa.repository.JpaRepository;import com.bjsxt.pojo.Users;
/*** 参数一 T :当前需要映射的实体* 参数二 ID :当前映射的实体中的OID的类型**/
public interface UsersRepository extends JpaRepository<Users,Integer>{}

UserRepository

package com.bjsxt.pojo;import java.io.Serializable;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name="t_users")
public class Users implements Serializable {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)@Column(name="id")private Integer id;@Column(name="name")private String name;@Column(name="age")private Integer age;@Column(name="address")private String address;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "Users [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";}
}

Users

package com.bjsxt.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;import com.bjsxt.dao.UsersRepository;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
/*** UsersService接口实现类***/
@Service
public class UsersServiceImpl implements UsersService {@Autowiredprivate UsersRepository usersRepository;@Override@Cacheable(value="users")public List<Users> findUserAll() {return this.usersRepository.findAll();}@Override//@Cacheable:对当前查询的对象做缓存处理@Cacheable(value="users")public Users findUserById(Integer id) {return this.usersRepository.findOne(id);}@Override@Cacheable(value="users",key="#pageable.pageSize")public Page<Users> findUserByPage(Pageable pageable) {return this.usersRepository.findAll(pageable);}@Override//@CacheEvict(value="users",allEntries=true) 清除缓存中以users缓存策略缓存的对象@CacheEvict(value="users",allEntries=true)public void saveUsers(Users users) {this.usersRepository.save(users);}}

UsersServiceImpl

UserService

package com.bjsxt;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}
}

App

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/orcl
spring.datasource.username=root
spring.datasource.password=123456spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=truespring.cache.ehcache.cofnig=ehcache.xml

application.properties

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"><diskStore path="java.io.tmpdir"/><!--defaultCache:echcache的默认缓存策略  --><defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"maxElementsOnDisk="10000000"diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"><persistence strategy="localTempSwap"/></defaultCache><!-- 自定义缓存策略 --><cache name="users"maxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"maxElementsOnDisk="10000000"diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"><persistence strategy="localTempSwap"/></cache>
</ehcache>

ehcache.xml

package com.bjsxt.test;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.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.bjsxt.App;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;/*** UsersService测试***/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=App.class)
public class UsersServiceTest {@Autowiredprivate UsersService usersService;@Testpublic void testFindUserById(){//第一次查询System.out.println(this.usersService.findUserById(1));//第二次查询System.out.println(this.usersService.findUserById(1));}@Testpublic void testFindUserByPage(){Pageable pageable = new PageRequest(0, 2);//第一次查询System.out.println(this.usersService.findUserByPage(pageable).getTotalElements());//第二次查询System.out.println(this.usersService.findUserByPage(pageable).getTotalElements());//第三次查询pageable = new PageRequest(1, 2);System.out.println(this.usersService.findUserByPage(pageable).getTotalElements());}@Testpublic void testFindAll(){//第一次查询System.out.println(this.usersService.findUserAll().size());Users users = new Users();users.setAddress("南京");users.setAge(43);users.setName("朱七");this.usersService.saveUsers(users);//第二次查询System.out.println(this.usersService.findUserAll().size());}
}

UsersServiceTest

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.10.RELEASE</version></parent><groupId>com.bjsxt</groupId><artifactId>23-spring-boot-ehcache</artifactId><version>0.0.1-SNAPSHOT</version><properties><java.version>1.7</java.version><thymeleaf.version>3.0.2.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version></properties><dependencies><!-- springBoot的启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- springBoot的启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- springBoot的启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- 测试工具的启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- druid连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.9</version></dependency><!-- Spring Boot缓存支持启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><!-- Ehcache坐标 --><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId></dependency></dependencies>
</project>

pom.xml

测试一

执行了一条sql ,说明第二次从ehcache里面拿到的,

Hibernate: select users0_.id as id1_0_0_, users0_.address as address2_0_0_, users0_.age as age3_0_0_, users0_.name as name4_0_0_ from t_users users0_ where users0_.id=?
Users [id=1, name=hello, age=34, address=shanghai]
Users [id=1, name=hello, age=34, address=shanghai]

测试二

* 如果是 @Cacheable(value="users",key="#pageable.pageSize")
* 第一次查询 有sql
* 二 没有从缓存取
* 三 没有从缓存取
* @Cacheable(value="users")
* 第一次查询 有sql
* 二 没有从缓存取
* 三 有sql
* 总结就是 ehcahce 缓存是默认把 key 为pageable 存储
* 如果下一次查询和pageable对象相同则 从缓存中取出

测试三

@CacheEvict(value="users",allEntries=true) 清除缓存中以users缓存策略缓存的对象

CacheEvict 用法

当添加数据时,查询数据错误问题,是因为查询的是缓存的数据,并没有查询添加的数据

添加上面注解后,第一次查询和第二次查询都是从数据库里查询的

转载于:https://www.cnblogs.com/ou-pc/p/9782619.html

Springboot--Ehcache-Jpa (1)相关推荐

  1. SpringBoot集成JPA(2)

    一.快速入门 1.首先就是导入依赖咯,没啥好说的,还有常见的一些依赖没导.web环境之类的,不过你既然都来到这了,我也就不用说了. <!--jpa--><dependency> ...

  2. SpringBoot详解(一)-快速入门

    SpringBoot详解系列文章: SpringBoot详解(一)-快速入门 SpringBoot详解(二)-Spring Boot的核心 SpringBoot详解(三)-Spring Boot的we ...

  3. SpringBoot缓存管理(二) 整合Redis缓存实现

    SpringBoot支持的缓存组件 觅波小说网 https://www.3812.info 在SpringBoot中,数据的缓存管理存储依赖于Spring框架中cache相关的org.springfr ...

  4. springboot 入门教程(4)--web开发(spring mvc和Thymeleaf模板,带源码)

    2019独角兽企业重金招聘Python工程师标准>>> 首先回顾下前几篇的内容:springboot 入门教程(1),springboot 入门教程-Thymeleaf(2), sp ...

  5. SpringBoot学习笔记(3):静态资源处理

    SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...

  6. Springboot整合redis(lettuce)

    springboot 整合redis(lettuce) 首先确保电脑上装了redis.最好能用redisDesktop查看一下数据情况 redis是一款非常流行的Nosql数据库.redis的功能非常 ...

  7. springboot学习笔记(五)

    一丶注值方式 1.在application.properties文件中注值 首先我们将application.yml中的学生名字和年龄给注释掉,来验证在applic.properties的注值方式. ...

  8. JPA(三)之实体关系一对多(多对一)

     1.背景介绍: 对于购买商品时,订单信息(Order)和订单商品信息(OrderItem)的关系就是一对多的关系. 2.实体bean: Order.java代码 ? 1 2 3 4 5 6 7 ...

  9. springboot+security整合(1)

    说明 springboot 版本 2.0.3 源码地址:点击跳转 系列 springboot+security 整合(1) springboot+security 整合(2) springboot+s ...

  10. SpringBoot学习笔记(16):单元测试

    SpringBoot学习笔记(16):单元测试 单元测试 单元测试(英语:Unit Testing)又称为模块测试,是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小 ...

最新文章

  1. 字符串匹配的KMP算法——Python实现
  2. 数据库-数据库设计原则-范式概念
  3. vista下文件夹拒绝访问的解决办法
  4. springboot 整合mybatis_SpringBoot整合Mybatis、MybatisPuls
  5. 错误类型、混淆矩阵及目标检测常用评价指标
  6. Lua 的table遍历 【转】
  7. 面试题 01.09. 字符串轮转
  8. python获取时间戳毫秒级_Python获取秒级时间戳与毫秒级时间戳
  9. JavaScript在发送请求AJAX请求时,URL的域名地址是使用绝对地址还是相对地址?
  10. 阶段3 2.Spring_08.面向切面编程 AOP_6 四种常用通知类型
  11. everything的安装后初始设置
  12. 形式化验证学习——什么是形式化?Formal
  13. 利用matlab来进行路径规划,matlab路径规划系列
  14. Java、JSP新华书店网上售书系统
  15. SpringCloud七:配置中心Eureka+Config+Bus+RabbitMQ
  16. 软件调试是鸡肋?你的认知决定你的层次!
  17. SSD-主控、闪存和固件(转)
  18. MATLAB 错误使用 instfreq TFD 应“非负”
  19. Win10新添实用功能,你发现了吗?
  20. 【电脑】你了解电脑吗?

热门文章

  1. 拓端tecdat|matlab脉冲响应图的时域特征
  2. Path of Equal Weight (30 分)
  3. m1芯片 服务器,今天来说说苹果M1 和华为ARM PC芯片
  4. 全卷积网络 FCN 详解
  5. windows7下redis的安装实践
  6. python基于OpenCV模块实现视频流数据切割为图像帧数据
  7. MySQL中 char和varchar的区别
  8. java 注解开发 解耦_Android java 解耦框架注解Dagger2
  9. PHP脚本调用systemctl,centos7之systemctl
  10. python mssql get image bin_python 使用pymssql连接sql server数据库