springboot集成jpa,在postgresql數據庫中建立主鍵自增表

依賴文件javascript

<?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.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
		<relativePath/>
	</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>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.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.postgresql</groupId>
			<artifactId>postgresql</artifactId>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

  

配置文件java

 1 spring:  2  datasource:  3     url: jdbc:postgresql://127.0.0.1:5432/postgres
 4  username: postgres  5  password: postgres  6  driverClassName: org.postgresql.Driver  7     initialization-mode: always  8  jpa:  9     show-sql: true
10  hibernate: 11       ddl-auto: update 12  properties: 13  hibernate: 14  temp: 15           use_jdbc_metadata_defaults: false

 

實體類web

package com.example.demo; import javax.persistence.*; import java.io.Serializable; /** * @Author: * @Description: * @Date Create in 2018/11/29 18:12 * @Modified By: */ @Entity @Table(name="test_pg_wushan") public class TestDomain implements Serializable { private static final long serialVersionUID = 2672553622864930471L; @Id @GeneratedValue(strategy= GenerationType.SEQUENCE,generator="s_test_pg_wushan") @SequenceGenerator(sequenceName="test_sequence", name="s_test_pg_wushan",allocationSize = 1) @Column(name="test_id") private Integer id; @Column(name="test_name") private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

 

repository類spring

 1 package com.example.demo;  2 
 3 import org.springframework.data.jpa.repository.JpaRepository;  4 import org.springframework.stereotype.Service;  5 
 6 /**
 7  * @Author:  8  * @Description:  9  * @Date Create in 2018/11/29 18:14 10  * @Modified By: 11  */
12 @Service 13 public interface TestRepository extends JpaRepository<TestDomain, Long> { 14 }

 

controller類sql

package com.example.demo; 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; /** * @Author: * @Description: * @Date Create in 2018/11/29 18:13 * @Modified By: */ @Controller public class TestController { @Autowired TestRepository testRepository ; @RequestMapping("/") @ResponseBody public String test1(){ return "hello"; } @RequestMapping("/save") @ResponseBody public String test2(){ TestDomain te = new TestDomain(); te.setName(System.currentTimeMillis()+""); testRepository.save(te); testRepository.flush(); return "success"; } }

備註:apache

建立表後,查看建立sql語句,發現主鍵並無設置爲序列自增。可是使用controller類操做存儲元素,是能夠在不指定主鍵值的狀況下存儲的。app

相關文章
相關標籤/搜索