(PNS) Spring Boot integrated Ehcache implementation caching mechanism "Learn Spring boot from scratch"

Source: Internet
Author: User
Tags xmlns mysql database

If you read my previous (35) article this article will be very simple, no challenge.

So let's first talk about this article we will learn the technical point: Spring Data jpa,spring Boot using mysql,spring mvc,ehcache,spring cache, etc. (where @cacheable see a section of theoretical knowledge), Here are a few steps:

(1) New maven Java Project

(2) Adding a dependency package to the Pom.xml

(3) Write spring boot boot class;

(4) configuration application.properties;

(5) Write the cache configuration class and the Ehcache.xml configuration file;

(6) Writing Demoinfo entity class for testing;

(7) Writing persistent class demoinforepository;

(8) Write the processing class Demoinfoservice;

(9) Writing Demoinfocontroller test class;

(10) Run the test;

The above is the specific steps, then we follow this step together to achieve it.

(1) New maven Java Project

Create a new Maven Java project named Spring-boot-ehcache.

(2) Adding a dependency package to the Pom.xml

Include the corresponding dependency package in the Pom.xml file, Spring Boot parent node dependent package, Spring boot Web support, cache dependent Spring-context-support, integration Ehcache required dependencies, JPA operations database, MySQL Database-driven, specific Pom.xml files:

<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>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>

<!--Configure the JDK compilation version. -

<java.version>1.8</java.version>

</properties>

<!--Spring Boot parent node dependency,

After introducing this, the relevant introduction does not need to add the version configuration,

Spring boot will automatically select the most appropriate version to add.

-

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.3.3.RELEASE</version>

</parent>

<dependencies>

<!--unit tests. -

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<scope>test</scope>

</dependency>

<!--Spring Boot Web support: Mvc,aop ...-

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!--

Includes a Support UI template (Velocity,freemarker,jasperreports),

Mail Service,

Scripting Services (JRuby),

Caching cache (EHCache),

Task schedule scheduling (Uartz).

-

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

</dependency>

<!--integrated Ehcache needs to be dependent--

<dependency>

<groupId>net.sf.ehcache</groupId>

<artifactId>ehcache</artifactId>

</dependency>

<!--JPA Operations database. -

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<!--MySQL database driver. -

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

<!--Spring Boot unit test. -

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

</project>

(3) Write spring Boot boot class (Com.kfit.App.java);

Package com.kfit;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

*

*

* @SpringBootApplication affirm that spring boot automatically makes the necessary configuration for the program,

*

@SpringBootApplication

Waiting on:

@Configuration

@EnableAutoConfiguration

@ComponentScan

*

* @author Angel (qq:412887952)

* @version v.0.1

*/

@SpringBootApplication

Public class App {

Public static void main (string[] args) {

Springapplication.run (App.class, args);

}

}

(4) configuration application.properties;

The main configuration of database connections and JPA basic configuration in Application.properties is as follows:

Src/main/resouces/application.properties:

########################################################

# # #datasource, MySQL database connection configuration

########################################################

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 auto-build table operation configuration

########################################################

# Specify the DBMS

Spring.jpa.database = MYSQL

# Show or not log for each SQL query

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 to the Entity manager)

Spring.jpa.properties.hibernate.dialect = Org.hibernate.dialect.MySQL5Dialec

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.