Several points of JPA annotations
1. Set Pojo as entity @Entity//Identify this pojo is a JPA entity public class Users implements serializable{}
2. Set table name @[email protected] (name=users)//Refer to the table named users public class users implements serializable{}
3. Setting the primary key public
Several points of JPA annotations
@Entity
1. Set Pojo as Entity
public class Users implements Serializable {
}
2. Set the table name
@Entity
@Table (name = "users")//Specify the table name as users
public class Users implements Serializable {
}
3. Setting the primary key
public class Users implements Serializable {
@Id
Private String Usercode;
4. Setting the field type
With the @column annotation setting, the following settings are included
Name: Field name
. Unique: Is it unique
. Nullable: Whether it is possible to empty
. inserttable: Whether you can insert
. Updateable: Can I update
. ColumnDefinition: Defines the DDL that creates this column when the table is built
. secondarytable: From the table name. If this column is not built on the primary table (which is built by default in the main table), this property defines the name of the column that is located from the table.
1. Create a new MAVEN project
2. Add the necessary dependencies
<?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.xlei</groupId> <artifactid>springboot_jpa</artifactid > <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>springboot_jpa</name> <desc Ription>demo Project forSpring boot</description> <parent> <groupId>org.springframework.boot</groupId> &L T;artifactid>spring-boot-starter-parent</artifactid> <version>1.5.9. Release</version> <relativePath/> <!--lookup Parent fromRepository-</parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceEncoding> <project.reporting.outputencoding>utf-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>o Rg.springframework.boot</groupid> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test</a Rtifactid> <scope>test</scope> </dependency> <!--MySQL Drive-< ;d ependency> <groupId>mysql</groupId> <artifactid>mysql-connector-java</arti factid> </dependency> </dependencies> <build> <plugins> <plug In> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifa ctid> </plugin> </plugins> </build></project>
3. New Springboot Startup class
Package Com.xlei; Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication Public class springbootjpaapplication { publicstaticvoid main (string[] args ) { Springapplication.run (springbootjpaapplication. class , args);} }
4. Create a new application.properties under Resources and directory
#建立/ Update data table configuration Spring.jpa.hibernate.ddl-auto=update# database address Spring.datasource.url=jdbc:mysql:/ /localhost:3306/user#数据库名spring. Datasource.username=root# Database Password Spring.datasource.password=123456
- Update:hibernate changes the database based on the given entity structure.
- Create: The database is created every time and is not deleted when it is closed
- None:mysql default settings, does not change the data structure
- Create-drop: Database is created, but is deleted every time sessionfactory is closed
5. New Entity class user
At this point, you can actually start springboot, but the data table is not generated because the JPA of the entity class has not been configured
PackageCom.xlei.domain;ImportJavax.persistence.*;/*** @Author: Leixiao * @Description: Springcloud * @Date: 2018/1/30*/@Entity//indicate this needs to generate a data table type@Table (name = "Users") Public classUser {@Id//declares a common policy generator with name "System-uuid" and a policy strategy "UUID". @GeneratedValuePrivateInteger ID; @Column (Nullable=false) PrivateString name; @Column (Nullable=false) PrivateInteger age; @Column (Nullable=false) PrivateBoolean sex; PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; } PublicBoolean Getsex () {returnsex; } Public voidsetsex (Boolean sex) { This. Sex =sex; }}
When the project is started, a user data table is generated at the specified location
6. Implementing CRUD
Crudrepository is an interface that provides common additions and deletions, and is provided internally by spring, and we simply call to
New Userrepository.java
Public Interface extends Crudrepository<user, string> {}
7. Implementing controller Control
New Usercontroller.java
PackageCom.xlei.controller;Importcom.xlei.dao.UserRepository;ImportCom.xlei.domain.User;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;/*** @Author: Leixiao * @Description: Springcloud * @Date: 2018/1/30*/@RestController//to include users in the spring container Public classUserscontroller {//Automatic Assembly@AutowiredPrivateuserrepository userrepository; @RequestMapping ("/list") PublicIterable<user>list () {iterable<User> all =Userrepository.findall (); returnAll ; }}
Springboot Integrated JPA