1.添加依赖
<!-- A.CTable 自动创建数据表 -->
<dependency>
<groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
<artifactId>mybatis-enhance-actable</artifactId>
<version>1.0.4</version>
</dependency>
2.配置application.properties
mybatis.table.auto=update mybatis.model.pack=com.boot.entity mybatis.database.type=mysql
2.创建config
MybatisEntity
/**
* <p>Title: MybatisEntity.java</p>
* <p>Description: </p>
* @author zhouyue
* @date 2018年11月29日
* @version 1.0
*/
package com.boot.entity;
/**
* <p>Title: MybatisEntity</p>
* <p>Description: </p>
* @author zhouyue
* @date 2018年11月29日
*/
import java.sql.Date;
import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
import com.gitee.sunchenbin.mybatis.actable.command.BaseModel;
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant;
@Table(name = "MybatisEntity")
public class MybatisEntity extends BaseModel{
private static final long serialVersionUID = 5199200306752426433L;
@Column(name = "id",type = MySqlTypeConstant.INT,length = 11,isKey = true,isAutoIncrement = true)
private Integer id;
@Column(name = "name",type = MySqlTypeConstant.VARCHAR,length = 111)
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,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;
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Date getCreate_time(){
return create_time;
}
public void setCreate_time(Date create_time){
this.create_time = create_time;
}
public Date getUpdate_time(){
return update_time;
}
public void setUpdate_time(Date update_time){
this.update_time = update_time;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
public Long getNumber(){
return number;
}
public void setNumber(Long number){
this.number = number;
}
public String getLifecycle(){
return lifecycle;
}
public void setLifecycle(String lifecycle){
this.lifecycle = lifecycle;
}
public Double getDekes(){
return dekes;
}
public void setDekes(Double dekes){
this.dekes = dekes;
}
}
mybatisTableConfig
/** * <p>Title: MybatisTableConfig.java</p> * <p>Description: </p> * @author zhouyue * @date 2018年11月29日 * @version 1.0 */ package com.boot.config; import com.alibaba.druid.pool.DruidDataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; /** * <p>Title: MybatisTableConfig</p> * <p>Description: </p> * @author zhouyue * @date 2018年11月29日 */ @Configuration @ComponentScan(basePackages = {"com.gitee.sunchenbin.mybatis.actable.manager.*"}) public class MybatisTableConfig { @Value("${spring.datasource.driver-class-name}") private String driver; @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Bean public PropertiesFactoryBean configProperties() throws Exception{ PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); propertiesFactoryBean.setLocations(resolver.getResources("classpath*:application.properties")); return propertiesFactoryBean; } @Bean public DruidDataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setMaxActive(30); dataSource.setInitialSize(10); dataSource.setValidationQuery("SELECT 1"); dataSource.setTestOnBorrow(true); return dataSource; } @Bean public DataSourceTransactionManager dataSourceTransactionManager() { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource()); return dataSourceTransactionManager; } @Bean public SqlSessionFactoryBean sqlSessionFactory() throws Exception{ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml")); sqlSessionFactoryBean.setTypeAliasesPackage("com.boot.entity.*"); return sqlSessionFactoryBean; } }
创建entity
/** * <p>Title: MybatisEntity.java</p> * <p>Description: </p> * @author zhouyue * @date 2018年11月29日 * @version 1.0 */ package com.boot.entity; /** * <p>Title: MybatisEntity</p> * <p>Description: </p> * @author zhouyue * @date 2018年11月29日 */ import java.sql.Date; import com.gitee.sunchenbin.mybatis.actable.annotation.Column; import com.gitee.sunchenbin.mybatis.actable.annotation.Table; import com.gitee.sunchenbin.mybatis.actable.command.BaseModel; import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant; @Table(name = "MybatisEntity") public class MybatisEntity extends BaseModel{ private static final long serialVersionUID = 5199200306752426433L; @Column(name = "id",type = MySqlTypeConstant.INT,length = 11,isKey = true,isAutoIncrement = true) private Integer id; @Column(name = "name",type = MySqlTypeConstant.VARCHAR,length = 111) 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,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; public Integer getId(){ return id; } public void setId(Integer id){ this.id = id; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public Date getCreate_time(){ return create_time; } public void setCreate_time(Date create_time){ this.create_time = create_time; } public Date getUpdate_time(){ return update_time; } public void setUpdate_time(Date update_time){ this.update_time = update_time; } public String getDescription(){ return description; } public void setDescription(String description){ this.description = description; } public Long getNumber(){ return number; } public void setNumber(Long number){ this.number = number; } public String getLifecycle(){ return lifecycle; } public void setLifecycle(String lifecycle){ this.lifecycle = lifecycle; } public Double getDekes(){ return dekes; } public void setDekes(Double dekes){ this.dekes = dekes; } }
代码地址
https://github.com/zyf970617/mybatis-auto-create-table.git