# A.CTable框架
**Repository Path**: jack_pan/mybatis-enhance
## Basic Information
- **Project Name**: A.CTable框架
- **Description**: A.CTable是一个基于Spring和Mybatis的Maven项目,增强了Mybatis的功能,过配置model注解的方式来创建表,修改表结构,目前仅支持Mysql
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: develop
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 175
- **Created**: 2018-07-12
- **Last Updated**: 2021-11-02
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# mybatis-enhance-actable-1.0.3
该项目是从之前写过的一个框架中抽取出来的,本身是对Mybatis做的增强功能,为了能够使习惯了hibernate框架的开发者能够快速的入手Mybatis,我给他取名叫做 “A.C.Table” 本意是自动建表的意思,A.C.Table是一个基于Spring和Mybatis的Maven项目,增强了Mybatis的功能,过配置model注解的方式来创建表,修改表结构,并且实现了共通的CUDR功能提升开发效率,目前仅支持Mysql,后续可能会扩展针对其他数据库的支持。
A.C.Table是采用了Spring、Mybatis技术的Maven结构,详细介绍如下:
**######### mybatis增加功能自动创建表——A.C.Table版本说明################**
1. 该版本修复了修改主键同时修改其类型引起的error(版本1.0.1)
2. 该版本修复了根据model创建时没有创建父类中的字段的问题(ps:目前只支持扫描一层继承)(版本1.0.1)
3. 该笨笨增加了对唯一约束的支持(版本1.0.1)
4. 从原有的框架中剥离出来,支持任意结构的spring+mybatis的框架使用(版本1.0.1)
5. 再次声明A.C.Table目前仅支持mysql数据库(版本1.0.1)
6. 修复了不同数据库中有相同表名时,启动报错的问题。(版本1.0.2)
7. 修复了model属性名与表字段名不一致时公共的查询方法查不出数据的问题。(版本1.0.2)
8. 增加了对公共的CUDR方法的优化,保存成功会返回id,query查询时可以设置参数进行分页查询(pageSize:int类型标识每页数量,currentPage:int类型标识当前第几页,start:int类型从第几条开始,orderField:string类型排序字段,sortStr:string类型排序方式(desc,asc))(版本1.0.3)
**使用步骤方法**
1. 使用该功能的项目需要依赖mybatis-enhance-actable-1.0.3.jar
已上传至maven中央仓库,使用时pom文件中增加如下依赖
com.gitee.sunchenbin.mybatis.actable
mybatis-enhance-actable
1.0.3
2. 在你的web项目上创建个目录比如config下面创建个文件autoCreateTable.properties文件的内容如下:
mybatis.table.auto=update
mybatis.model.pack=com.sunchenbin.store.model
mybatis.database.type=mysql
本系统提供三种模式:
2.1 当mybatis.table.auto=create时,系统启动后,会将所有的表删除掉,然后根据model中配置的结构重新建表,该操作会破坏原有数据。
2.2 当mybatis.table.auto=update时,系统会自动判断哪些表是新建的,哪些字段要修改类型等,哪些字段要删除,哪些字段要新增,该操作不会破坏原有数据。
2.3 当mybatis.table.auto=none时,系统不做任何处理。
2.4 mybatis.model.pack这个配置是用来配置要扫描的用于创建表的对象的包名
2.5 mybatis.database.type这个是用来区别数据库的,预计会支持这四种数据库mysql/oracle/sqlserver/postgresql,但目前仅支持mysql
3. spring的配置文件中需要做如下配置:
```
classpath*:config/autoCreateTable.properties
1. classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml
2. com.gitee.sunchenbin.mybatis.actable.dao.*
举例这两个配置配置的详细位置
classpath*:com/sunchenbin/store/mapping/*/*.xml
classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml
```
**代码用途讲解**
1.SysMysqlColumns.java这个对象里面配置的是mysql的数据类型,这里配置的类型越多,意味着创建表时能使用的类型越多
2.LengthCount.java是一个自定义的注解,用于标记在SysMysqlColumns.java里面配置的数据类型上的,标记该类型需要设置几个长度,如datetime/varchar(1)/decimal(5,2),分别是需要设置0个1个2个
3.Column.java也是一个自定义的注解,用于标记model中的字段上,作为创建表的依据如不标记,不会被扫描到,有几个属性用来设置字段名、字段类型、长度等属性的设置,详细请看代码上的注释
4.Table.java也是一个自定义的注解,用于标记在model对象上,有一个属性name,用于设置该model生成表后的表名,如不设置该注解,则该model不会被扫描到
5.系统启动后会去自动调用SysMysqlCreateTableManagerImpl.java的createMysqlTable()方法,没错,这就是核心方法了,负责创建、删除、修改表。
**model的写法例子**
```
@Table(name = "test")
public class Test 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,isUnique=true)
private Long number;
@Column(name = "lifecycle",type = MySqlTypeConstant.CHAR,length = 1,isNull=false)
private String lifecycle;
@Column(name = "dekes",type = MySqlTypeConstant.DOUBLE,length = 5,decimalLength = 2)
private Double dekes;
// get和set方法这里就不例举了太多
}
```
**共通的CUDR功能使用**
1.使用方法很简单,大家在manager或者Controller中直接注入BaseMysqlCRUDManager这个接口就可以了
2.注意:接口调用save、delete等方法时传入的对象必须是modle中用于创建表的对象
代码事例:
```
@Controller
public class TestController{
@Autowired
private TestManager testManager;
@Autowired
private BaseMysqlCRUDManager baseMysqlCRUDManager;
/**
* 首页
*/
@RequestMapping("/testDate")
@ResponseBody
public String testDate(){
Test2 test2 = new Test2();
test2.setNumber(3L);
baseMysqlCRUDManager.save(test2);
Test test = new Test();
test.setName("aaae333");
test.setNumber(9L);
test.setDescription("adfsdfe");
baseMysqlCRUDManager.delete(test);
baseMysqlCRUDManager.save(test);
int count = testManager.findTestCount();
System.out.println(count);
List query = baseMysqlCRUDManager.query(test);
String json = JsonUtil.format(query);
return json;
}
}
```