From c363372833a5d09836de5234d2d3e08f65e5a40d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=8C=BFDD?= Date: Fri, 17 Jun 2016 12:30:52 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=94=AF=E6=8C=81=EF=BC=9A?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=E9=85=8D=E7=BD=AE=E4=B8=8EEhCache=E4=BD=BF?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chapter4-4-1/pom.xml | 70 +++++++++++++++++++ .../main/java/com/didispace/Application.java | 21 ++++++ .../main/java/com/didispace/domain/User.java | 58 +++++++++++++++ .../com/didispace/domain/UserRepository.java | 22 ++++++ .../src/main/resources/application.properties | 7 ++ Chapter4-4-1/src/main/resources/ehcache.xml | 9 +++ .../java/com/didispace/ApplicationTests.java | 51 ++++++++++++++ 7 files changed, 238 insertions(+) create mode 100644 Chapter4-4-1/pom.xml create mode 100644 Chapter4-4-1/src/main/java/com/didispace/Application.java create mode 100644 Chapter4-4-1/src/main/java/com/didispace/domain/User.java create mode 100644 Chapter4-4-1/src/main/java/com/didispace/domain/UserRepository.java create mode 100644 Chapter4-4-1/src/main/resources/application.properties create mode 100644 Chapter4-4-1/src/main/resources/ehcache.xml create mode 100644 Chapter4-4-1/src/test/java/com/didispace/ApplicationTests.java diff --git a/Chapter4-4-1/pom.xml b/Chapter4-4-1/pom.xml new file mode 100644 index 0000000..a146394 --- /dev/null +++ b/Chapter4-4-1/pom.xml @@ -0,0 +1,70 @@ + + + 4.0.0 + + com.didispace + Chapter4-4-1 + 1.0.0 + jar + + Chapter4-4-1 + Spring Boot project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + mysql + mysql-connector-java + 5.1.21 + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.springframework.boot + spring-boot-starter-cache + + + + net.sf.ehcache + ehcache + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter4-4-1/src/main/java/com/didispace/Application.java b/Chapter4-4-1/src/main/java/com/didispace/Application.java new file mode 100644 index 0000000..cc03394 --- /dev/null +++ b/Chapter4-4-1/src/main/java/com/didispace/Application.java @@ -0,0 +1,21 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/3/23 下午2:34. + * @blog http://blog.didispace.com + */ +@SpringBootApplication +@EnableCaching +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/Chapter4-4-1/src/main/java/com/didispace/domain/User.java b/Chapter4-4-1/src/main/java/com/didispace/domain/User.java new file mode 100644 index 0000000..6afe696 --- /dev/null +++ b/Chapter4-4-1/src/main/java/com/didispace/domain/User.java @@ -0,0 +1,58 @@ +package com.didispace.domain; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/3/21 下午3:35. + * @blog http://blog.didispace.com + */ +@Entity +public class User { + + @Id + @GeneratedValue + private Long id; + + @Column(nullable = false) + private String name; + + @Column(nullable = false) + private Integer age; + + public User(){} + + public User(String name, Integer age) { + this.name = name; + this.age = age; + } + + 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; + } + +} diff --git a/Chapter4-4-1/src/main/java/com/didispace/domain/UserRepository.java b/Chapter4-4-1/src/main/java/com/didispace/domain/UserRepository.java new file mode 100644 index 0000000..61b4466 --- /dev/null +++ b/Chapter4-4-1/src/main/java/com/didispace/domain/UserRepository.java @@ -0,0 +1,22 @@ +package com.didispace.domain; + +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.data.jpa.repository.JpaRepository; + + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/3/23 下午2:34. + * @blog http://blog.didispace.com + */ +@CacheConfig(cacheNames = "users") +public interface UserRepository extends JpaRepository { + + @Cacheable(key = "#p0", condition = "#p0.length() < 10") + User findByName(String name); + +} diff --git a/Chapter4-4-1/src/main/resources/application.properties b/Chapter4-4-1/src/main/resources/application.properties new file mode 100644 index 0000000..6cfe333 --- /dev/null +++ b/Chapter4-4-1/src/main/resources/application.properties @@ -0,0 +1,7 @@ +spring.datasource.url=jdbc:mysql://localhost:3306/test +spring.datasource.username=root +spring.datasource.password=123456 +spring.datasource.driver-class-name=com.mysql.jdbc.Driver + +spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop +spring.jpa.properties.hibernate.show_sql=true \ No newline at end of file diff --git a/Chapter4-4-1/src/main/resources/ehcache.xml b/Chapter4-4-1/src/main/resources/ehcache.xml new file mode 100644 index 0000000..c178cc9 --- /dev/null +++ b/Chapter4-4-1/src/main/resources/ehcache.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/Chapter4-4-1/src/test/java/com/didispace/ApplicationTests.java b/Chapter4-4-1/src/test/java/com/didispace/ApplicationTests.java new file mode 100644 index 0000000..2be6302 --- /dev/null +++ b/Chapter4-4-1/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,51 @@ +package com.didispace; + +import com.didispace.domain.User; +import com.didispace.domain.UserRepository; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.cache.CacheManager; +import org.springframework.cache.ehcache.EhCacheCacheManager; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/3/23 下午2:34. + * @blog http://blog.didispace.com + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(Application.class) +public class ApplicationTests { + + @Autowired + private UserRepository userRepository; + + @Autowired + private CacheManager cacheManager; + + @Before + public void before() { + userRepository.save(new User("AAA", 10)); + } + + @Test + public void test() throws Exception { + + User u1 = userRepository.findByName("AAA"); + System.out.println("第一次查询:" + u1.getAge()); + + User u2 = userRepository.findByName("AAA"); + System.out.println("第二次查询:" + u2.getAge()); + + u1.setAge(20); + userRepository.save(u1); + User u3 = userRepository.findByName("AAA"); + System.out.println("第三次查询:" + u3.getAge()); + + } + +}