Mybatis开源框架A.CTable自动创建表
开源插件ACTable是基于Mybatis开发的增强插件,实现自动建表、并支持共通CRUD
(本文为Springboot项目,使用alibaba连接池,通用mapper)
要使用A.CTable,需要有以下步骤:
1、首先需要向pom.xml导入maven依赖
<!--通用mapper-->
<dependency>
<!--A.CTable -->
<!--用于自动生成数据表(类似于jpa)-->
<groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
<artifactId>mybatis-enhance-actable</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<!--该包进一步封装Mybatis,无需实现任何mapper对应的xml和java-->
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.0</version>
</dependency>
2、在项目资源文件夹(resources)下创建autoCreateTable.properties配置文件
mybatis.table.auto=update
mybatis.model.pack=Test.project.ActableDemo.entity
mybatis.database.type=mysql
其中当mybatis.table.auto的取值有三种,分别为create、update、none
取值为create时,程序启动后,会将所有的表删除掉,然后根据实体类的结构自动建表,该操作会破坏原有数据。
取值为update时,程序启动后,会根据表中的结构和实体类中的结构进行对比,创建表中没有的结构,删除表中多余的结构,该操作不会破坏原有的数据。
取值为none时,程序启动后,不会有任何操作。
mybatis.model.pack用来指定存放创建表结构的实体类的包名
mybatis.database.type用来指定数据库类型
3、配置Spring配置文件application.yml
spring:
# 使用druid数据源
datasource:
name: test
url: jdbc:mysql://${MYSQL_HOST:127.0.0.1}:${MYSQL_PORT:3306}/actable?useUnicode=true&useSSL=false&characterEncoding=UTF8
username: root
password: yesanqiu
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
server:
port: 8080
mybatis:
basepackage: Test.project.ActableDemo.**.mapper
mapper-locations: ["classpath*:mapper/**/*.xml","classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml"]
4、制定表结构,配置实体类
@Data
//lombok.Data快捷生成Getter,Setter,equals,hashCode,toString方法
@Table(name="test")
//创建一张表,表的名字为"test"
public class Test {
@Column(name="id",type = MySqlTypeConstant.INT,length = 11,isKey = true,isAutoIncrement = true)
private Integer id;
@Column(name="name",type = MySqlTypeConstant.VARCHAR,length = 20)
private String name;
@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)
private long number;
@Column(name="lifecycle",type = MySqlTypeConstant.VARCHAR)
private String lifecycle;
@Column(name="dekes",type = MySqlTypeConstant.DOUBLE)
private Double dekes;
}
@Coulumn 表示在表格中创建一列,其中的属性
name =“id”–>名字为"id",
type = MySqlTypeConstant.INT–>类型为"INT",
length = 11–>长度为11,
iskey = true–>是否为主键,若为true为主键,
isAutoIncrement = true–>是否自增,若为true则自增。
该表对应的sql如下
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`description` text,
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`number` bigint(255) DEFAULT NULL,
`lifecycle` varchar(255) DEFAULT NULL,
`dekes` double(255,0) DEFAULT NULL,
PRIMARY KEY (`id`)
)
5、启动方法配置
@SpringBootApplication
@EnableTransactionManagement(proxyTargetClass = true)
@ComponentScan(basePackages = {"Test.project.ActableDemo","com.gitee.sunchenbin.mybatis.actable.manager"})
@MapperScan({"Test.project.ActableDemo.**.mapper","com.gitee.sunchenbin.mybatis.actable.dao.**"})
public class ActableDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ActableDemoApplication.class, args);
}
}
表自动创建成功