今天我教大家如何在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.getPrimaryKey