Spring boot 搭配 JPA 生成表注释 和 字段注释

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

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

<?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.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>


</project>

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=false

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

@SpringBootApplication
@EntityScan(basePackages={"com.orange.verify.entity"})
public class BuilderApplication {

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

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;

}

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

上面大家会发现了,有两个@Table,第一个@table是jpa自带的,第二个是hibernate的,必须结合使用才能生成表注释…
经过测试,如果把jpa的@table删除,生成,是无反应的(无效的)…

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

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页