# create-table **Repository Path**: wch2019/create-table ## Basic Information - **Project Name**: create-table - **Description**: mybatis实体类生成数据库 - **Primary Language**: Java - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-03-21 - **Last Updated**: 2023-03-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mybatis实体类生成数据库 ## 为什么要用? 因为数据库忘记备份,时间久了数据库没了,所以研究了一下这个东西,感谢大佬开发的反向生成,详细连接地址在文末 ## JDK jdk版本1.7+ ## ACTable依赖 ```java com.gitee.sunchenbin.mybatis.actable mybatis-enhance-actable 1.5.0.RELEASE ``` ## SpringBoot配置方式 ```yml actable: table: # 可以设置为create(纯创建)、add(添加新的不删除内容)、update(更新内容,推荐用)、none(啥都不干) auto: create model: # 用于创建数据表的待扫描目录,该目录下的类如果有@Table注解,就会进行扫描 #你的实体类放的包 pack: com.xiaohai.create.entity database: #目前仅支持mysql type: mysql mybatis: mapperLocations: classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*xml ``` 配置解析: | **配置信息** | **可选值值信息** | **描述** | | --------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | actable.table.auto | none | 系统不做任何处理。 | | create | 系统启动后,会先将所有的表删除掉,然后根据model中配置的结构重新建表,该操作会破坏原有数据。 | | | update | 系统启动后,会自动判断哪些表是新建的,哪些字段要修改类型等,哪些字段要删除,哪些字段要新增,该操作不会破坏原有数据。 | | | add | 系统启动后,只做新增,比如新增表/新增字段/新增索引/新增唯一约束的功能,而不会去做修改和删除的操作 (只在版本1.0.9.RELEASE及以上支持)。 | | | actable.model.pack | - | 您的model包路径,多个路径可以用分号或者逗号隔开,会递归这个目录下的全部目录中的java对象,支持类似com.bz.**.entity | | actable.database.type | mysql | 这个是用来区别数据库的,预计会支持这四种数据库mysql/oracle/sqlserver/postgresql,但目前仅支持mysql | ### springboot2.0+启动类需要做如下配置(必备) 1.@ComponentScan配置,路径"com.gitee.sunchenbin.mybatis.actable.manager.*" 2.@MapperScan配置,路径"com.gitee.sunchenbin.mybatis.actable.dao.*" ```java @SpringBootApplication @MapperScan("com.gitee.sunchenbin.mybatis.actable.dao.*") @ComponentScan(basePackages = {"com.gitee.sunchenbin.mybatis.actable.manager.*"}) public class CreateTableApplication { public static void main(String[] args) { SpringApplication.run(CreateTableApplication.class, args); } } ``` ## 多种定义方式 ### 第一种定义方式,使用@Table和@Column方式定义 ```java Data @Table(name = "test") public class Test extends BaseModel { @Column(name = "id",type = MySqlTypeConstant.INT,length = 11,isKey = true,isAutoIncrement = true) private Integer id; @Column(name = "name",type = MySqlTypeConstant.VARCHAR, length = 45, defaultValue = "hello") private String name; @Column(name = "type",type = MySqlTypeConstant.VARCHAR,length = 45) private String type; @Column(name = "description",type = MySqlTypeConstant.TEXT) private String description; @Column(name = "create_time",type = MySqlTypeConstant.DATETIME) private Date create_time; @Column(name = "update_time",type = MySqlTypeConstant.DATETIME) private Date update_time; @Column(name = "number",type = MySqlTypeConstant.BIGINT,length = 5) private Long number; @Column(name = "lifecycle",type = MySqlTypeConstant.CHAR,length = 1) private String lifecycle; @Column(name = "dekes",type = MySqlTypeConstant.DOUBLE,length = 5,decimalLength = 2) private Double dekes; } ``` ### 第二种定义方式,使用@Table和@Column定义字段,具体内容用具体注解定义 ```java @Data @Table(name = "test1") public class Test1 extends BaseModel { @Id @IsAutoIncrement @Column private Integer id; @Column private Double price; @Column @ColumnType(value = MySqlTypeConstant.VARCHAR, length = 455) private String name; @Column private Date time; } ``` ### 第三种定义方式,懒人定义,按照驼峰规则转换 ,不需要对每个字段设置@Column ```java @Data @Table(isSimple = true) public class TestOne { @IsKey @IsAutoIncrement private Integer id; private String name; private String type; } ``` ## 参考 + [ACTable-gitee](https://gitee.com/sunchenbin/mybatis-enhance) + [ACTable开源框架文档](https://www.yuque.com/sunchenbin/actable) + [demo](https://gitee.com/wch2020/create-table)