由于近日基于Mybatis开发,而Mybatis-generator生成的查询接口又缺少针对其它属性关联查询的办法(或许是我没找到),也没找到相关添加的教程!无奈只好花点时间自己增加一个selectByCdt接口。写出来以便别人有类似需求时可以自行添加。 附近部分是根据本文需求已经改好的Jar包!可以直接下载试用!
为了方便查找改动的相关,我在局部修改的地方加入了//@Nothing-add
改后的源码发布到了 https://code.google.com/p/mybatis-generator-custom/ 需要的自行检出!
下面直接贴出修改的相关类。注意路径前M:修改 A:添加此方法!
M | org/mybatis/generator/api/IntrospectedTable.java
protected enum InternalAttribute { ATTR_DAO_IMPLEMENTATION_TYPE, ATTR_DAO_INTERFACE_TYPE, ATTR_PRIMARY_KEY_TYPE, ATTR_BASE_RECORD_TYPE, ATTR_RECORD_WITH_BLOBS_TYPE, ATTR_EXAMPLE_TYPE, ATTR_IBATIS2_SQL_MAP_PACKAGE, ATTR_IBATIS2_SQL_MAP_FILE_NAME, ATTR_IBATIS2_SQL_MAP_NAMESPACE, ATTR_MYBATIS3_XML_MAPPER_PACKAGE, 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, ATTR_FULLY_QUALIFIED_TABLE_NAME_AT_RUNTIME, ATTR_ALIASED_FULLY_QUALIFIED_TABLE_NAME_AT_RUNTIME, ATTR_COUNT_BY_EXAMPLE_STATEMENT_ID, ATTR_DELETE_BY_EXAMPLE_STATEMENT_ID, ATTR_DELETE_BY_PRIMARY_KEY_STATEMENT_ID, ATTR_INSERT_STATEMENT_ID, ATTR_INSERT_SELECTIVE_STATEMENT_ID, ATTR_SELECT_ALL_STATEMENT_ID, ATTR_SELECT_BY_EXAMPLE_STATEMENT_ID, ATTR_SELECT_BY_EXAMPLE_WITH_BLOBS_STATEMENT_ID, ATTR_SELECT_BY_PRIMARY_KEY_STATEMENT_ID, ATTR_UPDATE_BY_EXAMPLE_STATEMENT_ID, ATTR_UPDATE_BY_EXAMPLE_SELECTIVE_STATEMENT_ID, ATTR_UPDATE_BY_EXAMPLE_WITH_BLOBS_STATEMENT_ID, ATTR_UPDATE_BY_PRIMARY_KEY_STATEMENT_ID, ATTR_UPDATE_BY_PRIMARY_KEY_SELECTIVE_STATEMENT_ID, ATTR_UPDATE_BY_PRIMARY_KEY_WITH_BLOBS_STATEMENT_ID, ATTR_BASE_RESULT_MAP_ID, ATTR_RESULT_MAP_WITH_BLOBS_ID, ATTR_EXAMPLE_WHERE_CLAUSE_ID, ATTR_BASE_COLUMN_LIST_ID, ATTR_BLOB_COLUMN_LIST_ID, ATTR_MYBATIS3_UPDATE_BY_EXAMPLE_WHERE_CLAUSE_ID, ATTR_MYBATIS3_SQL_PROVIDER_TYPE, //@Nothing-add ATTR_SELECT_BY_CDT_STATEMENT_ID }
M | org/mybatis/generator/codegen/mybatis3/javamapper/JavaMapperGenerator.java
@Override public List<CompilationUnit> getCompilationUnits() { progressCallback.startTask(getString("Progress.17", //$NON-NLS-1$ introspectedTable.getFullyQualifiedTable().toString())); CommentGenerator commentGenerator = context.getCommentGenerator(); FullyQualifiedJavaType type = new FullyQualifiedJavaType( introspectedTable.getMyBatis3JavaMapperType()); Interface interfaze = new Interface(type); interfaze.setVisibility(JavaVisibility.PUBLIC); commentGenerator.addJavaFileComment(interfaze); String rootInterface = introspectedTable .getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_INTERFACE); if (!stringHasValue(rootInterface)) { rootInterface = context.getJavaClientGeneratorConfiguration() .getProperty(PropertyRegistry.ANY_ROOT_INTERFACE); } if (stringHasValue(rootInterface)) { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType( rootInterface); interfaze.addSuperInterface(fqjt); interfaze.addImportedType(fqjt); } addCountByExampleMethod(interfaze); addDeleteByExampleMethod(interfaze); addDeleteByPrimaryKeyMethod(interfaze); addInsertMethod(interfaze); addInsertSelectiveMethod(interfaze); addSelectByExampleWithBLOBsMethod(interfaze); addSelectByExampleWithoutBLOBsMethod(interfaze); addSelectByPrimaryKeyMethod(interfaze); //@Nothing-Add addSelectByCdtMethod(interfaze); addUpdateByExampleSelectiveMethod(interfaze); addUpdateByExampleWithBLOBsMethod(interfaze); addUpdateByExampleWithoutBLOBsMethod(interfaze); addUpdateByPrimaryKeySelectiveMethod(interfaze); addUpdateByPrimaryKeyWithBLOBsMethod(interfaze); addUpdateByPrimaryKeyWithoutBLOBsMethod(interfaze); List<CompilationUnit> answer = new ArrayList<CompilationUnit>(); if (context.getPlugins().clientGenerated(interfaze, null, introspectedTable)) { answer.add(interfaze); } List<CompilationUnit> extraCompilationUnits = getExtraCompilationUnits(); if (extraCompilationUnits != null) { answer.addAll(extraCompilationUnits); } return answer; }
//@Nothing-Add protected void addSelectByCdtMethod(Interface interfaze) { //这里我就直接和主键生成绑定吧!懒的写了! if (introspectedTable.getRules().generateSelectByPrimaryKey()) { AbstractJavaMapperMethodGenerator methodGenerator = new SelectByCdtMethodGenerator(false); initializeAndExecuteGenerator(methodGenerator, interfaze); } }
A | org/mybatis/generator/codegen/mybatis3/javamapper/elements/SelectByCdtMethodGenerator.java
public class SelectByCdtMethodGenerator extends AbstractJavaMapperMethodGenerator { private boolean isSimple; public SelectByCdtMethodGenerator(boolean isSimple) { super(); this.isSimple = isSimple; } @Override public void addInterfaceElements(Interface interfaze) { Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>(); Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); FullyQualifiedJavaType parameterType = introspectedTable.getRules() .calculateAllFieldsClass(); FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance().getNewListInstance(); returnType.addTypeArgument(parameterType); method.setReturnType(FullyQualifiedJavaType.getNewListInstance()); importedTypes.add(returnType); importedTypes.add(parameterType); method.setName(introspectedTable.getSelectByCdtStatementId()); method.addParameter(new Parameter(parameterType, "record")); //$NON-NLS-1$ addMapperAnnotations(interfaze, method); context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); if (context.getPlugins().clientSelectByPrimaryKeyMethodGenerated( method, interfaze, introspectedTable)) { interfaze.addImportedTypes(importedTypes); interfaze.addMethod(method); } } public void addMapperAnnotations(Interface interfaze, Method method) { return; } }
M | org/mybatis/generator/codegen/mybatis3/xmlmapper/XMLMapperGenerator.java
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); //@Nothing-add addSelectByCdtElement(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; }
A | org/mybatis/generator/codegen/mybatis3/xmlmapper/elements/SelectByCdtElementGenerator.java
public class SelectByCdtElementGenerator extends AbstractXmlElementGenerator { public SelectByCdtElementGenerator() { super(); } @Override public void addElements(XmlElement parentElement) { XmlElement answer = new XmlElement("select"); //$NON-NLS-1$ answer.addAttribute(new Attribute( "id", introspectedTable.getSelectByCdtStatementId())); //$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 = introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName(); 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())); XmlElement dynamicElement = new XmlElement("where"); //$NON-NLS-1$ answer.addElement(dynamicElement); for (IntrospectedColumn introspectedColumn : introspectedTable.getBaseColumns()) { XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$ sb.setLength(0); sb.append(introspectedColumn.getJavaProperty()); //$NON-NLS-1$ sb.append(" != null"); //$NON-NLS-1$ isNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$ dynamicElement.addElement(isNotNullElement); sb.setLength(0); sb.append("and "); sb.append(MyBatis3FormattingUtilities .getAliasedEscapedColumnName(introspectedColumn)); sb.append(" = "); //$NON-NLS-1$ sb.append(MyBatis3FormattingUtilities.getParameterClause( introspectedColumn)); //$NON-NLS-1$ // sb.append(','); isNotNullElement.addElement(new TextElement(sb.toString())); } if (context.getPlugins() .sqlMapSelectByPrimaryKeyElementGenerated(answer, introspectedTable)) { parentElement.addElement(answer); } } }
相关推荐
1. **mybatis-generator-core-1.3.2-javadoc.jar**:这是MyBatis Generator的API文档,包含了所有类和方法的详细说明,有助于开发者理解和使用该工具。 2. **mysql-connector-java-5.1.25-bin.jar**:这是MySQL的...
MyBatis Generator (MBG) 是一个强大的工具,用于自动生成 MyBatis 映射文件、Java 模型类和 DAO 接口。这个工具极大地减少了手动编写这些常见代码的工作量,使开发者能够专注于业务逻辑的实现。"mybatis-generator-...
mybatis-generator-config_1_0.dtd文件存在于mybatis-generator-core-1.3.2.jar包中,路径如下org/mybatis/generator/config/xml/mybatis-generator-config_1_0.dtd 可以设置开发工具的dtd配置,配置...
总的来说,MyBatis逆向工程通过`mybatis-generator-core-1.3.2.jar`提供了自动化代码生成的能力,简化了开发流程,降低了出错的可能性。开发者只需维护好配置文件,就能随着数据库的变化自动更新相关的Java代码,...
MyBatis Generator(MBG)是一款强大的工具,用于自动生成MyBatis的实体类、DAO接口和对应的XML映射文件。在Java开发中,MyBatis Generator(简称MBG)可以显著提高开发效率,减少手动编写重复代码的工作量。这里...
这个"mybatis-generator-core-1.3.2.jar"文件是Mybatis Generator的核心库,版本为1.3.2,它包含了执行逆向工程所需的所有功能。 Mybatis Generator的主要目标是通过数据库中的数据表来生成Java实体类(PO,即Plain...
MyBatis Generator (MBG) 是一个强大的工具,它能够自动生成MyBatis映射器接口、XML映射文件以及实体类,极大地提高了开发效率。在`mybatis-generator-core-1.3.2`版本中,我们关注的是如何通过数据库中的字段注释...
深入理解MyBatis Generator的工作原理,可以帮助我们更好地定制生成代码的格式,甚至扩展其功能,例如添加新的插件来支持更复杂的生成逻辑。此外,逆向工程也有助于我们了解MyBatis框架与数据库交互的底层机制,提升...
MyBatis Generator (MBG) 是一款强大的工具,它能够帮助开发者自动生成MyBatis相关的Java源代码,包括DAO层、实体类以及XML映射文件等。`mybatis-generator-core-1.3.2.zip` 文件是MyBatis Generator的一个版本,即...
使用方法:解压缩之后修改XML文件(如何改可以百度,就是换jdbc参数以及你要生成的dao,mapper跟model的名字及所在位置)然后windos下cmd然后cd到你放置解压文件的位置执行命令:java -jar mybatis-generator-core-...
MyBatis Generator Core 1.3.2 是一个强大的工具,用于自动生成MyBatis的映射文件、Java模型类以及DAO接口。这个版本的工具是MyBatis框架的一个重要组成部分,它极大地简化了开发过程,减少了手动编写SQL映射和Java...
在本案例中,我们关注的是`mybatis-generator-core-1.3.2-bundle.zip`这个压缩包,其中包含的是MyBatis Generator的核心库版本1.3.2。 MyBatis Generator的核心功能主要体现在以下几个方面: 1. **配置文件**:MBG...
mybatis-generator-core-1.3.2代码自动生成 官网下载解压即用 内含 mybatis-3.2.6.jar/mybatis-generator-core-1.3.2.jar/mysql-connector-java-5.1.30.jar/mybatis-generator-core-1.3.2-javadoc.jar/mybatis-...
1. **主类**:`org.mybatis.generator.api.MyBatisGenerator` 是 MBG 的核心类,负责执行代码生成过程。你可以通过调用它的 `generate()` 方法并传入配置信息来启动生成。 2. **配置文件模板**:通常命名为 `...
在标题 "mybatis-generator-core-1.3.2-生成中文注释" 中提到的是 MBG 的一个特定版本,该版本优化了对中文注释的支持。在描述中提到,这个版本能够读取并生成数据库中的中文注释,而原版可能仅处理英文字段名和表名...