mysql数据库表生成jpa注解_Spring boot 搭配 JPA 生成表注释 和 字段注释

  • Post author:
  • Post category:mysql


由于在数据库表反向生成过程中呢,需要通过jpa自动生成表,并且这个表必须有注释…废话不多说,直接亮配置…

1.首先这是我的pom.xml配置咯,说明下,只通过jpa生成,下面的lombok,mybatis-plus,generator这三个可以不要(也可以保留),这三个是我测试工程用到的…真正需要的是:jpa,jdbc,mysql

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”>

4.0.0

<groupId>com.orange.verify</groupId>

<artifactId>builder</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>builder</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/> <!– 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>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jdbc</artifactId>

</dependency>

<dependency>

<groupId>org.mybatis.generator</groupId>

<artifactId>mybatis-generator-core</artifactId>

<version>1.3.7</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.40</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-boot-starter</artifactId>

<version>3.0.6</version>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>1.16.18</version>

<scope>compile</scope>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

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

66

67

68

69

70

71

72

73

74

75

76

2.接下来是我的application.properties配置咯…数据库名称自行更改

spring.jpa.show-sql=true

spring.datasource.url=jdbc:mysql://localhost:3306/com_orange_verify_db?characterEncoding=UTF-8

spring.datasource.username=root

spring.datasource.password=root

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

spring.jpa.properties.hibernate.hbm2ddl.auto=update

spring.jpa.properties.hibernate.show_sql=false1

2

3

4

5

6

7

8

3.启动类扫描你要生成到数据的实体

@SpringBootApplication

@EntityScan(basePackages={“com.orange.verify.entity”})

public class BuilderApplication {

public static void main(String[] args) {

SpringApplication.run(BuilderApplication.class, args);

}

}1

2

3

4

5

6

7

8

4.实体编辑

package com.orange.verify.entity;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

import java.io.Serializable;

@Entity

@Table(name = “test”)

@org.hibernate.annotations.Table(appliesTo = “test”,comment=”我会有表注释的哟…”)

public class Test implements Serializable {

private static final long serialVersionUID = 1L;

@Id

@Column(nullable = false,columnDefinition = “varchar(100) default ” comment ‘我是字段注释…'”)

private String id;

@Column(nullable = false,columnDefinition = “int(2) comment ‘我是年龄注释…'”)

private Integer age;

}1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

6c6bf6759064713a4ad75997f5bf2562.png

配置完这些,直接启动项目,就可以生成到数据库了…接下来讲讲在这个过程中学习到的东西了…

上面大家会发现了,有两个@Table,第一个@table是jpa自带的,第二个是hibernate的,必须结合使用才能生成表注释…

经过测试,如果把jpa的@table删除,生成,是无反应的(无效的)…

然后呢,在测试过程中呢,发现…直接注解到属性上面,然后不用写get set也能生成…



版权声明:本文为weixin_32423063原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。