2

1.前言

本文学习目标:

  1. 修改mybatis-generator-plus插件的源码(中文注释、4格缩进,简化生成文件个数);
  2. 运用上一篇讲解的assembly插件,将修改后的代码打包。

2.项目结构

clipboard.png

2.1 pom中引入插件

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>pro-all</artifactId>
        <groupId>cn.whbing.pro</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mybatis-genarator-modify</artifactId>

    <!--mybatis-generator-plus源码找那个需要以下依赖-->
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.9.1</version>
        </dependency>
    </dependencies>

    <build>

        <finalName>mybatis-genarator-modify</finalName>

        <!--该resource节点的加入是为了将dtd文件也打包,否则出错-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                    <include>**/*.dtd</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                    <include>**/*.dtd</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <!--assembly的配置-->
                <configuration>
                    <finalName>mybatis-genarator-modify</finalName>

                    <!--这里先使用自带的打包,后续再修改成复杂的-->
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <!--入口文件-->
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>org.mybatis.generator.api.ShellRunner</mainClass> <!-- 你的主类名 -->
                        </manifest>
                    </archive>
                </configuration>

                <!--在package生命周期执行-->
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>

2.2 生成target

clipboard.png

运行命令后,在target目录下生成了文件:

clipboard.png

将上述jar包复制到测试文件夹中,将generatorConfig.xml文件也复制过来:

clipboard.png

其中generatorConfig.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
    <classPathEntry
        location="/Users/didi/.m2/repository/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar" />
    <context id="context1"  defaultModelType="flat" targetRuntime="MyBatis3Simple">
        <!-- 为了防止生成的代码中有很多注释,比较难看,加入下面的配置控制  -->
        <!-- 是否去除自动生成的注释 true:是 :false:否 -->
        <!-- <commentGenerator >
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator> -->
        <!--去除注释的配置要放在数据库配置的前面,不然会报错-->
        <!-- 注释end -->

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:MySql://localhost/table***?characterEncoding=UTF-8&amp;allowMultiQueries=true"
            userId="root" password="***" />
        <javaModelGenerator targetPackage="com.***.domain.entity"
            targetProject="src/main/java" />
        <sqlMapGenerator targetPackage="sqlmap"
            targetProject="src/main/resources" />
        <javaClientGenerator targetPackage="com.***.dao"
            targetProject="src/main/java" type="XMLMAPPER" />
        <table tableName="your_tablename" domainObjectName="TaskDO"
            enableCountByExample="true" enableUpdateByExample="true"
            enableDeleteByExample="true" enableSelectByExample="true"
            selectByExampleQueryId="false"></table>


    </context>
</generatorConfiguration>

可以看到成功生成文件

执行命令:

java -jar mybatis-genarator-modify-jar-with-dependencies.jar -configfile generatorConfig.xml

clipboard.png


王小禾
99 声望6 粉丝

public class Myself {