公司业务上有源码没有数据库表 所以我就想通过实体类生成表, 这边是的jpa的方式实现的 先看pom
这里我是基于boot来生成的版本是2.7.3
在这里插入图片描述

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
  </dependencies>

配置文件

server:
  port: 8082

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/mydemo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: 123456

    #jpa配置  更新或自动生成表  控制台打印sql
  jpa:
    database: mysql
    show-sql: true
    hibernate:
      ddl-auto: update

启动类 在这里配置上 @EntityScan 注解 这个地址就是你实体类的地址

@EntityScan(basePackages={"com.example.reversegeneration.entity"})
@SpringBootApplication
public class ReversegenerationApplication {

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

实体类

package com.example.reversegeneration.entity;
import org.hibernate.annotations.CreationTimestamp;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "`airline_info`") //设置表名
@org.hibernate.annotations.Table(appliesTo = "airline_info", comment = "测试信息")//设置表注释
public class AirlineInfo implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "`id`")
    @GeneratedValue(strategy = GenerationType.IDENTITY)  //mysql自动增长
    private Long id;
    
    @Column(name = "`name`",nullable = false,columnDefinition = "varchar(100) COMMENT '名称'")
    private String name;
    
    @Column(name = "`code`",nullable = false,columnDefinition = "varchar(100) COMMENT '航司二字码'")
    private String code;

    @Column(name = "`valid`",nullable = false,columnDefinition = "int(100) COMMENT '状态,0:无效, 1:有效'")
    private Integer valid;


    @Column(name = "`contact_name`",nullable = false,columnDefinition = "varchar(100) COMMENT '加密联系人'")
    private String contactName;

    @Column(name = "`contact_phone`",nullable = false,columnDefinition = "varchar(100) COMMENT '加密联系电话'")
    private String contactPhone;

    @Column(name = "`contact_email`",nullable = false,columnDefinition = "varchar(100) COMMENT '邮箱'")
    private String contactEmail;


    @Column(name = "`address`",nullable = false,columnDefinition = "varchar(100) COMMENT '地址'")
    private String address;

  
    @Column(name = "`create_time`",nullable = false,columnDefinition = "DATETIME COMMENT '创建时间'")
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") // 格式化时间
    @CreationTimestamp // 注解是放在JPA的实体类上的会生成一个默认的一个时间戳
    private Date createTime;

    
    
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") // 格式化时间
    @CreationTimestamp // 注解是放在JPA的实体类上的会生成一个默认的一个时间戳
    @Column(name = "`update_time`",nullable = false,columnDefinition = "DATETIME COMMENT '更新时间'")
    private Date updateTime;


    @Column(name = "`create_by`",nullable = false,columnDefinition = "varchar(100) COMMENT '创建人'")
    private String createBy;

    @Column(name = "`update_by`",nullable = false,columnDefinition = "varchar(100) COMMENT '更新人'")
    private String updateBy;

}

当然也可以不加注释
@Column(name = “update_by”) 也是可以生成的,就是生成没有注释

参考大佬的链接 https://www.cnblogs.com/wf-zhang/p/14893274.html

更多推荐