我是靠谱客的博主 积极斑马,这篇文章主要介绍mybaites执行DDL建表、添加字段、同步数据库,mysql数据库mybaites执行DDL建表、添加字段、同步数据库,现在分享给大家,希望可以做个参考。

mybaites执行DDL建表、添加字段、同步数据库

最近做项目有一个功能是实现同步数据库,在网上搜了很多没有找到合适的,自己拼凑出来了完整的sql,在这里分享一下,也当作自己的工作笔记,本人是刚毕业的小白,写的不好勿喷!

建表CREATE TABLE

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//我这里是传一个hashMap把表名放到map里,剩下的字段在同一个实体类里,就放到了ArrayList里(因为建表时候可能会有多个字段), update id="createTable" parameterType="hashMap"> create table ${param.tableName}//这是表名 <foreach collection="param.list" item="item" open="(" separator="," close=")"> //colCode是字段代码 colDatatype是字段类型 colLength是字段长度,因为可能是浮点型所以需要判断一下 ${item.colCode} ${item.colDatatype}(${item.colLength} <if test="item.colPrecision !=null and item.colPrecision != ''"> ,${item.colPrecision}//colPrecision是小数位 </if> )//isPk是主键 <if test="item.isPk != null and item.isPk != ''"> ${item.isPk} </if>//isNull是是否为空 ${item.isNull} <if test="item.colDefault !=null and item.colDefault != ''"> default ${item.colDefault}//这是默认值 </if> <if test="item.isAutoIncrement !=null and item.isAutoIncrement != ''"> ${item.isAutoIncrement}//是否自增 </if> <if test="item.colComment != null and item.colComment !=''"> comment #{item.colComment}//注释 </if> </foreach> </update>

上面的主键,是否为空,自增等不要忘了在controller里进行判断哦,这里放一部分我写的判断

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for (SysColumns sysColumns : list) { if ((sysColumns.getIsPk()).equals("Y")){ sysColumns.setIsPk("PRIMARY KEY"); }else { sysColumns.setIsPk(null); } if ((sysColumns.getIsNull()).equals("N")){ sysColumns.setIsNull("not null"); }else { sysColumns.setIsNull("null"); } if ((sysColumns.getIsAutoIncrement()).equals("Y")){ sysColumns.setIsAutoIncrement("auto_increment"); }else { sysColumns.setIsAutoIncrement(null); } }

单独添加字段的,alter table时候用的

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//这里我是把每一个属性都单独put到map里了,这个也不要忘了在controller里进行判断,这个和上面的有点不大一样,这里就不放了,太乱了见谅。。。 <update id="addColumns" parameterType="hashMap"> alter table ${param.tableName} add ${param.colCode} ${param.colDatatype}(${param.colLength} <if test="param.colPrecision !=null and param.colPrecision != ''"> ,${param.colPrecision} </if> ) <if test="param.isPk != null and param.isPk != ''"> ${param.isPk} </if> ${param.isNull} <if test="param.colDefault !=null and param.colDefault != ''"> default ${param.colDefault} </if> <if test="param.isAutoIncrement !=null and param.isAutoIncrement != ''"> ${param.isAutoIncrement} </if> <if test="param.colComment != null and param.colComment != ''"> comment #{param.colComment} </if> </update>

##SysTable实体类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class SysTable extends BaseEntity { //tableId private Long tableId; //unitId是表所属模块的Id private Long unitId; //表代码(英文名) private String tableCode; //表名(中文名) private String tableName; //是否部署 private String isDeploy; //部署者 private String deployBy; //部署时间 private Date deployTime; //下面是set和get方法还有重写toString

##SysColumns实体类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class SysColumns extends BaseEntity { //colId private Long colId; //所属表的Id private Long tableId; //字段名(中文名) private String colName; //字段名(英文名) private String colCode; //字段注释 private String colComment; //字段类型 private String colDatatype; //字段长度 private Long colLength; //小数位 private Long colPrecision; //默认值 private String colDefault; //是否为空 private String isNull; //是否主键 private String isPk; //是否自增 private String isAutoIncrement; //部署者 private String deployBy; //部署时间 private Date deployTime;

最后附上一个从数据库向平台同步字段的代码

超级超级笨的办法同步的。。。全是截取字符串。。。这里就不多解释了,因为我写完了我自己都看不懂了。。。太乱了。。。

复制代码
1
2
3
4
5
6
7
8
9
10
11
<select id="selectColumns" parameterType="String" resultMap="SysColumnsResult"> SELECT a.colCode,a.colName,a.colDatatype,a.colPrecision,a.colDefault,a.colComment,IF((position(',' in a.colLength))>0, (SUBSTRING_INDEX(a.colLength ,',',1)),a.colLength) as colLength,a.isPk,a.isNull,a.isAutoIncrement from ( select COLUMN_NAME as 'colCode',column_comment as 'colName',SUBSTRING_INDEX(column_type,'(',1) as 'colDatatype',NUMERIC_SCALE as 'colPrecision', column_default as 'colDefault',column_comment as 'colComment', IF((position('(' in column_type))>0, (SUBSTRING_INDEX(SUBSTRING(column_type,1,LENGTH(column_type)-1) ,'(',-1)),NULL) as'colLength', IF(column_key='PRI','Y','N') as 'isPk', IF(IS_NULLABLE='NO','N','Y') as 'isNull', IF(EXTRA='auto_increment','Y','N') as'isAutoIncrement' from INFORMATION_SCHEMA.Columns where table_name=#{tableName} and table_schema='ruoyu' ) a </select>

最后

以上就是积极斑马最近收集整理的关于mybaites执行DDL建表、添加字段、同步数据库,mysql数据库mybaites执行DDL建表、添加字段、同步数据库的全部内容,更多相关mybaites执行DDL建表、添加字段、同步数据库内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部