Pro JPA2 第十一章(高级主题)-缓存-干货
额,这一章介绍下Spring-Data-JPA中EhCache的使用:
目录结构:
pom.xml
cache的pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>spring-data</artifactId> <groupId>spring-data</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cache</artifactId> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.36</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.15</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
spring-data的pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>spring-data</groupId> <artifactId>spring-data</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>jpa</module> <module>specification</module> <module>rest</module> <module>cache</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.M3</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <querydsl.version>4.1.0</querydsl.version> </properties> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <exclusions> <exclusion> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>org.springframework.plugin</groupId> <artifactId>spring-plugin-core</artifactId> <version>1.2.0.RELEASE</version> </dependency> </dependencies> <build> <plugins> <!-- 热部署 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>${spring-loaded.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> <repositories> <repository> <id>spring-libs-snapshot</id> <url>https://repo.spring.io/libs-snapshot</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-libs-snapshot</id> <url>https://repo.spring.io/libs-snapshot</url> </pluginRepository> </pluginRepositories> </project>
Person实体类:
package cache.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; /** * Created by barton on 16-6-3. */ @Entity public class Person { @Id @GeneratedValue private Long id; private String name; private Integer age; private String address; public Person() { super(); } public Person(Long id, String name, Integer age, String address) { super(); this.id = id; this.name = name; this.age = age; this.address = address; } public Long getId() { return id; } public void setId(Long 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; } }
repository:
package cache.repository; import cache.entity.Person; import org.springframework.data.jpa.repository.JpaRepository; /** * Created by barton on 16-6-4. */ public interface PersonRepository extends JpaRepository<Person,Long> { }
service
package cache.service; import cache.entity.Person; /** * Created by barton on 16-6-4. */ public interface DemoService { Person save(Person person); void remove(Long id); Person findOne(Person person); }
service implements
package cache.service.impl; import cache.entity.Person; import cache.repository.PersonRepository; import cache.service.DemoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; /** * Created by barton on 16-6-4. */ @Service public class DemoServiceImpl implements DemoService { @Autowired private PersonRepository repository; @Override @CachePut(value = "people", key = "#person.id") public Person save(Person person) { Person p = this.repository.save(person); System.out.println("为id,key为:" + p.getId() + "数据做了缓存"); return p; } @Override @CacheEvict(value = "people") public void remove(Long id) { System.out.println("删除了id,key为:" + id + "的数据缓存"); this.repository.delete(id); } @Override @Cacheable(value = "people", key = "#person.id") public Person findOne(Person person) { Person person1 = this.repository.findOne(person.getId()); System.out.println("为id,key为:" + person1.getId() + "数据做了缓存"); return person1; } }
run
package cache; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; /** * Created by barton on 16-6-3. */ @SpringBootApplication @EnableCaching // 开启缓存支持 public class CacheApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(CacheApplication.class, args); } @Override public void run(String... args) throws Exception { } }
application.yml
spring: cache: cache-names: people jpa: hibernate: ddl-auto: update show-sql: true datasource: url: jdbc:mysql://localhost:3306/cache username: root password: root driver-class-name: com.mysql.jdbc.Driver # Number of ms to wait before throwing an exception if no connection is available. max-wait: 10000 # Maximum number of active connections that can be allocated from this pool at the same time. max-active: 50 # Validate the connection before borrowing it from the pool. test-on-borrow: true