- JPA Dependency
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
- Domain class
Package com.hikvison.test.pgtest.entity;
Import java.io.Serializable;
Import javax.persistence.*;
Import lombok.Data;
/**
* Test some performance of the pg database
* 1. Primary key increment
* 2. High concurrent lock mechanism
*
* @dateAugust 28, 2018 7:23:17 PM
*/
@Data
@Entity
@Table(name="test_pg_wushan")
Public class TestEntity implements Serializable {
Private static final long serialVersionUID = 2672553622864930471L;
@Id
@SequenceGenerator(sequenceName="test_sequence", name="abc" )
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="abc")
@Column(name="id")
Private Integer id;
@Column(name="test_name")
Private String name;
@Transient
Private Integer version;
}
- Repository class
package com.hikvison.test.pgtest.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.hikvison.test.pgtest.entity.TestEntity;
public interface TestRepository extends JpaRepository<TestEntity, Long> {
}
- Controller class
Package com.hikvison.test.pgtest.controller;
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 com.hikvison.test.pgtest.entity.TestEntity;
Import com.hikvison.test.pgtest.repository.TestRepository;
/**
* Test contro
*
* @date August 28, 2018 7:35:25 PM
*/
@Controller
Public class TeatController {
@Autowired
TestRepository r ;
@RequestMapping("/")
@ResponseBody
Public String test1(){
Return "hello";
}
@RequestMapping("/save")
@ResponseBody
Public String test2(){
TestEntity te = new TestEntity();
te.setName(System.currentTimeMillis()+"");
R.save(te);
Return "success";
}
}
Add database link information, database driver, Spring boot dependency, start running. Implement primary key self-increment parsing:
- In the domain class, you use the
@Id
@SequenceGenerator(sequenceName="test_sequence", name="abc" )
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="abc")
@Column(name="id")
private Integer id;
which
1) test_sequence: The sequence name in the database, if it does not exist, will be created with an initial value of 1 and a step of 1 (PostgreSQL and Oracle, the dependent sequence implements the self-increment of the primary key)
2) @SequenceGenerator, note the use of this annotation to declare a sequence.
There is a problem:
I tested the table primary key number starting with 50, not figuring out why.
Using JPA to create a primary key increment table in the PostgreSQL database