1. Create a Spring boot project
2. Save Wait Build complete
3, add SPRING-BOOT-STARTER-DATA-JPA, Memory database dependency package Hsqldb
<!--Add Data JPA dependency-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> < dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <scope>runtime</scope> </dependency>
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>com.kevin</groupId> <artifactId>boot</artifactId> <vers ion>0.0.1-snapshot</version> <packaging>jar</packaging> <name>boot</name> <de Scription>demo Project forSpring Boot</description> <!--Spring-boot-starter-inherit from the parent project to obtain a reasonable default value. The parent project provides the following features: Java1. 8 as the default compiler level. UTF-8 Source code. A dependency Management section that initiates dependencies from spring to inherit the Pom, managing a publicly dependent version. This dependency management allows you to ignore these dependencies when you use them in your own pom<version>tags. Smart resource filtering. Smart plugin configuration (exec plugin, git commit ID and shadow). Smart resource filtering application.properties and application.yml include profile-specific files such as application-dev.properties and Application-dev.yml)-<parent> <GROUPID>ORG.SPRINGFRAMEWORK.BOOT</GROUPID&G T <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> <PR Oject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties& Gt <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Add Data JPA dependency--& Gt <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring- BoOt-starter-data-jpa</artifactid> </dependency> <dependency> <groupid>org. Hsqldb</groupid> <artifactId>hsqldb</artifactId> <scope>runtime</scope> ; </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </DEP endency> </dependencies> <build> <plugins> <plugin> < Groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifact id> </plugin> </plugins> </build></project>
4. Project Structure Chart
5. Related Documents
Applicationcontext.properties
Spring.jpa.hibernate.ddl-auto = Create-drop
Entity class User.java
PackageCom.kevin.boot.model;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;Importjavax.persistence.Id;Importjava.io.Serializable;/** * @authorPingan.yang * @program SPRINGBOOTJPA * @description * @create 2018-05-29 19:10*/@Entity Public classUserImplementsSerializable {@Id @GeneratedValuePrivate intID; @Column (Nullable=false) PrivateString name; @Column (Nullable=false) Private intAge ; PublicUser () {} PublicUser (String name,intAge ) { This. Name =name; This. Age =Age ; } Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; }}
Database underlying operations class Userrepository.java
Package com.kevin.boot.repository; Import Com.kevin.boot.model.User; Import org.springframework.data.jpa.repository.JpaRepository; Import java.util.List; Public Interface extends Jparepository<user, long> { User Getuserbyid (int ID); List<User> getuserbyname (String name);}
User Business Center Control class
PackageCom.kevin.boot.controller;ImportCom.kevin.boot.model.User;Importcom.kevin.boot.repository.UserRepository;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.bind.annotation.RestController;Importjava.util.List;/** * @authorKevin.yang * @program SPRINGBOOTJPA * @description * @create 2018-05-29 19:11*///Restcontroller = Controller + responsebody@RestController Public classUsercontroller {@AutowiredPrivateuserrepository userrepository; @RequestMapping ("/saveuser") PublicString Saveuser (@RequestParam ("name") string name, @RequestParam ("age")intAge ) { Try{User User=NewUser (name, age); Userrepository.save (user); } Catch(Exception e) {e.printstacktrace (); return"Error"; } return"Save Success"; } @RequestMapping ("/getuserbyid") PublicUser Getuserbyid (@RequestParam ("id")intID) {returnUserrepository.getuserbyid (ID); } @RequestMapping ("/getalluser") PublicList<user>Getalluser () {returnUserrepository.findall (); }}
6. Run the test
Run the spring boot startup class Bootapplication.java
Enter the test address in the browser
1>http://127.0.0.1:8080/saveuser?name=kevin.yang&age=1
2>http://127.0.0.1:8080/getalluser
Test results
1> Test Add user Information
2> Test Query User Information
Spring Boot integrated SPRING-BOOT-STARTER-DATA-JPA environment setup