From 995454981da9e4b81600f5d4ed5166c46a36bc67 Mon Sep 17 00:00:00 2001
From: mrbird <852252810@qq.com>
Date: Wed, 2 May 2018 15:31:41 +0800
Subject: [PATCH] =?UTF-8?q?Spring=20Boot=E4=B8=AD=E4=BD=BF=E7=94=A8Ehcache?=
=?UTF-8?q?=E7=BC=93=E5=AD=98=E6=95=B0=E6=8D=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
10.Spring-Boot-Ehcache-Cache/pom.xml | 81 +++++++++++++++++++
.../main/java/com/springboot/Application.java | 15 ++++
.../java/com/springboot/ApplicationTest.java | 42 ++++++++++
.../java/com/springboot/bean/Student.java | 31 +++++++
.../com/springboot/mapper/StudentMapper.java | 26 ++++++
.../springboot/service/StudentService.java | 20 +++++
.../service/impl/StudentServiceImpl.java | 32 ++++++++
.../src/main/resources/application.yml | 74 +++++++++++++++++
.../src/main/resources/ehcache.xml | 22 +++++
.../src/main/resources/init.sql | 8 ++
10 files changed, 351 insertions(+)
create mode 100644 10.Spring-Boot-Ehcache-Cache/pom.xml
create mode 100644 10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/Application.java
create mode 100644 10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/ApplicationTest.java
create mode 100644 10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/bean/Student.java
create mode 100644 10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/mapper/StudentMapper.java
create mode 100644 10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/service/StudentService.java
create mode 100644 10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/service/impl/StudentServiceImpl.java
create mode 100644 10.Spring-Boot-Ehcache-Cache/src/main/resources/application.yml
create mode 100644 10.Spring-Boot-Ehcache-Cache/src/main/resources/ehcache.xml
create mode 100644 10.Spring-Boot-Ehcache-Cache/src/main/resources/init.sql
diff --git a/10.Spring-Boot-Ehcache-Cache/pom.xml b/10.Spring-Boot-Ehcache-Cache/pom.xml
new file mode 100644
index 0000000..3e2fe27
--- /dev/null
+++ b/10.Spring-Boot-Ehcache-Cache/pom.xml
@@ -0,0 +1,81 @@
+
+
+ 4.0.0
+
+ com.springboot
+ Spring-Boot-Ehcache-Cache
+ 0.0.1-SNAPSHOT
+ jar
+
+ demo
+ Demo project for Spring Boot
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 1.5.9.RELEASE
+
+
+
+
+ UTF-8
+ UTF-8
+ 1.7
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+
+ org.mybatis.spring.boot
+ mybatis-spring-boot-starter
+ 1.3.1
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+ org.springframework.boot
+ spring-boot-starter-cache
+
+
+
+
+ net.sf.ehcache
+ ehcache
+
+
+
+
+ com.oracle
+ ojdbc6
+ 6.0
+
+
+ com.alibaba
+ druid-spring-boot-starter
+ 1.1.6
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
+
diff --git a/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/Application.java b/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/Application.java
new file mode 100644
index 0000000..ccfc0ab
--- /dev/null
+++ b/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/Application.java
@@ -0,0 +1,15 @@
+package com.springboot;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cache.annotation.EnableCaching;
+
+
+@SpringBootApplication
+@EnableCaching
+public class Application {
+
+ public static void main(String[] args) {
+ SpringApplication.run(Application.class,args);
+ }
+}
diff --git a/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/ApplicationTest.java b/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/ApplicationTest.java
new file mode 100644
index 0000000..0e5efa7
--- /dev/null
+++ b/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/ApplicationTest.java
@@ -0,0 +1,42 @@
+package com.springboot;
+
+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.cache.CacheManager;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import com.springboot.bean.Student;
+import com.springboot.service.StudentService;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest(classes = Application.class)
+public class ApplicationTest {
+
+ @Autowired
+ private StudentService studentService;
+
+
+ @Test
+ public void test1() throws Exception {
+ Student student1 = this.studentService.queryStudentBySno("001");
+ System.out.println("学号" + student1.getSno() + "的学生姓名为:" + student1.getName());
+
+ Student student2 = this.studentService.queryStudentBySno("001");
+ System.out.println("学号" + student2.getSno() + "的学生姓名为:" + student2.getName());
+ }
+
+ @Test
+ public void test2() throws Exception {
+
+ Student student1 = this.studentService.queryStudentBySno("001");
+ System.out.println("学号" + student1.getSno() + "的学生姓名为:" + student1.getName());
+
+ student1.setName("康康");
+ this.studentService.update(student1);
+
+ Student student2 = this.studentService.queryStudentBySno("001");
+ System.out.println("学号" + student2.getSno() + "的学生姓名为:" + student2.getName());
+ }
+}
diff --git a/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/bean/Student.java b/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/bean/Student.java
new file mode 100644
index 0000000..0ff713e
--- /dev/null
+++ b/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/bean/Student.java
@@ -0,0 +1,31 @@
+package com.springboot.bean;
+
+import java.io.Serializable;
+
+public class Student implements Serializable{
+
+ private static final long serialVersionUID = -339516038496531943L;
+ private String sno;
+ private String name;
+ private String sex;
+ public String getSno() {
+ return sno;
+ }
+ public void setSno(String sno) {
+ this.sno = sno;
+ }
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ public String getSex() {
+ return sex;
+ }
+ public void setSex(String sex) {
+ this.sex = sex;
+ }
+
+
+}
diff --git a/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/mapper/StudentMapper.java b/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/mapper/StudentMapper.java
new file mode 100644
index 0000000..d0e0303
--- /dev/null
+++ b/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/mapper/StudentMapper.java
@@ -0,0 +1,26 @@
+package com.springboot.mapper;
+
+import org.apache.ibatis.annotations.Delete;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Result;
+import org.apache.ibatis.annotations.Results;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
+
+import com.springboot.bean.Student;
+
+@Mapper
+public interface StudentMapper {
+
+ @Update("update student set sname=#{name},ssex=#{sex} where sno=#{sno}")
+ int update(Student student);
+
+ @Delete("delete from student where sno=#{sno}")
+ void deleteStudentBySno(String sno);
+
+ @Select("select * from student where sno=#{sno}")
+ @Results(id = "student", value = { @Result(property = "sno", column = "sno", javaType = String.class),
+ @Result(property = "name", column = "sname", javaType = String.class),
+ @Result(property = "sex", column = "ssex", javaType = String.class) })
+ Student queryStudentBySno(String sno);
+}
diff --git a/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/service/StudentService.java b/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/service/StudentService.java
new file mode 100644
index 0000000..cb35c4f
--- /dev/null
+++ b/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/service/StudentService.java
@@ -0,0 +1,20 @@
+package com.springboot.service;
+
+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 com.springboot.bean.Student;
+
+@CacheConfig(cacheNames = "student")
+public interface StudentService {
+ @CachePut(key = "#p0.sno")
+ Student update(Student student);
+
+ @CacheEvict(key = "#p0", allEntries = true)
+ void deleteStudentBySno(String sno);
+
+ @Cacheable(key = "#p0")
+ Student queryStudentBySno(String sno);
+}
diff --git a/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/service/impl/StudentServiceImpl.java b/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/service/impl/StudentServiceImpl.java
new file mode 100644
index 0000000..804962a
--- /dev/null
+++ b/10.Spring-Boot-Ehcache-Cache/src/main/java/com/springboot/service/impl/StudentServiceImpl.java
@@ -0,0 +1,32 @@
+package com.springboot.service.impl;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Repository;
+
+import com.springboot.bean.Student;
+import com.springboot.mapper.StudentMapper;
+import com.springboot.service.StudentService;
+
+@Repository("studentService")
+public class StudentServiceImpl implements StudentService{
+
+ @Autowired
+ private StudentMapper studentMapper;
+
+ @Override
+ public Student update(Student student) {
+ this.studentMapper.update(student);
+ return this.studentMapper.queryStudentBySno(student.getSno());
+ }
+
+ @Override
+ public void deleteStudentBySno(String sno) {
+ this.studentMapper.deleteStudentBySno(sno);
+ }
+
+ @Override
+ public Student queryStudentBySno(String sno) {
+ return this.studentMapper.queryStudentBySno(sno);
+ }
+
+}
diff --git a/10.Spring-Boot-Ehcache-Cache/src/main/resources/application.yml b/10.Spring-Boot-Ehcache-Cache/src/main/resources/application.yml
new file mode 100644
index 0000000..ea78b2c
--- /dev/null
+++ b/10.Spring-Boot-Ehcache-Cache/src/main/resources/application.yml
@@ -0,0 +1,74 @@
+server:
+ context-path: /web
+
+spring:
+ datasource:
+ druid:
+ # 数据库访问配置, 使用druid数据源
+ type: com.alibaba.druid.pool.DruidDataSource
+ driver-class-name: oracle.jdbc.driver.OracleDriver
+ url: jdbc:oracle:thin:@localhost:1521:ORCL
+ username: test
+ password: 123456
+ # 连接池配置
+ initial-size: 5
+ min-idle: 5
+ max-active: 20
+ # 连接等待超时时间
+ max-wait: 30000
+ # 配置检测可以关闭的空闲连接间隔时间
+ time-between-eviction-runs-millis: 60000
+ # 配置连接在池中的最小生存时间
+ min-evictable-idle-time-millis: 300000
+ validation-query: select '1' from dual
+ test-while-idle: true
+ test-on-borrow: false
+ test-on-return: false
+ # 打开PSCache,并且指定每个连接上PSCache的大小
+ pool-prepared-statements: true
+ max-open-prepared-statements: 20
+ max-pool-prepared-statement-per-connection-size: 20
+ # 配置监控统计拦截的filters, 去掉后监控界面sql无法统计, 'wall'用于防火墙
+ filters: stat,wall
+ # Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔
+ aop-patterns: com.springboot.servie.*
+
+
+ # WebStatFilter配置
+ web-stat-filter:
+ enabled: true
+ # 添加过滤规则
+ url-pattern: /*
+ # 忽略过滤的格式
+ exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
+
+ # StatViewServlet配置
+ stat-view-servlet:
+ enabled: true
+ # 访问路径为/druid时,跳转到StatViewServlet
+ url-pattern: /druid/*
+ # 是否能够重置数据
+ reset-enable: false
+ # 需要账号密码才能访问控制台
+ login-username: druid
+ login-password: druid123
+ # IP白名单
+ # allow: 127.0.0.1
+ # IP黑名单(共同存在时,deny优先于allow)
+ # deny: 192.168.1.218
+
+ # 配置StatFilter
+ filter:
+ stat:
+ log-slow-sql: true
+
+ cache:
+ ehcache:
+ config: 'classpath:ehcache.xml'
+
+logging:
+ level:
+ com:
+ springboot:
+ mapper: debug
+
\ No newline at end of file
diff --git a/10.Spring-Boot-Ehcache-Cache/src/main/resources/ehcache.xml b/10.Spring-Boot-Ehcache-Cache/src/main/resources/ehcache.xml
new file mode 100644
index 0000000..97cf335
--- /dev/null
+++ b/10.Spring-Boot-Ehcache-Cache/src/main/resources/ehcache.xml
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/10.Spring-Boot-Ehcache-Cache/src/main/resources/init.sql b/10.Spring-Boot-Ehcache-Cache/src/main/resources/init.sql
new file mode 100644
index 0000000..15bfe6b
--- /dev/null
+++ b/10.Spring-Boot-Ehcache-Cache/src/main/resources/init.sql
@@ -0,0 +1,8 @@
+CREATE TABLE STUDENT (
+ SNO VARCHAR2(3 BYTE) NOT NULL ,
+ SNAME VARCHAR2(9 BYTE) NOT NULL ,
+ SSEX CHAR(2 BYTE) NOT NULL
+);
+INSERT INTO STUDENT VALUES ('001', 'KangKang', 'M ');
+INSERT INTO STUDENT VALUES ('002', 'Mike', 'M ');
+INSERT INTO STUDENT VALUES ('003', 'Jane', 'F ');
\ No newline at end of file