1. 程式人生 > >springboot整合hibernate自動建立資料庫表

springboot整合hibernate自動建立資料庫表

新建一個maven專案,我習慣性的建web了,有些依賴是沒必要的,主要用到的依賴有:

                     <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId
>
</dependency>

下面是我的pom.xml檔案:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xy</groupId>
  <artifactId>springboot-hibernate-createtable</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springboot-hibernate-createtable Maven Webapp</name>
  <url>http://maven.apache.org</url>
<parent> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-parent</artifactId> 
<version>1.5.2.RELEASE</version> 
<relativePath /> <!-- lookup parent from repository --> 
</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-data-jpa</artifactId> 
</dependency> 

<dependency> 
<groupId>mysql</groupId> 
<artifactId>mysql-connector-java</artifactId> 
</dependency> 
<dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-web</artifactId> 
</dependency> 

       <!-- jsp標籤庫 -->
        <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>jstl</artifactId>
       </dependency>
       
<dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-test</artifactId> 
<scope>test</scope> 
</dependency> 
</dependencies> 


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

application.priperties檔案:

server.port=8080


# Hibernate \u76F8\u5173\u914D\u7F6E


## \u65B9\u8A00
#hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
## \u663E\u793ASql
hibernate.show_sql=true
## \u81EA\u52A8\u5EFA\u8868\u65B9\u5F0F
#hibernate.hbm2ddl.auto= update
## \u81EA\u52A8\u626B\u63CF\u7684\u5305\u524D\u7F00
entitymanager.packagesToScan= com.xy


## \u6570\u636E\u5E93\u8FDE\u63A5
spring.datasource.url=jdbc:mysql://192.168.26.3:3306/test?\
  useUnicode=true&characterEncoding=utf-8&useSSL=true&autoReconnect=true


## \u7528\u6237\u540D
spring.datasource.username=root


## \u5BC6\u7801
spring.datasource.password=root


## \u6570\u636E\u5E93\u9A71\u52A8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


## \u5EFA\u8868\u65B9\u5F0F這裡一定要寫update是修改,不是替換,預設的是替換
spring.jpa.properties.hibernate.hbm2ddl.auto=update


# \u65B9\u8A00
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

啟動類App.java:

package com.xy;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class App {


public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}



}

實體類User.java:

package com.xy.entity;


import javax.persistence.*;


/**
 * 
 */
@Entity
@Table(name = "t_user")
public class User {


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;


    @Column(name = "user_name")
    private String userName;


    private String password;


    @Column(name = "nick_name")
    private String nickName;


    private String email;


    public Integer getId() {
        return id;
    }


    public void setId(Integer id) {
        this.id = id;
    }


    public String getUserName() {
        return userName;
    }


    public void setUserName(String userName) {
        this.userName = userName;
    }


    public String getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }


    public String getNickName() {
        return nickName;
    }


    public void setNickName(String nickName) {
        this.nickName = nickName;
    }


    public String getEmail() {
        return email;
    }


    public void setEmail(String email) {
        this.email = email;
    }
}
最後啟動App.java就會生成表結構了