今天我教大家如何在mapper.xml文件里增加我们自定义的sql节点,还是老规矩沿用之前的项目用例,如有看不懂的同学可以翻看下上几篇的教程
1.打开IntrospectedTable.java找到enum InternalAttribute这个枚举定义增加一行我们的sql id ATTR_SELECT_BY_CONDITION_STATEMENT_ID
protected enum InternalAttribute { /** The attr dao implementation type. */ ATTR_DAO_IMPLEMENTATION_TYPE, /** The attr dao interface type. */ ATTR_DAO_INTERFACE_TYPE, /** The attr primary key type. */ ATTR_PRIMARY_KEY_TYPE, /** The attr base record type. */ ATTR_BASE_RECORD_TYPE, /** The attr record with blobs type. */ ATTR_RECORD_WITH_BLOBS_TYPE, /** The attr example type. */ ATTR_EXAMPLE_TYPE, /** The ATT r_ ibati s2_ sq l_ ma p_ package. */ ATTR_IBATIS2_SQL_MAP_PACKAGE, /** The ATT r_ ibati s2_ sq l_ ma p_ fil e_ name. */ ATTR_IBATIS2_SQL_MAP_FILE_NAME, /** The ATT r_ ibati s2_ sq l_ ma p_ namespace. */ ATTR_IBATIS2_SQL_MAP_NAMESPACE, /** The ATT r_ mybati s3_ xm l_ mappe r_ package. */ ATTR_MYBATIS3_XML_MAPPER_PACKAGE, /** The ATT r_ mybati s3_ xm l_ mappe r_ fil e_ name. */ ATTR_MYBATIS3_XML_MAPPER_FILE_NAME, /** also used as XML Mapper namespace if a Java mapper is generated. */ ATTR_MYBATIS3_JAVA_MAPPER_TYPE, /** used as XML Mapper namespace if no client is generated. */ ATTR_MYBATIS3_FALLBACK_SQL_MAP_NAMESPACE, /** The attr fully qualified table name at runtime. */ ATTR_FULLY_QUALIFIED_TABLE_NAME_AT_RUNTIME, /** The attr aliased fully qualified table name at runtime. */ ATTR_ALIASED_FULLY_QUALIFIED_TABLE_NAME_AT_RUNTIME, /** The attr count by example statement id. */ ATTR_COUNT_BY_EXAMPLE_STATEMENT_ID, /** The attr delete by example statement id. */ ATTR_DELETE_BY_EXAMPLE_STATEMENT_ID, /** The attr delete by primary key statement id. */ ATTR_DELETE_BY_PRIMARY_KEY_STATEMENT_ID, /** The attr insert statement id. */ ATTR_INSERT_STATEMENT_ID, /** The attr insert selective statement id. */ ATTR_INSERT_SELECTIVE_STATEMENT_ID, /** The attr select all statement id. */ ATTR_SELECT_ALL_STATEMENT_ID, /** The attr select by example statement id. */ ATTR_SELECT_BY_EXAMPLE_STATEMENT_ID, /** The attr select by example with blobs statement id. */ ATTR_SELECT_BY_EXAMPLE_WITH_BLOBS_STATEMENT_ID, /** The attr select by primary key statement id. */ ATTR_SELECT_BY_PRIMARY_KEY_STATEMENT_ID, /** The attr update by example statement id. */ ATTR_UPDATE_BY_EXAMPLE_STATEMENT_ID, /** The attr update by example selective statement id. */ ATTR_UPDATE_BY_EXAMPLE_SELECTIVE_STATEMENT_ID, /** The attr update by example with blobs statement id. */ ATTR_UPDATE_BY_EXAMPLE_WITH_BLOBS_STATEMENT_ID, /** The attr update by primary key statement id. */ ATTR_UPDATE_BY_PRIMARY_KEY_STATEMENT_ID, /** The attr update by primary key selective statement id. */ ATTR_UPDATE_BY_PRIMARY_KEY_SELECTIVE_STATEMENT_ID, /** The attr update by primary key with blobs statement id. */ ATTR_UPDATE_BY_PRIMARY_KEY_WITH_BLOBS_STATEMENT_ID, /** The attr base result map id. */ ATTR_BASE_RESULT_MAP_ID, /** The attr result map with blobs id. */ ATTR_RESULT_MAP_WITH_BLOBS_ID, /** The attr example where clause id. */ ATTR_EXAMPLE_WHERE_CLAUSE_ID, /** The attr base column list id. */ ATTR_BASE_COLUMN_LIST_ID, /** The attr blob column list id. */ ATTR_BLOB_COLUMN_LIST_ID, /** The ATT r_ mybati s3_ updat e_ b y_ exampl e_ wher e_ claus e_ id. */ ATTR_MYBATIS3_UPDATE_BY_EXAMPLE_WHERE_CLAUSE_ID, /** The ATT r_ mybati s3_ sq l_ provide r_ type. */ ATTR_MYBATIS3_SQL_PROVIDER_TYPE, ATTR_SELECT_BY_CONDITION_STATEMENT_ID // 通过条件查询SQL ID }
2.IntrospectedTable.java增加sql id的set,get方法用于之后的读取操作
public void setSelectByConditionStatementId(String s) { internalAttributes.put(InternalAttribute.ATTR_SELECT_BY_CONDITION_STATEMENT_ID, s); } public String getSelectByConditionStatementId() { return internalAttributes .get(InternalAttribute.ATTR_SELECT_BY_CONDITION_STATEMENT_ID); }
3.新建一个xml sql生成的实现类SelectByConditionElementGenerator.java
package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements; import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.XmlElement; import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities; /** * * @author Jeff Butler * */ public class SelectByConditionElementGenerator extends AbstractXmlElementGenerator { public SelectByConditionElementGenerator() { super(); } @Override public void addElements(XmlElement parentElement) { XmlElement answer = new XmlElement("select"); //$NON-NLS-1$ answer.addAttribute(new Attribute( "id", introspectedTable.getSelectByConditionStatementId())); //$NON-NLS-1$ if (introspectedTable.getRules().generateResultMapWithBLOBs()) { answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$ introspectedTable.getResultMapWithBLOBsId())); } else { answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$ introspectedTable.getBaseResultMapId())); } String parameterType; if (introspectedTable.getRules().generatePrimaryKeyClass()) { parameterType = introspectedTable.getPrimaryKeyType(); } else { // PK fields are in the base class. If more than on PK // field, then they are coming in a map. if (introspectedTable.getPrimaryKeyColumns().size() > 1) { parameterType = "map"; //$NON-NLS-1$ } else { parameterType = introspectedTable.getPrimaryKeyColumns().get(0) .getFullyQualifiedJavaType().toString(); } } answer.addAttribute(new Attribute("parameterType", //$NON-NLS-1$ parameterType)); context.getCommentGenerator().addComment(answer); StringBuilder sb = new StringBuilder(); sb.append("select "); //$NON-NLS-1$ if (stringHasValue(introspectedTable .getSelectByPrimaryKeyQueryId())) { sb.append('\''); sb.append(introspectedTable.getSelectByPrimaryKeyQueryId()); sb.append("' as QUERYID,"); //$NON-NLS-1$ } answer.addElement(new TextElement(sb.toString())); answer.addElement(getBaseColumnListElement()); if (introspectedTable.hasBLOBColumns()) { answer.addElement(new TextElement(",")); //$NON-NLS-1$ answer.addElement(getBlobColumnListElement()); } sb.setLength(0); sb.append("from "); //$NON-NLS-1$ sb.append(introspectedTable .getAliasedFullyQualifiedTableNameAtRuntime()); answer.addElement(new TextElement(sb.toString())); boolean and = false; for (IntrospectedColumn introspectedColumn : introspectedTable .getPrimaryKeyColumns()) { sb.setLength(0); if (and) { sb.append(" and "); //$NON-NLS-1$ } else { sb.append("where "); //$NON-NLS-1$ and = true; } sb.append(MyBatis3FormattingUtilities .getAliasedEscapedColumnName(introspectedColumn)); sb.append(" = "); //$NON-NLS-1$ sb.append(MyBatis3FormattingUtilities .getParameterClause(introspectedColumn)); answer.addElement(new TextElement(sb.toString())); } parentElement.addElement(answer); } }
4.在XMLMapperGenerator.java中编写一个调用我们上面写的实现类方法
protected void addSelectByConditionElement(XmlElement parentElement) { if (introspectedTable.getRules().generateSelectByPrimaryKey()) { AbstractXmlElementGenerator elementGenerator = new SelectByConditionElementGenerator(); initializeAndExecuteGenerator(elementGenerator, parentElement); } }
5.在我们之前修改自定义xml sql id的地方增加调用,XMLMapperGenerator.java的getSqlMapElement()方法修改
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); addSelectByConditionElement(answer); // 增加自定义SQL return answer; }
6.自定义sql的代码已经差不多这样了,根据之前的示例我们这次改造的地方其实不是很多,但是很实用;对于这个sql的拼写我只是简单的实现一个select all语句,更加复杂的你可以直接修改SelectByConditionElementGenerator.java实现类
下面我们可以看看执行成功后的结果,可以看到最下面多了一个select的语句
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.test.dao.QfdSketchMapper"> <resultMap id="BaseResultMap" type="com.test.domain.QfdSketch"> <!-- WARNING - @mbg.generated This element is automatically generated by MyBatis Generator, do not modify. --> <id column="id" jdbcType="BIGINT" property="id" /> <result column="courseName" jdbcType="VARCHAR" property="coursename" /> <result column="courseReq" jdbcType="VARCHAR" property="coursereq" /> <result column="teaId" jdbcType="VARCHAR" property="teaid" /> <result column="teaName" jdbcType="VARCHAR" property="teaname" /> <result column="stuId" jdbcType="VARCHAR" property="stuid" /> <result column="stuName" jdbcType="VARCHAR" property="stuname" /> <result column="stuPhone" jdbcType="VARCHAR" property="stuphone" /> <result column="textbookId" jdbcType="BIGINT" property="textbookid" /> <result column="creator" jdbcType="VARCHAR" property="creator" /> <result column="lastModifier" jdbcType="VARCHAR" property="lastmodifier" /> <result column="countDate" jdbcType="INTEGER" property="countdate" /> <result column="countTime" jdbcType="INTEGER" property="counttime" /> <result column="countStu" jdbcType="INTEGER" property="countstu" /> <result column="sketchType" jdbcType="INTEGER" property="sketchtype" /> <result column="courseStyle" jdbcType="INTEGER" property="coursestyle" /> <result column="classType" jdbcType="INTEGER" property="classtype" /> <result column="process" jdbcType="INTEGER" property="process" /> <result column="courseStatus" jdbcType="INTEGER" property="coursestatus" /> <result column="pay" jdbcType="VARCHAR" property="pay" /> <result column="perPay" jdbcType="VARCHAR" property="perpay" /> <result column="couponPay" jdbcType="VARCHAR" property="couponpay" /> <result column="originalPay" jdbcType="VARCHAR" property="originalpay" /> <result column="couponRule" jdbcType="INTEGER" property="couponrule" /> <result column="beginDate" jdbcType="BIGINT" property="begindate" /> <result column="endDate" jdbcType="BIGINT" property="enddate" /> <result column="courseNum" jdbcType="INTEGER" property="coursenum" /> <result column="hasNum" jdbcType="INTEGER" property="hasnum" /> <result column="cancelNum" jdbcType="INTEGER" property="cancelnum" /> <result column="courseNumInput" jdbcType="INTEGER" property="coursenuminput" /> <result column="orderNum" jdbcType="INTEGER" property="ordernum" /> <result column="fav" jdbcType="BIGINT" property="fav" /> <result column="vipLevel" jdbcType="INTEGER" property="viplevel" /> <result column="cancelReason" jdbcType="INTEGER" property="cancelreason" /> <result column="createDate" jdbcType="BIGINT" property="createdate" /> <result column="lastModDate" jdbcType="BIGINT" property="lastmoddate" /> <result column="formatter" jdbcType="VARCHAR" property="formatter" /> <result column="status" jdbcType="TINYINT" property="status" /> </resultMap> <sql id="Base_Column_List"> <!-- WARNING - @mbg.generated This element is automatically generated by MyBatis Generator, do not modify. --> 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 </sql> <insert id="insert" parameterType="com.test.domain.QfdSketch"> <!-- WARNING - @mbg.generated This element is automatically generated by MyBatis Generator, do not modify. --> insert into qfd_sketch <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="coursename != null"> courseName, </if> <if test="coursereq != null"> courseReq, </if> <if test="teaid != null"> teaId, </if> <if test="teaname != null"> teaName, </if> <if test="stuid != null"> stuId, </if> <if test="stuname != null"> stuName, </if> <if test="stuphone != null"> stuPhone, </if> <if test="textbookid != null"> textbookId, </if> <if test="creator != null"> creator, </if> <if test="lastmodifier != null"> lastModifier, </if> <if test="countdate != null"> countDate, </if> <if test="counttime != null"> countTime, </if> <if test="countstu != null"> countStu, </if> <if test="sketchtype != null"> sketchType, </if> <if test="coursestyle != null"> courseStyle, </if> <if test="classtype != null"> classType, </if> <if test="process != null"> process, </if> <if test="coursestatus != null"> courseStatus, </if> <if test="pay != null"> pay, </if> <if test="perpay != null"> perPay, </if> <if test="couponpay != null"> couponPay, </if> <if test="originalpay != null"> originalPay, </if> <if test="couponrule != null"> couponRule, </if> <if test="begindate != null"> beginDate, </if> <if test="enddate != null"> endDate, </if> <if test="coursenum != null"> courseNum, </if> <if test="hasnum != null"> hasNum, </if> <if test="cancelnum != null"> cancelNum, </if> <if test="coursenuminput != null"> courseNumInput, </if> <if test="ordernum != null"> orderNum, </if> <if test="fav != null"> fav, </if> <if test="viplevel != null"> vipLevel, </if> <if test="cancelreason != null"> cancelReason, </if> <if test="createdate != null"> createDate, </if> <if test="lastmoddate != null"> lastModDate, </if> <if test="formatter != null"> formatter, </if> <if test="status != null"> status, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=BIGINT}, </if> <if test="coursename != null"> #{coursename,jdbcType=VARCHAR}, </if> <if test="coursereq != null"> #{coursereq,jdbcType=VARCHAR}, </if> <if test="teaid != null"> #{teaid,jdbcType=VARCHAR}, </if> <if test="teaname != null"> #{teaname,jdbcType=VARCHAR}, </if> <if test="stuid != null"> #{stuid,jdbcType=VARCHAR}, </if> <if test="stuname != null"> #{stuname,jdbcType=VARCHAR}, </if> <if test="stuphone != null"> #{stuphone,jdbcType=VARCHAR}, </if> <if test="textbookid != null"> #{textbookid,jdbcType=BIGINT}, </if> <if test="creator != null"> #{creator,jdbcType=VARCHAR}, </if> <if test="lastmodifier != null"> #{lastmodifier,jdbcType=VARCHAR}, </if> <if test="countdate != null"> #{countdate,jdbcType=INTEGER}, </if> <if test="counttime != null"> #{counttime,jdbcType=INTEGER}, </if> <if test="countstu != null"> #{countstu,jdbcType=INTEGER}, </if> <if test="sketchtype != null"> #{sketchtype,jdbcType=INTEGER}, </if> <if test="coursestyle != null"> #{coursestyle,jdbcType=INTEGER}, </if> <if test="classtype != null"> #{classtype,jdbcType=INTEGER}, </if> <if test="process != null"> #{process,jdbcType=INTEGER}, </if> <if test="coursestatus != null"> #{coursestatus,jdbcType=INTEGER}, </if> <if test="pay != null"> #{pay,jdbcType=VARCHAR}, </if> <if test="perpay != null"> #{perpay,jdbcType=VARCHAR}, </if> <if test="couponpay != null"> #{couponpay,jdbcType=VARCHAR}, </if> <if test="originalpay != null"> #{originalpay,jdbcType=VARCHAR}, </if> <if test="couponrule != null"> #{couponrule,jdbcType=INTEGER}, </if> <if test="begindate != null"> #{begindate,jdbcType=BIGINT}, </if> <if test="enddate != null"> #{enddate,jdbcType=BIGINT}, </if> <if test="coursenum != null"> #{coursenum,jdbcType=INTEGER}, </if> <if test="hasnum != null"> #{hasnum,jdbcType=INTEGER}, </if> <if test="cancelnum != null"> #{cancelnum,jdbcType=INTEGER}, </if> <if test="coursenuminput != null"> #{coursenuminput,jdbcType=INTEGER}, </if> <if test="ordernum != null"> #{ordernum,jdbcType=INTEGER}, </if> <if test="fav != null"> #{fav,jdbcType=BIGINT}, </if> <if test="viplevel != null"> #{viplevel,jdbcType=INTEGER}, </if> <if test="cancelreason != null"> #{cancelreason,jdbcType=INTEGER}, </if> <if test="createdate != null"> #{createdate,jdbcType=BIGINT}, </if> <if test="lastmoddate != null"> #{lastmoddate,jdbcType=BIGINT}, </if> <if test="formatter != null"> #{formatter,jdbcType=VARCHAR}, </if> <if test="status != null"> #{status,jdbcType=TINYINT}, </if> </trim> </insert> <update id="update" parameterType="com.test.domain.QfdSketch"> <!-- WARNING - @mbg.generated This element is automatically generated by MyBatis Generator, do not modify. --> update qfd_sketch <set> <if test="coursename != null"> courseName = #{coursename,jdbcType=VARCHAR}, </if> <if test="coursereq != null"> courseReq = #{coursereq,jdbcType=VARCHAR}, </if> <if test="teaid != null"> teaId = #{teaid,jdbcType=VARCHAR}, </if> <if test="teaname != null"> teaName = #{teaname,jdbcType=VARCHAR}, </if> <if test="stuid != null"> stuId = #{stuid,jdbcType=VARCHAR}, </if> <if test="stuname != null"> stuName = #{stuname,jdbcType=VARCHAR}, </if> <if test="stuphone != null"> stuPhone = #{stuphone,jdbcType=VARCHAR}, </if> <if test="textbookid != null"> textbookId = #{textbookid,jdbcType=BIGINT}, </if> <if test="creator != null"> creator = #{creator,jdbcType=VARCHAR}, </if> <if test="lastmodifier != null"> lastModifier = #{lastmodifier,jdbcType=VARCHAR}, </if> <if test="countdate != null"> countDate = #{countdate,jdbcType=INTEGER}, </if> <if test="counttime != null"> countTime = #{counttime,jdbcType=INTEGER}, </if> <if test="countstu != null"> countStu = #{countstu,jdbcType=INTEGER}, </if> <if test="sketchtype != null"> sketchType = #{sketchtype,jdbcType=INTEGER}, </if> <if test="coursestyle != null"> courseStyle = #{coursestyle,jdbcType=INTEGER}, </if> <if test="classtype != null"> classType = #{classtype,jdbcType=INTEGER}, </if> <if test="process != null"> process = #{process,jdbcType=INTEGER}, </if> <if test="coursestatus != null"> courseStatus = #{coursestatus,jdbcType=INTEGER}, </if> <if test="pay != null"> pay = #{pay,jdbcType=VARCHAR}, </if> <if test="perpay != null"> perPay = #{perpay,jdbcType=VARCHAR}, </if> <if test="couponpay != null"> couponPay = #{couponpay,jdbcType=VARCHAR}, </if> <if test="originalpay != null"> originalPay = #{originalpay,jdbcType=VARCHAR}, </if> <if test="couponrule != null"> couponRule = #{couponrule,jdbcType=INTEGER}, </if> <if test="begindate != null"> beginDate = #{begindate,jdbcType=BIGINT}, </if> <if test="enddate != null"> endDate = #{enddate,jdbcType=BIGINT}, </if> <if test="coursenum != null"> courseNum = #{coursenum,jdbcType=INTEGER}, </if> <if test="hasnum != null"> hasNum = #{hasnum,jdbcType=INTEGER}, </if> <if test="cancelnum != null"> cancelNum = #{cancelnum,jdbcType=INTEGER}, </if> <if test="coursenuminput != null"> courseNumInput = #{coursenuminput,jdbcType=INTEGER}, </if> <if test="ordernum != null"> orderNum = #{ordernum,jdbcType=INTEGER}, </if> <if test="fav != null"> fav = #{fav,jdbcType=BIGINT}, </if> <if test="viplevel != null"> vipLevel = #{viplevel,jdbcType=INTEGER}, </if> <if test="cancelreason != null"> cancelReason = #{cancelreason,jdbcType=INTEGER}, </if> <if test="createdate != null"> createDate = #{createdate,jdbcType=BIGINT}, </if> <if test="lastmoddate != null"> lastModDate = #{lastmoddate,jdbcType=BIGINT}, </if> <if test="formatter != null"> formatter = #{formatter,jdbcType=VARCHAR}, </if> <if test="status != null"> status = #{status,jdbcType=TINYINT}, </if> </set> where id = #{id,jdbcType=BIGINT} </update> <delete id="delete" parameterType="java.lang.Long"> <!-- WARNING - @mbg.generated This element is automatically generated by MyBatis Generator, do not modify. --> delete from qfd_sketch where id = #{id,jdbcType=BIGINT} </delete> <select id="select" parameterType="java.lang.Long" resultMap="BaseResultMap"> <!-- WARNING - @mbg.generated This element is automatically generated by MyBatis Generator, do not modify. --> select <include refid="Base_Column_List" /> from qfd_sketch where id = #{id,jdbcType=BIGINT} </select> </mapper>
7.小结,我们自定义的sql作用范围很大,所以这个功能必须要改造的,sql增加了就要相应的增加dao类的方法名,可以参考之前的加成添加即可