上一篇文章主要是讲了mybatis-generator-core-1.3.2.jar的使用,这一篇我要介绍的是,修改jar包代码,实现生成自定义模板。 1.我们从这里可以下载mybatis-generator-core-1.3.2.jar项目源码 h ...
上一篇文章主要是讲了mybatis-generator-core-1.3.2.jar的使用,这一篇我要介绍的是,修改jar包代码,实现生成自定义模板。
1.我们从这里可以下载mybatis-generator-core-1.3.2.jar项目源码 http://maven.outofmemory.cn/org.mybatis.generator/mybatis-generator-core/1.3.2/
2.在eclipse下导入存在的maven项目,File->Import
选择项目源码位置,点finish完成导入。
项目目录结构大概这样子。
3.下面我逆工程要生成的mapping和
4.开始修改,首先说明一下各目录
最底边的tse包是我自定义的包,里面是个主类,测试生成的代码是否达到预期标准。
由于这个架包是老外写的,生成的代码风格和我们不大一一样,如果你想修改代码格式,建议你看一下菠萝大象的文章,我这里就不讲代码格式了。
http://www.blogjava.net/bolo/archive/2015/03/20/423683.html
首先,我们先修改逆工程要生成的接口文件mapping的代码,默认情况下有增删改查,我们讲其中一个改方法update吧
比如 我要让生成的mapping中有这样的一个方法 void update(Map<String, Object> dataMap);
就修改org.mybatis.generator.codegen.mybatis3.javamapper.elements包下的UpdateByPrimaryKeyWithoutBLOBsMethodGenerator类,如下:
/* * Copyright 2009 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.mybatis.generator.codegen.mybatis3.javamapper.elements;import java.util.Set;import java.util.TreeSet;import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;import org.mybatis.generator.api.dom.java.Interface;import org.mybatis.generator.api.dom.java.JavaVisibility;import org.mybatis.generator.api.dom.java.Method;import org.mybatis.generator.api.dom.java.Parameter;/** * * @author Jeff Butler * */public class UpdateByPrimaryKeyWithoutBLOBsMethodGenerator extends AbstractJavaMapperMethodGenerator { public UpdateByPrimaryKeyWithoutBLOBsMethodGenerator() { super(); } @Override public void addInterfaceElements(Interface interfaze) { Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>(); FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType( introspectedTable.getBaseRecordType()); importedTypes.add(parameterType); //新增一个方法 Method method = new Method(); //添加方法修饰符PUBLIC method.setVisibility(JavaVisibility.PUBLIC); //设置返回值,这里我用的是自定义的void,无返回值方法 getVoidInstance() //FullyQualifiedJavaType类中可以自定义返回值方法,大家可以自己进去添加 //不想那么麻烦的话,可以 new FullyQualifiedJavaType("void") , 构造函数写上返回类型就行了 method.setReturnType(FullyQualifiedJavaType.getVoidInstance()); //设置方法名,同样可以自己进去看 method.setName(introspectedTable.getUpdateByPrimaryKeyStatementId()); //method.addParameter(new Parameter(parameterType, "record")); //$NON-NLS-1$ FullyQualifiedJavaType mapType=FullyQualifiedJavaType.getMyMapInstance(); //方法的参数,这里是Map类型的dateMap参数 Parameter parameter = new Parameter(mapType, "dataMap"); method.addParameter(parameter); context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); addMapperAnnotations(interfaze, method); if (context.getPlugins() .clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated(method, interfaze, introspectedTable)) { interfaze.addImportedTypes(importedTypes); interfaze.addMethod(method); } } public void addMapperAnnotations(Interface interfaze, Method method) { return; }}
原标题:Mybatis逆工程(下)
关键词:mybatis
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。