mybatis ${sql };
新建的时候不能加 ; 号,否则 “### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘CREATE TABLE test_table
(BH_LEVEL
varchar(1000) default ‘ ‘ COMMENT ‘人才’ at line 1”
mapper
int createTable(Map map);
void alterColumn(Map map);
void delAlterColumn(Map map);
void updateAlterColumn(Map map);
void updateAlterColumnType(Map map);
xml
<update id="createTable" parameterType="java.util.Map">
${sql };
</update>
<insert id="alterColumn" parameterType="java.util.Map">
alter table ${table} add column ${column} ${type} COMMENT #{comment};
</insert>
<insert id="delAlterColumn" parameterType="java.util.Map">
ALTER TABLE ${table} DROP COLUMN ${column};
</insert>
<insert id="updateAlterColumn" parameterType="java.util.Map">
alter table ${table} CHANGE ${change} ${newchange} ${type} COMMENT #{comment};
</insert>
<insert id="updateAlterColumnType" parameterType="java.util.Map">
alter table ${table} MODIFY ${modify} ${type} COMMENT #{comment};
</insert>
单元测试
@Test
public void testMybatis() {
Map map = new HashMap();
String sql = "CREATE TABLE `test_create_mybatis` (`username` varchar(64) NOT NULL,`series` varchar(64) NOT NULL,`token` varchar(64) NOT NULL,`last_used` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`series`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
// map.put("sql", sql);
// ************ 建表 ************
// int table = testMapper.createTable(map);
// map.put("table","test_create_mybatis");
// map.put("column","create_column");
// 调用设置字段类型的方法
// map.put("type","varchar(100)");
// map.put("comment","新增字段");
// ******** 新增表字段 ************
// testMapper.alterColumn(map);
// map.put("table","test_create_mybatis");
// map.put("column","create_column");
// ******** 删除表字段 ************
// testMapper.delAlterColumn(map);
// ******** update表字段 一定要重新指定字段类型************
map.put("table","test_create_mybatis");
map.put("change","new_column");
map.put("newchange","bbb");
// 调用设置字段类型的方法
map.put("type","varchar(20)");
map.put("comment","修改字段");
testMapper.updateAlterColumn(map);
// ******** update表字段类型 ************
// map.put("table","test_create_mybatis");
// map.put("modify","new_column");
// // 调用设置字段类型的方法
// map.put("type","varchar(10)");
// map.put("comment","修改字段类型");
//
// testMapper.updateAlterColumnType(map);
}