使用技术:
springboot
mybatis/mybatis-plus
application.yml/application. properties
在这里使用application.yml 和 mybatis-plus做示例 , 其他的请看配置说明进行更改
1.引入依赖
注意这里使用的版本是1.4.6
<!--mybatis-plus自动建表功能-->
<dependency>
<groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
<artifactId>mybatis-enhance-actable</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
2.application.yml 配置
#自动建表配置
mybatis:
table:
auto: update
model:
pack: com.wteam.dragon.security.bean,com.wteam.dragon.pay.bean #扫描用于创建表的对象的包名
database:
type: mysql
mybatis-plus:
mapper-locations: classpath*:xxxxxx/*.xml,classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml
配置说明:
(1)auto
当auto=create时,系统启动后,会将所有的表删除掉,然后根据model中配置的结构重新建表,该操作会破坏原有数据。
当auto=update时,系统会自动判断哪些表是新建的,哪些字段要修改类型等,哪些字段要删除,哪些字段要新增,该操作不会破坏原有数据。
当auto=none时,系统不做任何处理。
当auto=add,新增表/新增字段/新增索引/新增唯一约束的功能,不做修改和删除
(2)pack
扫描用于创建表的对象的包名(这里换成你自己的创建表的包名,支持多路径,用逗号隔开即可)
(3)type
目前只支持mysql
(4)mapper-locations
配置mapper路径
(5)如果使用的是mybatis
将mapper-locations放到mybatis配置中
mybatis:
mapper-locations: classpath*:xxxxxx/*.xml,classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml
(6)如果使用application. properties
除了格式转换,还要将mapper-locations写成mapperLocations
3.在Application启动类中加上(直接复制不要进行更改)
@MapperScan({"com.gitee.sunchenbin.mybatis.actable.dao.*"} )
@ComponentScan("com.gitee.sunchenbin.mybatis.actable.manager.*")
4.使用方法
@Table
public class Role extends BaseModel {
/**
* 自增id
*/
@IsKey
@IsNotNull
@IsAutoIncrement
@TableId(type = IdType.AUTO)
@ColumnType(MySqlTypeConstant.BIGINT)
@Column(comment = "自增id")
private Long roleId;
/**
* 角色名字
*/
@IsNotNull
@Column(comment = "角色名字", length = 20)
@ColumnType(MySqlTypeConstant.VARCHAR)
private String name;
/**
* 角色的中文名字
*/
@Column(comment = "角色的中文名字", length = 20)
@ColumnType(MySqlTypeConstant.VARCHAR)
private String nameZh;
}
注:更多注解的用法可以看官网:地址
5.启动启动类查看是否创建成功(注意连接mysql)

6.感谢作者孙琛斌为我们提供的A.CTable
项目地址:https://gitee.com/sunchenbin/mybatis-enhance
7.有问题请在下面提出
版权声明:本文为q15976405716原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。