Mybatis逆工程jar包的修改和打包

论坛 期权论坛 脚本     
niminba   2021-5-23 03:55   1927   0

上一篇文章Mybatis逆工程的使用主要是讲了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和xml格式。

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(interfagt;
<groupId>org.codehaus.mojo</groupId>
<artifactId>jdepend-maven-plugin</artifactId>
<version>2.0-beta-2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
</plugin>
</plugins>
</reporting>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</dependency>
</dependencies>
<scm>
<url>https://mybatis.googlecode.com/svn/sub-projects/generator/tags/mybatis-generator-1.3.2/mybatis-generator-core</url>
<connection>scm:svn:https://mybatis.googlecode.com/svn/sub-projects/generator/tags/mybatis-generator-1.3.2/mybatis-generator-core</connection>
<developerConnection>scm:svn:https://mybatis.googlecode.com/svn/sub-projects/generator/tags/mybatis-generator-1.3.2/mybatis-generator-core</developerConnection>
</scm> 
</project> 

然后是修改src/main/assembly/src.xml代码

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bundle</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>generatorConfig.xml</include>
</includes>
<excludes>
<exclude>log4j.properties</exclude>
<exclude>src.xml</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>src/main/scripts</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>run.bat</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>${project.artifactId}-${project.version}.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>${project.artifactId}-${project.version}-sources.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly> 

接下来,在src/main/ 下面新建scripts文件夹,在scripts文件夹新建txt文本文档,输入以下代码

java -jar mybatis-generator-1.3.2.jar -configfile generatorConfig.xml –overwrite
pause

改文件名为run.bat

至此,打包配置完毕。

大家可以在项目下右键Run as->maven build 在goal里输入package。或者命令行cmd中 进入项目的目录,运行mvn package,这里第一次运行会等待很久,因为maven会下载依赖的jar包,请耐心等待。

打包完毕,就会在项目根目录下的target目录生成如下结构

从上图中我们可以看到mybatis-generator-core-1.3.2.jar包已经生成。接下来我们可以用它加上generatorConfig.xml来生成自己想要的代码。

如果修改代码过程中有什么不懂的,请多看源代码。

OK,晒下成果图

本文就讲到这里!

以上所述是小编给大家介绍的Mybatis逆工程jar包的修改和打包的相关知识,希望对大家有所帮助!

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP