如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了。
那么我们先说说这一篇文章我们都会学到的技术点:Spring Data JPA,SpringBoot使用Mysql,Spring MVC,EHCache,Spring Cache等(其中@Cacheable请看上一节的理论知识),具体分如下几个步骤:
(1)新建Maven Java Project
(2)在pom.xml中加入依赖包
(3)编写Spring Boot启动类;
(4)配置application.properties;
(5)编写缓存配置类以及ehcache.xml配置文件;
(6)编写DemoInfo实体类进行测试;
(7)编写持久类DemoInfoRepository;
(8)编写处理类DemoInfoService;
(9)编写DemoInfoController测试类;
(10)运行测试;
以上就是具体的步骤了,那么接下来我们一起按照这个步骤来进行实现吧。
(1)新建Maven Java Project
新建一个工程名为spring-boot-ehcache的maven java project。
(2)在pom.xml中加入依赖包
在pom.xml文件中加入相应的依赖包,SpringBoot父节点依赖包;spring boot web支持;缓存依赖spring-context-support;集成ehcache需要的依赖;JPA操作数据库;mysql 数据库驱动,具体pom.xml文件:
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="" target="_blank">http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"" target="_blank">>
<modelVersion>4.0.0</modelVersion>
<groupId>com.kfit</groupId>
<artifactId>spring-boot-ehcache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-ehcache</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 配置JDK编译版本. -->
<java.version>1.8</java.version>
</properties>
<!-- springboot 父节点依赖,
引入这个之后相关的引入就不需要添加version配置,
spring boot会自动选择最合适的版本进行添加。
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<!-- 单元测试. -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- springboot web支持:mvc,aop... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--
包含支持UI模版(Velocity,FreeMarker,JasperReports),
邮件服务,
脚本服务(JRuby),
缓存Cache(EHCache),
任务计划Scheduling(uartz)。
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- 集成ehcache需要的依赖-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<!-- JPA操作数据库. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql数据库驱动. -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Spring boot单元测试. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(3)编写Spring Boot启动类(com.kfit.App.java);
package com.kfit;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
*
*@SpringBootApplication申明让spring boot自动给程序进行必要的配置,
*
@SpringBootApplication
等待于:
@Configuration
@EnableAutoConfiguration
@ComponentScan
*
*@author Angel(QQ:412887952)
*@version v.0.1
*/
@SpringBootApplication
publicclass App {
publicstaticvoid main(String[]args) {
SpringApplication.run(App.class,args);
}
}
(4)配置application.properties;
在application.properties中主要配置数据库连接和JPA的基本配置,具体如下:
Src/main/resouces/application.properties:
########################################################
###datasource ,mysql数据库连接配置
########################################################
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username= root
spring.datasource.password= root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
########################################################
### Java Persistence Api,JPA自动建表操作配置
########################################################
# Specify the DBMS
spring.jpa.database= MYSQL
# Show or not log for each sqlquery
spring.jpa.show-sql= true
# Hibernate ddl auto (create,create-drop, update)
spring.jpa.hibernate.ddl-auto= update
# Naming strategy
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them tothe entity manager)
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
。。。。。。。。。。。。。。。。。
版权原因,完整文章,请参考如下:37. Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】