本文學習目標:java
mybatis-generator-plus
插件的源碼(中文註釋、4格縮進,簡化生成文件個數);
pom.xml
mysql
<?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>
運行命令後,在target目錄下生成了文件:sql
將上述jar
包複製到測試文件夾中,將generatorConfig.xml
文件也複製過來:數據庫
其中generatorConfig.xml
文件:apache
<?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&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>
能夠看到成功生成文件segmentfault
執行命令:api
java -jar mybatis-genarator-modify-jar-with-dependencies.jar -configfile generatorConfig.xml