springboot+hibernate+mysql automatic table building

Keywords: Hibernate Spring MySQL Database

  • Introducing Maven dependency packages
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

spring - boot-starter-data-jpa has been included hibernate Relevant dependencies are needed, so only Jpa dependencies need to be introduced.

  • create profile

Add a line in application.properties: spring.profiles.active=dev

The content of the application-dev.properties configuration file is as follows:

server.port=80

# Hibernate related configuration

## dialect
#hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
## Display Sql
hibernate.show_sql=true
## Automatic tabulation
#hibernate.hbm2ddl.auto= update
## Packet prefix for automatic scanning
entitymanager.packagesToScan= com.zslin

## Database Connection
spring.datasource.url=jdbc:mysql://localhost:3306/study05?\
  useUnicode=true&characterEncoding=utf-8&useSSL=true&autoReconnect=true

## User name
spring.datasource.username=root

## Password
spring.datasource.password=123

## Database Driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

## Tabulation method
spring.jpa.properties.hibernate.hbm2ddl.auto=update

# dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

Note: The last two configurations, spring.jpa.properties.hibernate.hbm2ddl.auto=update instead of hibernate.hbm2ddl.auto=update, use Jpa so the key name needs to be adjusted accordingly, otherwise the table will not be built automatically.

spring.jpa.properties.hibernate.hbm2ddl.auto has several configurations:

  • create: Each time Hibernate is loaded, the table generated last time is deleted and the new table is regenerated, even if there are no changes twice, which results in a new start-up. data base It is also an important cause of data loss.

  • create-drop: Tables are generated every time Hibernate is loaded, but when Session Factory is closed, the generated tables are automatically deleted.

  • Update: The most commonly used attribute value is to create a data table when Hibernate is first loaded (provided there is a database first), and then to update it only according to the model when HIbernate is loaded later. Even if the model has deleted some attributes, the data table will not delete the fields along with it.

  • validate: The data table structure is validated every time Hibernate is loaded, only compared with existing data tables, and the table structure is modified according to the model, but no new tables are created.

Specific configuration can be referred to in the article. Springboot File Structure and Configuration File

  • Create Model Entity Classes
package com.zslin.model;

import javax.persistence.*;

/**
 * Created by Zhong Shulin 393156105@qq.com on 2016/10/18 17:15.
 */
@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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

Be careful:

1. Notes are needed on the build ID: @Id and @GeneratedValue(strategy) = Generation Type. AUTO) will grow automatically

2. Add a comment @Column(name = field name) on the property that needs to reset the table field name.

3. Add comments on the class name: @Entity and @Table(name) = "t_user", t_user is the table name

  • Start the project

After starting the project, t_user tables will appear in the study05 database, and there are corresponding table fields.

Sample code: https://github.com/zsl131/spring-boot-test/tree/master/study05

Posted by diggysmalls on Wed, 17 Jul 2019 13:00:57 -0700