1.JPA introduction
JPA (Java Persistence API) is the Java persistence specification officially proposed by Sun. It provides Java developers with an object/relational mapping tool to manage relational data in Java applications. His appearance is mainly to simplify existing persistence development work and integrate ORM technology, ending the current messy situation in which ORM frameworks such as Hibernate, TopLink, and JDO operate independently. JPA is developed on the basis of fully absorbing existing ORM frameworks such as Hibernate, TopLink, JDO, etc. It has the advantages of ease of use and strong scalability. From the above explanation, we can understand that JPA is a set of specifications, and products such as Hibernate, TopLink, and JDO implement the JPA specifications.
Spring Data JPA is a set of JPA application frameworks encapsulated by Spring based on the ORM framework and JPA specifications. The bottom layer is implemented using Hibernate’s JPA technology, which allows developers to access and operate data with minimalist code. It provides common functions including adding, deleting, modifying, checking, etc., and is easy to expand! Learning and using Spring Data JPA can greatly improve development efficiency.
2.mysql environment setup
Refer to the mysql module in the code repository. Only docker-compose.yml is posted here.
version: '3'
services:
mysql:
image: registry.cn-hangzhou.aliyuncs.com/zhengqing/mysql:5.7
container_name: mysql_3306
restart: unless-stopped
volumes:
- "./mysql/my.cnf:/etc/mysql/my.cnf"
- "./mysql/init-file.sql:/etc/mysql/init-file.sql"
- "./mysql/data:/var/lib/mysql"
# - "./mysql/conf.d:/etc/mysql/conf.d"
- "./mysql/log/mysql/error.log:/var/log/mysql/error.log"
- "./mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d" # init sql script directory -- tips: it can be excute when `/var/lib/mysql` is empty
environment: # set environment,equals docker run -e
TZ: Asia/Shanghai
LANG: en_US.UTF-8
MYSQL_ROOT_PASSWORD: root # set root password
MYSQL_DATABASE: demo # init database name
ports: # port mappping
- "3306:3306"
start the test environment
docker-compose -f docker-compose.yml -p mysql5.7 up -d
3.Code Project
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">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jpa</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
</dependencies>
</project>
entity
package com.et.jpa.entity;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@GeneratedValue(generator = "system-uuid")
private String id;
private String name;
private Integer age;
private Boolean sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
}
here some annotation explain as follows:
- @Entity is a class annotation used to annotate that the class is an entity class and is used to establish an association with a table in the database. When the project is started for the first time, a table with the same name as the entity class (table) will be generated in the data by default. ), you can also modify the table name through the name attribute in the annotation, such as @Entity(name=”user”), so that the name of the table in the database is user. This annotation is very important. If you start the project for the first time without this annotation, you will find that the database does not generate the corresponding table.
- The @Table annotation is also a class annotation. This annotation can be used to modify the name of the table. This annotation can be completely ignored. The @Entity annotation already has the function of this annotation.
- The attribute annotation of the @Id class indicates that the attribute field is a primary key. This attribute must be present and is indispensable.
- @GeneratedValue This annotation is usually used together with the @Id primary key annotation to define the presentation form of the primary key. This annotation usually has a variety of usage strategies, which are summarized as follows:
- @GeneratedValue(strategy= GenerationType.IDENTITY) This annotation is automatically generated by the database. The primary key is auto-incrementing. It is most frequently used in the MySQL database and is not supported by Oracle.
- @GeneratedValue(strategy= GenerationType.AUTO) The primary key is controlled by the program. The default primary key generation strategy is the serialization method by Oracle and the auto-increment method by MySQL.
- @GeneratedValue(strategy= GenerationType.SEQUENCE) generates the primary key based on the sequence of the underlying database. The condition is that the database supports sequence, Oracle supports it, and Mysql does not support it.
- @GeneratedValue(strategy= GenerationType.TABLE) uses a specific database table to save the primary key, rarely used。
application.properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
#Camel case naming automatically converts to underline
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
#use mysql database
spring.jpa.database=mysql
# show sql
spring.jpa.show-sql=true
server.port=8088
Here we briefly introduce the several configurations of spring.jpa.properties.hibernate.hbm2ddl.auto:
create
:It means that each time Hibernate is loaded, the last generated table (including data) will be deleted, and then a new table will be regenerated. This will be done even if there are no modifications twice. Suitable for clearing the database before each execution of a single test.create-drop
:Indicates that the table will be generated every time Hibernate is loaded, but when the SessionFactory is closed, the generated table will be automatically deleted.update
:The most commonly used attribute values, the data table is created when Hibernate is loaded for the first time (the premise is that a database must be available first). The last generated table will not be deleted when Hibernate is loaded later. It will be updated based on the entity. Only new fields will be added and will not be deleted. field (even if it has been deleted from the entity).validate
:Every time Hibernate is loaded, the data table structure will be verified, it will only be compared with the existing data table, and the table structure will be modified according to the model, but no new table will be created.- Not configuring this item means disabling the automatic table creation function.
spring.jpa.show-sql=true This configuration will print sql statements on the console when performing database operations, which is convenient for us to check and troubleshoot.
controller
package com.et.jpa.controller;
import com.et.jpa.entity.User;
import com.et.jpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class HelloWorldController {
@Autowired
private UserRepository userRepository;
@RequestMapping("/hello")
@ResponseBody
public Map<String, Object> showHelloWorld(){
Map<String, Object> map = new HashMap<>();
map.put("msg", "HelloWorld");
return map;
}
@RequestMapping("/add")
public User add(String name){
User user = new User();
user.setName(name);
return userRepository.save(user);
}
@RequestMapping("/list")
public Iterable<User> list(){
Iterable<User> all = userRepository.findAll();
return all;
}
}
repository
package com.et.jpa.repository;
import com.et.jpa.entity.User;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, String> {
}
PagingAndSortingRepository inherits CrudRepository, JpaRepository inherits PagingAndSortingRepository. In other words, CrudRepository provides basic addition, deletion, modification and query; PagingAndSortingRepository provides paging and sorting methods; JpaRepository provides the methods required by JPA.
Code Repository
4.Test
When you start the spring boot application, the table will be automatically created. View the console as follows:
Hibernate: create table user (id varchar(255) not null, age integer, name varchar(255), sex bit, primary key (id)) engine=MyISAM
Input http://127.0.0.1:8088/add?name=hblog2,insert data。console output as follows:
Hibernate: insert into user (age, name, sex, id) values (?, ?, ?, ?)
Input http://127.0.0.1:8088/list,view table data,console output as follows:
Hibernate: select user0_.id as id1_0_, user0_.age as age2_0_, user0_.name as name3_0_, user0_.sex as sex4_0_ from user user0_