Mybatis-generator修改源代码实现自定义方法,返回List对象(三)
来源:互联网 发布:sql with ur 编辑:程序博客网 时间:2024/06/03 15:08
前两篇文章我们讲了如何获取源代码即创建工程、修改源代码为dao(mapper)层添加一个方法,那么这一篇,我们来讲如何在xml添加这个方法所需要sql
3、实现XML文件添加Dao(Mapper)层的实现
前面有讲过,下图中的两个包,分别是管理dao(Mapper)文件和xml文件的,
之前我们已经介绍了javamapper这个包了,而且在dao层添加了方法,那么我们现在来讲xmlmapper这个包,跟前面的很像,这个包下面有个XMLMapperGenerator类,里头有个getSqlMapElement方法,它也有很多的addxxx的方法,
而这些方法里头调用的,是org.mybatis.generator.codegen.mybatis3.xmlmapper.elements包下面类的对象
如果第二篇的对象你已经完成了,那么这里要做的,其实和第二篇差不多了,
我们先在elements包里,用SelectByPrimaryKeyElementGenerator类复制出来一个,类名叫SelectByObjectElementGenerator,这个类里的内容,我们等会儿再来修改,
先去XMLMapperGenerator类添加一个方法
//自定义方法 protected void addSelectByObject(XmlElement parentElement) { if (introspectedTable.getRules().generateSelectByPrimaryKey()) { AbstractXmlElementGenerator elementGenerator = new SelectByObjectElementGenerator(); initializeAndExecuteGenerator(elementGenerator, parentElement); } }
然后在该类的getSqlMapElement方法中调用这个方法
那么这样做完以后,理论上新生成的xml文件,就会多出一个了,但是里头面的sql依然是SelectByPrimaryKey方法的sql,接下去,我们就去修改SelectByObjectElementGenerator类的addElements方法,将其改为我们所需要的sql。
@Override public void addElements(XmlElement parentElement) { //先创建一个select标签 XmlElement answer = new XmlElement("select"); //$NON-NLS-1$ //设置该select标签的id,正式第二篇里在枚举中设置的值 answer.addAttribute(new Attribute("id", introspectedTable.getSelectByObject())); //添加parameterType String parameterType; if (introspectedTable.getRules().generateRecordWithBLOBsClass()) { parameterType = introspectedTable.getRecordWithBLOBsType(); } else { parameterType = introspectedTable.getBaseRecordType(); } answer.addAttribute(new Attribute("parameterType", parameterType)); //设置resultMap为BaseResultMap answer.addAttribute(new Attribute("resultMap",introspectedTable.getBaseResultMapId())); context.getCommentGenerator().addComment(answer); //接下去是拼接我们的sql StringBuilder sb = new StringBuilder(); sb.append("select "); Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns().iterator(); //循环插入字段名称 while (iter.hasNext()) { sb.append(MyBatis3FormattingUtilities.getSelectListPhrase(iter.next())); if (iter.hasNext()) { sb.append(", "); } if (sb.length() > 80) { answer.addElement(new TextElement(sb.toString())); sb.setLength(0); } } if (sb.length() > 0) { answer.addElement((new TextElement(sb.toString()))); } sb.setLength(0); sb.append("from "); sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()); answer.addElement(new TextElement(sb.toString())); //在这里添加where条件 XmlElement selectTrimElement = new XmlElement("trim"); //设置trim标签 selectTrimElement.addAttribute(new Attribute("prefix", "where")); selectTrimElement.addAttribute(new Attribute("prefixOverrides", "and")); //添加where和and answer.addElement(selectTrimElement); //循环所有的列 for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) { XmlElement selectNotNullElement = new XmlElement("if"); //$NON-NLS-1$ sb.setLength(0); sb.append(introspectedColumn.getJavaProperty()); sb.append(" != null "); selectNotNullElement.addAttribute(new Attribute("test", sb.toString())); sb.setLength(0); sb.append(" and "); //添加and sb.append(MyBatis3FormattingUtilities .getEscapedColumnName(introspectedColumn)); sb.append(" = "); //添加等号 sb.append(MyBatis3FormattingUtilities .getParameterClause(introspectedColumn)); selectNotNullElement.addElement(new TextElement(sb.toString())); selectTrimElement.addElement(selectNotNullElement); } // String orderByClause = introspectedTable.getTableConfigurationProperty(PropertyRegistry.TABLE_SELECT_ALL_ORDER_BY_CLAUSE); boolean hasOrderBy = StringUtility.stringHasValue(orderByClause); if (hasOrderBy) { sb.setLength(0); sb.append("order by "); //$NON-NLS-1$ sb.append(orderByClause); answer.addElement(new TextElement(sb.toString())); } if (context.getPlugins().sqlMapSelectAllElementGenerated( answer, introspectedTable)) { parentElement.addElement(answer); } }
修改完成以后,保存,执行MyTestRun,再查看xml文件,就会看到
这样,我们在使用dao(Mapper)层的SelectByObject方法的时候,就会调用这条sql了,
那么我们的目的就达到了,欢迎留言共同探讨
- Mybatis-generator修改源代码实现自定义方法,返回List对象(三)
- Mybatis-generator修改源代码实现自定义方法,返回List对象(一)
- Mybatis-generator修改源代码实现自定义方法,返回List对象(二)
- mybatis-generator 源代码修改
- mybatis generator插件自定义修改
- MyBatis Generator系列(三)----修改源码实现中文注释
- MyBatis-generator-core 生成的Dao 怎么自定义名字 (或者手动修改哪些源码可以实现)
- 修改mybatis-generator-1.3.2源码实现自定义代码生成详解
- 自定义MyBatis返回Map对象
- mybatis三剑客之一 mybatis-generator自动生成数据对象
- mybatis generator 自定义类名(修改mybatis-generator-1.3.2源码改dao类名)
- mybatis返回HashMap<Long,List<自定义类>>
- MyBatis Generator系列(八)----MyBatis Generator自定义插件实现自定义Mapper
- Intellij IDEA 2016学习系列之(三)修改mybatis-generator源码生成中文注释
- 自定义MyBatis Generator Plugin
- Mybatis返回list排序无效问题解决方法
- 修改mybatis-generator-1.3.2源码实现中文注释
- 修改mybatis-generator-core1.3.2源码实现注释等功能
- 。net 后台将List数据转换为json后 前台js 调用
- 翻转字符串算法
- Spring IoC 学习(3)
- 第四章——文本输入和委托模式
- C++中的访问运算符(.和->)
- Mybatis-generator修改源代码实现自定义方法,返回List对象(三)
- 有用的pycharm快捷键收藏
- Tomcat 6 JNDI数据源详解
- FX3/CX3 yuv/rgb输出修改
- STM32-重定向C库printf 格式化输出符号详细说明
- 软件版本中的Alpha,Beta,RC,Trial是什么意思?
- CentOS7.2搭建ownCloud私有云
- SpringBoot学习教程
- Web开发手册-w3resource