今天抽空写一下生成器比较重要的环节,如何自定义mybatis生成器的sql xml文件,因为原生出来的格式不好看,命名也不符合我们日常使用习惯,很多冗余的sql节点,下面我直接直入主题演示代码了,还是老规矩使用之前教程延续下来的项目用例
1.先看看我们原始生成出来的mapper.xml文件,1.节点间没有空行不符合我们日常格式 2.CRUD方法太累赘,我们其实只需要insert,update,delete,select
id, courseName, courseReq, teaId, teaName, stuId, stuName, stuPhone, textbookId,
creator, lastModifier, countDate, countTime, countStu, sketchType, courseStyle, classType,
process, courseStatus, pay, perPay, couponPay, originalPay, couponRule, beginDate,
endDate, courseNum, hasNum, cancelNum, courseNumInput, orderNum, fav, vipLevel, cancelReason,
createDate, lastModDate, formatter, status
delete from qfd_sketch
where id = #{id,jdbcType=BIGINT}
insert into qfd_sketch (id, courseName, courseReq,
teaId, teaName, stuId,
stuName, stuPhone, textbookId,
creator, lastModifier, countDate,
countTime, countStu, sketchType,
courseStyle, classType, process,
courseStatus, pay, perPay,
couponPay, originalPay, couponRule,
beginDate, endDate, courseNum,
hasNum, cancelNum, courseNumInput,
orderNum, fav, vipLevel,
cancelReason, createDate, lastModDate,
formatter, status)
values (#{id,jdbcType=BIGINT}, #{coursename,jdbcType=VARCHAR}, #{coursereq,jdbcType=VARCHAR},
#{teaid,jdbcType=VARCHAR}, #{teaname,jdbcType=VARCHAR}, #{stuid,jdbcType=VARCHAR},
#{stuname,jdbcType=VARCHAR}, #{stuphone,jdbcType=VARCHAR}, #{textbookid,jdbcType=BIGINT},
#{creator,jdbcType=VARCHAR}, #{lastmodifier,jdbcType=VARCHAR}, #{countdate,jdbcType=INTEGER},
#{counttime,jdbcType=INTEGER}, #{countstu,jdbcType=INTEGER}, #{sketchtype,jdbcType=INTEGER},
#{coursestyle,jdbcType=INTEGER}, #{classtype,jdbcType=INTEGER}, #{process,jdbcType=INTEGER},
#{coursestatus,jdbcType=INTEGER}, #{pay,jdbcType=VARCHAR}, #{perpay,jdbcType=VARCHAR},
#{couponpay,jdbcType=VARCHAR}, #{originalpay,jdbcType=VARCHAR}, #{couponrule,jdbcType=INTEGER},
#{begindate,jdbcType=BIGINT}, #{enddate,jdbcType=BIGINT}, #{coursenum,jdbcType=INTEGER},
#{hasnum,jdbcType=INTEGER}, #{cancelnum,jdbcType=INTEGER}, #{coursenuminput,jdbcType=INTEGER},
#{ordernum,jdbcType=INTEGER}, #{fav,jdbcType=BIGINT}, #{viplevel,jdbcType=INTEGER},
#{cancelreason,jdbcType=INTEGER}, #{createdate,jdbcType=BIGINT}, #{lastmoddate,jdbcType=BIGINT},
#{formatter,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT})
insert into qfd_sketch
id,
courseName,
courseReq,
teaId,
teaName,
stuId,
stuName,
stuPhone,
textbookId,
creator,
lastModifier,
countDate,
countTime,
countStu,
sketchType,
courseStyle,
classType,
process,
courseStatus,
pay,
perPay,
couponPay,
originalPay,
couponRule,
beginDate,
endDate,
courseNum,
hasNum,
cancelNum,
courseNumInput,
orderNum,
fav,
vipLevel,
cancelReason,
createDate,
lastModDate,
formatter,
status,
#{id,jdbcType=BIGINT},
#{coursename,jdbcType=VARCHAR},
#{coursereq,jdbcType=VARCHAR},
#{teaid,jdbcType=VARCHAR},
#{teaname,jdbcType=VARCHAR},
#{stuid,jdbcType=VARCHAR},
#{stuname,jdbcType=VARCHAR},
#{stuphone,jdbcType=VARCHAR},
#{textbookid,jdbcType=BIGINT},
#{creator,jdbcType=VARCHAR},
#{lastmodifier,jdbcType=VARCHAR},
#{countdate,jdbcType=INTEGER},
#{counttime,jdbcType=INTEGER},
#{countstu,jdbcType=INTEGER},
#{sketchtype,jdbcType=INTEGER},
#{coursestyle,jdbcType=INTEGER},
#{classtype,jdbcType=INTEGER},
#{process,jdbcType=INTEGER},
#{coursestatus,jdbcType=INTEGER},
#{pay,jdbcType=VARCHAR},
#{perpay,jdbcType=VARCHAR},
#{couponpay,jdbcType=VARCHAR},
#{originalpay,jdbcType=VARCHAR},
#{couponrule,jdbcType=INTEGER},
#{begindate,jdbcType=BIGINT},
#{enddate,jdbcType=BIGINT},
#{coursenum,jdbcType=INTEGER},
#{hasnum,jdbcType=INTEGER},
#{cancelnum,jdbcType=INTEGER},
#{coursenuminput,jdbcType=INTEGER},
#{ordernum,jdbcType=INTEGER},
#{fav,jdbcType=BIGINT},
#{viplevel,jdbcType=INTEGER},
#{cancelreason,jdbcType=INTEGER},
#{createdate,jdbcType=BIGINT},
#{lastmoddate,jdbcType=BIGINT},
#{formatter,jdbcType=VARCHAR},
#{status,jdbcType=TINYINT},
update qfd_sketch
courseName = #{coursename,jdbcType=VARCHAR},
courseReq = #{coursereq,jdbcType=VARCHAR},
teaId = #{teaid,jdbcType=VARCHAR},
teaName = #{teaname,jdbcType=VARCHAR},
stuId = #{stuid,jdbcType=VARCHAR},
stuName = #{stuname,jdbcType=VARCHAR},
stuPhone = #{stuphone,jdbcType=VARCHAR},
textbookId = #{textbookid,jdbcType=BIGINT},
creator = #{creator,jdbcType=VARCHAR},
lastModifier = #{lastmodifier,jdbcType=VARCHAR},
countDate = #{countdate,jdbcType=INTEGER},
countTime = #{counttime,jdbcType=INTEGER},
countStu = #{countstu,jdbcType=INTEGER},
sketchType = #{sketchtype,jdbcType=INTEGER},
courseStyle = #{coursestyle,jdbcType=INTEGER},
classType = #{classtype,jdbcType=INTEGER},
process = #{process,jdbcType=INTEGER},
courseStatus = #{coursestatus,jdbcType=INTEGER},
pay = #{pay,jdbcType=VARCHAR},
perPay = #{perpay,jdbcType=VARCHAR},
couponPay = #{couponpay,jdbcType=VARCHAR},
originalPay = #{originalpay,jdbcType=VARCHAR},
couponRule = #{couponrule,jdbcType=INTEGER},
beginDate = #{begindate,jdbcType=BIGINT},
endDate = #{enddate,jdbcType=BIGINT},
courseNum = #{coursenum,jdbcType=INTEGER},
hasNum = #{hasnum,jdbcType=INTEGER},
cancelNum = #{cancelnum,jdbcType=INTEGER},
courseNumInput = #{coursenuminput,jdbcType=INTEGER},
orderNum = #{ordernum,jdbcType=INTEGER},
fav = #{fav,jdbcType=BIGINT},
vipLevel = #{viplevel,jdbcType=INTEGER},
cancelReason = #{cancelreason,jdbcType=INTEGER},
createDate = #{createdate,jdbcType=BIGINT},
lastModDate = #{lastmoddate,jdbcType=BIGINT},
formatter = #{formatter,jdbcType=VARCHAR},
status = #{status,jdbcType=TINYINT},
where id = #{id,jdbcType=BIGINT}
update qfd_sketch
set courseName = #{coursename,jdbcType=VARCHAR},
courseReq = #{coursereq,jdbcType=VARCHAR},
teaId = #{teaid,jdbcType=VARCHAR},
teaName = #{teaname,jdbcType=VARCHAR},
stuId = #{stuid,jdbcType=VARCHAR},
stuName = #{stuname,jdbcType=VARCHAR},
stuPhone = #{stuphone,jdbcType=VARCHAR},
textbookId = #{textbookid,jdbcType=BIGINT},
creator = #{creator,jdbcType=VARCHAR},
lastModifier = #{lastmodifier,jdbcType=VARCHAR},
countDate = #{countdate,jdbcType=INTEGER},
countTime = #{counttime,jdbcType=INTEGER},
countStu = #{countstu,jdbcType=INTEGER},
sketchType = #{sketchtype,jdbcType=INTEGER},
courseStyle = #{coursestyle,jdbcType=INTEGER},
classType = #{classtype,jdbcType=INTEGER},
process = #{process,jdbcType=INTEGER},
courseStatus = #{coursestatus,jdbcType=INTEGER},
pay = #{pay,jdbcType=VARCHAR},
perPay = #{perpay,jdbcType=VARCHAR},
couponPay = #{couponpay,jdbcType=VARCHAR},
originalPay = #{originalpay,jdbcType=VARCHAR},
couponRule = #{couponrule,jdbcType=INTEGER},
beginDate = #{begindate,jdbcType=BIGINT},
endDate = #{enddate,jdbcType=BIGINT},
courseNum = #{coursenum,jdbcType=INTEGER},
hasNum = #{hasnum,jdbcType=INTEGER},
cancelNum = #{cancelnum,jdbcType=INTEGER},
courseNumInput = #{coursenuminput,jdbcType=INTEGER},
orderNum = #{ordernum,jdbcType=INTEGER},
fav = #{fav,jdbcType=BIGINT},
vipLevel = #{viplevel,jdbcType=INTEGER},
cancelReason = #{cancelreason,jdbcType=INTEGER},
createDate = #{createdate,jdbcType=BIGINT},
lastModDate = #{lastmoddate,jdbcType=BIGINT},
formatter = #{formatter,jdbcType=VARCHAR},
status = #{status,jdbcType=TINYINT}
where id = #{id,jdbcType=BIGINT}
2.根据已知问题,我们先处理一下问题吧,保留insert,update,delete方法作为示例,顺便给他们增加隔行的格式化
先看看XMLMapperGenerator.java这个类的getSqlMapElement()方法,可以看到很多addxxx方法,这里就是拼接sql xml文件的逻辑,我们只保留必须的方法
protected XmlElement getSqlMapElement() {
FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
progressCallback.startTask(getString(
"Progress.12", table.toString())); //$NON-NLS-1$
XmlElement answer = new XmlElement("mapper"); //$NON-NLS-1$
String namespace = introspectedTable.getMyBatis3SqlMapNamespace();
answer.addAttribute(new Attribute("namespace", //$NON-NLS-1$
namespace));
context.getCommentGenerator().addRootComment(answer);
addResultMapWithoutBLOBsElement(answer);
addResultMapWithBLOBsElement(answer);
addExampleWhereClauseElement(answer);
addMyBatis3UpdateByExampleWhereClauseElement(answer);
addBaseColumnListElement(answer);
addBlobColumnListElement(answer);
addSelectByExampleWithBLOBsElement(answer);
addSelectByExampleWithoutBLOBsElement(answer);
addSelectByPrimaryKeyElement(answer);
addDeleteByPrimaryKeyElement(answer);
addDeleteByExampleElement(answer);
addInsertElement(answer);
addInsertSelectiveElement(answer);
addCountByExampleElement(answer);
addUpdateByExampleSelectiveElement(answer);
addUpdateByExampleWithBLOBsElement(answer);
addUpdateByExampleWithoutBLOBsElement(answer);
addUpdateByPrimaryKeySelectiveElement(answer);
addUpdateByPrimaryKeyWithBLOBsElement(answer);
addUpdateByPrimaryKeyWithoutBLOBsElement(answer);
return answer;
}
protected XmlElement getSqlMapElement() {
FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
progressCallback.startTask(getString(
"Progress.12", table.toString())); //$NON-NLS-1$
XmlElement answer = new XmlElement("mapper"); //$NON-NLS-1$
String namespace = introspectedTable.getMyBatis3SqlMapNamespace();
answer.addAttribute(new Attribute("namespace", //$NON-NLS-1$
namespace));
context.getCommentGenerator().addRootComment(answer);
// addResultMapWithoutBLOBsElement(answer);
// addResultMapWithBLOBsElement(answer);
// addExampleWhereClauseElement(answer);
// addMyBatis3UpdateByExampleWhereClauseElement(answer);
// addBaseColumnListElement(answer);
// addBlobColumnListElement(answer);
// addSelectByExampleWithBLOBsElement(answer);
// addSelectByExampleWithoutBLOBsElement(answer);
// addSelectByPrimaryKeyElement(answer);
// addDeleteByPrimaryKeyElement(answer);
// addDeleteByExampleElement(answer);
// addInsertElement(answer);
// addInsertSelectiveElement(answer);
// addCountByExampleElement(answer);
// addUpdateByExampleSelectiveElement(answer);
// addUpdateByExampleWithBLOBsElement(answer);
// addUpdateByExampleWithoutBLOBsElement(answer);
// addUpdateByPrimaryKeySelectiveElement(answer);
// addUpdateByPrimaryKeyWithBLOBsElement(answer);
// addUpdateByPrimaryKeyWithoutBLOBsElement(answer);
addResultMapWithoutBLOBsElement(answer);
addBaseColumnListElement(answer);
addInsertSelectiveElement(answer);
addUpdateByPrimaryKeySelectiveElement(answer);
addDeleteByPrimaryKeyElement(answer);
return answer;
}
id, courseName, courseReq, teaId, teaName, stuId, stuName, stuPhone, textbookId,
creator, lastModifier, countDate, countTime, countStu, sketchType, courseStyle, classType,
process, courseStatus, pay, perPay, couponPay, originalPay, couponRule, beginDate,
endDate, courseNum, hasNum, cancelNum, courseNumInput, orderNum, fav, vipLevel, cancelReason,
createDate, lastModDate, formatter, status
insert into qfd_sketch
id,
courseName,
courseReq,
teaId,
teaName,
stuId,
stuName,
stuPhone,
textbookId,
creator,
lastModifier,
countDate,
countTime,
countStu,
sketchType,
courseStyle,
classType,
process,
courseStatus,
pay,
perPay,
couponPay,
originalPay,
couponRule,
beginDate,
endDate,
courseNum,
hasNum,
cancelNum,
courseNumInput,
orderNum,
fav,
vipLevel,
cancelReason,
createDate,
lastModDate,
formatter,
status,
#{id,jdbcType=BIGINT},
#{coursename,jdbcType=VARCHAR},
#{coursereq,jdbcType=VARCHAR},
#{teaid,jdbcType=VARCHAR},
#{teaname,jdbcType=VARCHAR},
#{stuid,jdbcType=VARCHAR},
#{stuname,jdbcType=VARCHAR},
#{stuphone,jdbcType=VARCHAR},
#{textbookid,jdbcType=BIGINT},
#{creator,jdbcType=VARCHAR},
#{lastmodifier,jdbcType=VARCHAR},
#{countdate,jdbcType=INTEGER},
#{counttime,jdbcType=INTEGER},
#{countstu,jdbcType=INTEGER},
#{sketchtype,jdbcType=INTEGER},
#{coursestyle,jdbcType=INTEGER},
#{classtype,jdbcType=INTEGER},
#{process,jdbcType=INTEGER},
#{coursestatus,jdbcType=INTEGER},
#{pay,jdbcType=VARCHAR},
#{perpay,jdbcType=VARCHAR},
#{couponpay,jdbcType=VARCHAR},
#{originalpay,jdbcType=VARCHAR},
#{couponrule,jdbcType=INTEGER},
#{begindate,jdbcType=BIGINT},
#{enddate,jdbcType=BIGINT},
#{coursenum,jdbcType=INTEGER},
#{hasnum,jdbcType=INTEGER},
#{cancelnum,jdbcType=INTEGER},
#{coursenuminput,jdbcType=INTEGER},
#{ordernum,jdbcType=INTEGER},
#{fav,jdbcType=BIGINT},
#{viplevel,jdbcType=INTEGER},
#{cancelreason,jdbcType=INTEGER},
#{createdate,jdbcType=BIGINT},
#{lastmoddate,jdbcType=BIGINT},
#{formatter,jdbcType=VARCHAR},
#{status,jdbcType=TINYINT},
update qfd_sketch
courseName = #{coursename,jdbcType=VARCHAR},
courseReq = #{coursereq,jdbcType=VARCHAR},
teaId = #{teaid,jdbcType=VARCHAR},
teaName = #{teaname,jdbcType=VARCHAR},
stuId = #{stuid,jdbcType=VARCHAR},
stuName = #{stuname,jdbcType=VARCHAR},
stuPhone = #{stuphone,jdbcType=VARCHAR},
textbookId = #{textbookid,jdbcType=BIGINT},
creator = #{creator,jdbcType=VARCHAR},
lastModifier = #{lastmodifier,jdbcType=VARCHAR},
countDate = #{countdate,jdbcType=INTEGER},
countTime = #{counttime,jdbcType=INTEGER},
countStu = #{countstu,jdbcType=INTEGER},
sketchType = #{sketchtype,jdbcType=INTEGER},
courseStyle = #{coursestyle,jdbcType=INTEGER},
classType = #{classtype,jdbcType=INTEGER},
process = #{process,jdbcType=INTEGER},
courseStatus = #{coursestatus,jdbcType=INTEGER},
pay = #{pay,jdbcType=VARCHAR},
perPay = #{perpay,jdbcType=VARCHAR},
couponPay = #{couponpay,jdbcType=VARCHAR},
originalPay = #{originalpay,jdbcType=VARCHAR},
couponRule = #{couponrule,jdbcType=INTEGER},
beginDate = #{begindate,jdbcType=BIGINT},
endDate = #{enddate,jdbcType=BIGINT},
courseNum = #{coursenum,jdbcType=INTEGER},
hasNum = #{hasnum,jdbcType=INTEGER},
cancelNum = #{cancelnum,jdbcType=INTEGER},
courseNumInput = #{coursenuminput,jdbcType=INTEGER},
orderNum = #{ordernum,jdbcType=INTEGER},
fav = #{fav,jdbcType=BIGINT},
vipLevel = #{viplevel,jdbcType=INTEGER},
cancelReason = #{cancelreason,jdbcType=INTEGER},
createDate = #{createdate,jdbcType=BIGINT},
lastModDate = #{lastmoddate,jdbcType=BIGINT},
formatter = #{formatter,jdbcType=VARCHAR},
status = #{status,jdbcType=TINYINT},
where id = #{id,jdbcType=BIGINT}
delete from qfd_sketch
where id = #{id,jdbcType=BIGINT}
4.看到上面的文件后发现已经达到我们一个目的了,把多余的方法给去掉,但是格式不够好看,我们来完成下面一个目标,增加sql节点隔行以及修改sql语句缩进格式
继续看XMLMapperGenerator.java类的initializeAndExecuteGenerator()方法
protected void initializeAndExecuteGenerator(
AbstractXmlElementGenerator elementGenerator,
XmlElement parentElement) {
elementGenerator.setContext(context);
elementGenerator.setIntrospectedTable(introspectedTable);
elementGenerator.setProgressCallback(progressCallback);
elementGenerator.setWarnings(warnings);
elementGenerator.addElements(parentElement);
}
protected void initializeAndExecuteGenerator(
AbstractXmlElementGenerator elementGenerator,
XmlElement parentElement) {
elementGenerator.setContext(context);
elementGenerator.setIntrospectedTable(introspectedTable);
elementGenerator.setProgressCallback(progressCallback);
elementGenerator.setWarnings(warnings);
parentElement.addElement(new TextElement("")); // 增加一行空白的隔行
elementGenerator.addElements(parentElement);
}
打开OutputUtilities.java方法看
public static void xmlIndent(StringBuilder sb, int indentLevel) {
for (int i = 0; i < indentLevel; i++) {
sb.append(" "); //$NON-NLS-1$
}
}
public static void xmlIndent(StringBuilder sb, int indentLevel) {
for (int i = 0; i < indentLevel; i++) {
// sb.append(" "); //$NON-NLS-1$
sb.append(" "); // 4个空格符
}
}
5.修改方法后我们重新生成下xml文件看看效果
id, courseName, courseReq, teaId, teaName, stuId, stuName, stuPhone, textbookId,
creator, lastModifier, countDate, countTime, countStu, sketchType, courseStyle, classType,
process, courseStatus, pay, perPay, couponPay, originalPay, couponRule, beginDate,
endDate, courseNum, hasNum, cancelNum, courseNumInput, orderNum, fav, vipLevel, cancelReason,
createDate, lastModDate, formatter, status
insert into qfd_sketch
id,
courseName,
courseReq,
teaId,
teaName,
stuId,
stuName,
stuPhone,
textbookId,
creator,
lastModifier,
countDate,
countTime,
countStu,
sketchType,
courseStyle,
classType,
process,
courseStatus,
pay,
perPay,
couponPay,
originalPay,
couponRule,
beginDate,
endDate,
courseNum,
hasNum,
cancelNum,
courseNumInput,
orderNum,
fav,
vipLevel,
cancelReason,
createDate,
lastModDate,
formatter,
status,
#{id,jdbcType=BIGINT},
#{coursename,jdbcType=VARCHAR},
#{coursereq,jdbcType=VARCHAR},
#{teaid,jdbcType=VARCHAR},
#{teaname,jdbcType=VARCHAR},
#{stuid,jdbcType=VARCHAR},
#{stuname,jdbcType=VARCHAR},
#{stuphone,jdbcType=VARCHAR},
#{textbookid,jdbcType=BIGINT},
#{creator,jdbcType=VARCHAR},
#{lastmodifier,jdbcType=VARCHAR},
#{countdate,jdbcType=INTEGER},
#{counttime,jdbcType=INTEGER},
#{countstu,jdbcType=INTEGER},
#{sketchtype,jdbcType=INTEGER},
#{coursestyle,jdbcType=INTEGER},
#{classtype,jdbcType=INTEGER},
#{process,jdbcType=INTEGER},
#{coursestatus,jdbcType=INTEGER},
#{pay,jdbcType=VARCHAR},
#{perpay,jdbcType=VARCHAR},
#{couponpay,jdbcType=VARCHAR},
#{originalpay,jdbcType=VARCHAR},
#{couponrule,jdbcType=INTEGER},
#{begindate,jdbcType=BIGINT},
#{enddate,jdbcType=BIGINT},
#{coursenum,jdbcType=INTEGER},
#{hasnum,jdbcType=INTEGER},
#{cancelnum,jdbcType=INTEGER},
#{coursenuminput,jdbcType=INTEGER},
#{ordernum,jdbcType=INTEGER},
#{fav,jdbcType=BIGINT},
#{viplevel,jdbcType=INTEGER},
#{cancelreason,jdbcType=INTEGER},
#{createdate,jdbcType=BIGINT},
#{lastmoddate,jdbcType=BIGINT},
#{formatter,jdbcType=VARCHAR},
#{status,jdbcType=TINYINT},
update qfd_sketch
courseName = #{coursename,jdbcType=VARCHAR},
courseReq = #{coursereq,jdbcType=VARCHAR},
teaId = #{teaid,jdbcType=VARCHAR},
teaName = #{teaname,jdbcType=VARCHAR},
stuId = #{stuid,jdbcType=VARCHAR},
stuName = #{stuname,jdbcType=VARCHAR},
stuPhone = #{stuphone,jdbcType=VARCHAR},
textbookId = #{textbookid,jdbcType=BIGINT},
creator = #{creator,jdbcType=VARCHAR},
lastModifier = #{lastmodifier,jdbcType=VARCHAR},
countDate = #{countdate,jdbcType=INTEGER},
countTime = #{counttime,jdbcType=INTEGER},
countStu = #{countstu,jdbcType=INTEGER},
sketchType = #{sketchtype,jdbcType=INTEGER},
courseStyle = #{coursestyle,jdbcType=INTEGER},
classType = #{classtype,jdbcType=INTEGER},
process = #{process,jdbcType=INTEGER},
courseStatus = #{coursestatus,jdbcType=INTEGER},
pay = #{pay,jdbcType=VARCHAR},
perPay = #{perpay,jdbcType=VARCHAR},
couponPay = #{couponpay,jdbcType=VARCHAR},
originalPay = #{originalpay,jdbcType=VARCHAR},
couponRule = #{couponrule,jdbcType=INTEGER},
beginDate = #{begindate,jdbcType=BIGINT},
endDate = #{enddate,jdbcType=BIGINT},
courseNum = #{coursenum,jdbcType=INTEGER},
hasNum = #{hasnum,jdbcType=INTEGER},
cancelNum = #{cancelnum,jdbcType=INTEGER},
courseNumInput = #{coursenuminput,jdbcType=INTEGER},
orderNum = #{ordernum,jdbcType=INTEGER},
fav = #{fav,jdbcType=BIGINT},
vipLevel = #{viplevel,jdbcType=INTEGER},
cancelReason = #{cancelreason,jdbcType=INTEGER},
createDate = #{createdate,jdbcType=BIGINT},
lastModDate = #{lastmoddate,jdbcType=BIGINT},
formatter = #{formatter,jdbcType=VARCHAR},
status = #{status,jdbcType=TINYINT},
where id = #{id,jdbcType=BIGINT}
delete from qfd_sketch
where id = #{id,jdbcType=BIGINT}
6.可以通过上面的修改后文件可以看到已经实现了我们的要求,但是sql id还是不满足我们的要求,应该改成纯粹的insert,update,delete
找到IntrospectedTable.java类的calculateXmlAttributes()方法,可以看到很多的setxxx方法带了具体的字符串参数,这个就是对应sql文件里面的sql id我们把对应的改改
protected void calculateXmlAttributes() {
setIbatis2SqlMapPackage(calculateSqlMapPackage());
setIbatis2SqlMapFileName(calculateIbatis2SqlMapFileName());
setMyBatis3XmlMapperFileName(calculateMyBatis3XmlMapperFileName());
setMyBatis3XmlMapperPackage(calculateSqlMapPackage());
setIbatis2SqlMapNamespace(calculateIbatis2SqlMapNamespace());
setMyBatis3FallbackSqlMapNamespace(calculateMyBatis3FallbackSqlMapNamespace());
setSqlMapFullyQualifiedRuntimeTableName(calculateSqlMapFullyQualifiedRuntimeTableName());
setSqlMapAliasedFullyQualifiedRuntimeTableName(calculateSqlMapAliasedFullyQualifiedRuntimeTableName());
setCountByExampleStatementId("countByExample"); //$NON-NLS-1$
setDeleteByExampleStatementId("deleteByExample"); //$NON-NLS-1$
setDeleteByPrimaryKeyStatementId("deleteByPrimaryKey"); //$NON-NLS-1$
setInsertStatementId("insert"); //$NON-NLS-1$
setInsertSelectiveStatementId("insertSelective"); //$NON-NLS-1$
setSelectAllStatementId("selectAll"); //$NON-NLS-1$
setSelectByExampleStatementId("selectByExample"); //$NON-NLS-1$
setSelectByExampleWithBLOBsStatementId("selectByExampleWithBLOBs"); //$NON-NLS-1$
setSelectByPrimaryKeyStatementId("selectByPrimaryKey"); //$NON-NLS-1$
setUpdateByExampleStatementId("updateByExample"); //$NON-NLS-1$
setUpdateByExampleSelectiveStatementId("updateByExampleSelective"); //$NON-NLS-1$
setUpdateByExampleWithBLOBsStatementId("updateByExampleWithBLOBs"); //$NON-NLS-1$
setUpdateByPrimaryKeyStatementId("updateByPrimaryKey"); //$NON-NLS-1$
setUpdateByPrimaryKeySelectiveStatementId("updateByPrimaryKeySelective"); //$NON-NLS-1$
setUpdateByPrimaryKeyWithBLOBsStatementId("updateByPrimaryKeyWithBLOBs"); //$NON-NLS-1$
setBaseResultMapId("BaseResultMap"); //$NON-NLS-1$
setResultMapWithBLOBsId("ResultMapWithBLOBs"); //$NON-NLS-1$
setExampleWhereClauseId("Example_Where_Clause"); //$NON-NLS-1$
setBaseColumnListId("Base_Column_List"); //$NON-NLS-1$
setBlobColumnListId("Blob_Column_List"); //$NON-NLS-1$
setMyBatis3UpdateByExampleWhereClauseId("Update_By_Example_Where_Clause"); //$NON-NLS-1$
}
protected void calculateXmlAttributes() {
setIbatis2SqlMapPackage(calculateSqlMapPackage());
setIbatis2SqlMapFileName(calculateIbatis2SqlMapFileName());
setMyBatis3XmlMapperFileName(calculateMyBatis3XmlMapperFileName());
setMyBatis3XmlMapperPackage(calculateSqlMapPackage());
setIbatis2SqlMapNamespace(calculateIbatis2SqlMapNamespace());
setMyBatis3FallbackSqlMapNamespace(calculateMyBatis3FallbackSqlMapNamespace());
setSqlMapFullyQualifiedRuntimeTableName(calculateSqlMapFullyQualifiedRuntimeTableName());
setSqlMapAliasedFullyQualifiedRuntimeTableName(calculateSqlMapAliasedFullyQualifiedRuntimeTableName());
setCountByExampleStatementId("countByExample"); //$NON-NLS-1$
setDeleteByExampleStatementId("deleteByExample"); //$NON-NLS-1$
// setDeleteByPrimaryKeyStatementId("deleteByPrimaryKey"); //$NON-NLS-1$
setDeleteByPrimaryKeyStatementId("delete"); // deleteByPrimaryKey改成delete
setInsertStatementId("insert"); //$NON-NLS-1$
// setInsertSelectiveStatementId("insertSelective"); //$NON-NLS-1$
setInsertSelectiveStatementId("insert"); // insertSelective改成insert
setSelectAllStatementId("selectAll"); //$NON-NLS-1$
setSelectByExampleStatementId("selectByExample"); //$NON-NLS-1$
setSelectByExampleWithBLOBsStatementId("selectByExampleWithBLOBs"); //$NON-NLS-1$
setSelectByPrimaryKeyStatementId("selectByPrimaryKey"); //$NON-NLS-1$
setUpdateByExampleStatementId("updateByExample"); //$NON-NLS-1$
setUpdateByExampleSelectiveStatementId("updateByExampleSelective"); //$NON-NLS-1$
setUpdateByExampleWithBLOBsStatementId("updateByExampleWithBLOBs"); //$NON-NLS-1$
setUpdateByPrimaryKeyStatementId("updateByPrimaryKey"); //$NON-NLS-1$
// setUpdateByPrimaryKeySelectiveStatementId("updateByPrimaryKeySelective"); //$NON-NLS-1$
setUpdateByPrimaryKeySelectiveStatementId("update"); // updateByPrimaryKeySelective改成update
setUpdateByPrimaryKeyWithBLOBsStatementId("updateByPrimaryKeyWithBLOBs"); //$NON-NLS-1$
setBaseResultMapId("BaseResultMap"); //$NON-NLS-1$
setResultMapWithBLOBsId("ResultMapWithBLOBs"); //$NON-NLS-1$
setExampleWhereClauseId("Example_Where_Clause"); //$NON-NLS-1$
setBaseColumnListId("Base_Column_List"); //$NON-NLS-1$
setBlobColumnListId("Blob_Column_List"); //$NON-NLS-1$
setMyBatis3UpdateByExampleWhereClauseId("Update_By_Example_Where_Clause"); //$NON-NLS-1$
}
7.修改方法后我们再重新生成文件看看效果
id, courseName, courseReq, teaId, teaName, stuId, stuName, stuPhone, textbookId,
creator, lastModifier, countDate, countTime, countStu, sketchType, courseStyle, classType,
process, courseStatus, pay, perPay, couponPay, originalPay, couponRule, beginDate,
endDate, courseNum, hasNum, cancelNum, courseNumInput, orderNum, fav, vipLevel, cancelReason,
createDate, lastModDate, formatter, status
insert into qfd_sketch
id,
courseName,
courseReq,
teaId,
teaName,
stuId,
stuName,
stuPhone,
textbookId,
creator,
lastModifier,
countDate,
countTime,
countStu,
sketchType,
courseStyle,
classType,
process,
courseStatus,
pay,
perPay,
couponPay,
originalPay,
couponRule,
beginDate,
endDate,
courseNum,
hasNum,
cancelNum,
courseNumInput,
orderNum,
fav,
vipLevel,
cancelReason,
createDate,
lastModDate,
formatter,
status,
#{id,jdbcType=BIGINT},
#{coursename,jdbcType=VARCHAR},
#{coursereq,jdbcType=VARCHAR},
#{teaid,jdbcType=VARCHAR},
#{teaname,jdbcType=VARCHAR},
#{stuid,jdbcType=VARCHAR},
#{stuname,jdbcType=VARCHAR},
#{stuphone,jdbcType=VARCHAR},
#{textbookid,jdbcType=BIGINT},
#{creator,jdbcType=VARCHAR},
#{lastmodifier,jdbcType=VARCHAR},
#{countdate,jdbcType=INTEGER},
#{counttime,jdbcType=INTEGER},
#{countstu,jdbcType=INTEGER},
#{sketchtype,jdbcType=INTEGER},
#{coursestyle,jdbcType=INTEGER},
#{classtype,jdbcType=INTEGER},
#{process,jdbcType=INTEGER},
#{coursestatus,jdbcType=INTEGER},
#{pay,jdbcType=VARCHAR},
#{perpay,jdbcType=VARCHAR},
#{couponpay,jdbcType=VARCHAR},
#{originalpay,jdbcType=VARCHAR},
#{couponrule,jdbcType=INTEGER},
#{begindate,jdbcType=BIGINT},
#{enddate,jdbcType=BIGINT},
#{coursenum,jdbcType=INTEGER},
#{hasnum,jdbcType=INTEGER},
#{cancelnum,jdbcType=INTEGER},
#{coursenuminput,jdbcType=INTEGER},
#{ordernum,jdbcType=INTEGER},
#{fav,jdbcType=BIGINT},
#{viplevel,jdbcType=INTEGER},
#{cancelreason,jdbcType=INTEGER},
#{createdate,jdbcType=BIGINT},
#{lastmoddate,jdbcType=BIGINT},
#{formatter,jdbcType=VARCHAR},
#{status,jdbcType=TINYINT},
update qfd_sketch
courseName = #{coursename,jdbcType=VARCHAR},
courseReq = #{coursereq,jdbcType=VARCHAR},
teaId = #{teaid,jdbcType=VARCHAR},
teaName = #{teaname,jdbcType=VARCHAR},
stuId = #{stuid,jdbcType=VARCHAR},
stuName = #{stuname,jdbcType=VARCHAR},
stuPhone = #{stuphone,jdbcType=VARCHAR},
textbookId = #{textbookid,jdbcType=BIGINT},
creator = #{creator,jdbcType=VARCHAR},
lastModifier = #{lastmodifier,jdbcType=VARCHAR},
countDate = #{countdate,jdbcType=INTEGER},
countTime = #{counttime,jdbcType=INTEGER},
countStu = #{countstu,jdbcType=INTEGER},
sketchType = #{sketchtype,jdbcType=INTEGER},
courseStyle = #{coursestyle,jdbcType=INTEGER},
classType = #{classtype,jdbcType=INTEGER},
process = #{process,jdbcType=INTEGER},
courseStatus = #{coursestatus,jdbcType=INTEGER},
pay = #{pay,jdbcType=VARCHAR},
perPay = #{perpay,jdbcType=VARCHAR},
couponPay = #{couponpay,jdbcType=VARCHAR},
originalPay = #{originalpay,jdbcType=VARCHAR},
couponRule = #{couponrule,jdbcType=INTEGER},
beginDate = #{begindate,jdbcType=BIGINT},
endDate = #{enddate,jdbcType=BIGINT},
courseNum = #{coursenum,jdbcType=INTEGER},
hasNum = #{hasnum,jdbcType=INTEGER},
cancelNum = #{cancelnum,jdbcType=INTEGER},
courseNumInput = #{coursenuminput,jdbcType=INTEGER},
orderNum = #{ordernum,jdbcType=INTEGER},
fav = #{fav,jdbcType=BIGINT},
vipLevel = #{viplevel,jdbcType=INTEGER},
cancelReason = #{cancelreason,jdbcType=INTEGER},
createDate = #{createdate,jdbcType=BIGINT},
lastModDate = #{lastmoddate,jdbcType=BIGINT},
formatter = #{formatter,jdbcType=VARCHAR},
status = #{status,jdbcType=TINYINT},
where id = #{id,jdbcType=BIGINT}
delete from qfd_sketch
where id = #{id,jdbcType=BIGINT}
8.已经改成比较符合我们规范和使用的xml文件了,看着很让人舒服不别扭;小结:这些修改代码都是源码生成器必经的逻辑,所以有兴趣继续改造的同学可以继续断点查看并按自己的习惯修改,我只是大概讲了那些部分都是关键地方,下一篇我应该会讲解如何增加自定义sql了,大家有兴趣继续学习的请不要错过咯,再次谢谢大家的支持