Scenario introduction
During the process of writing code, we always hope that there is a plug-in that can quickly generate common similar code. It feels like mybatis-generator is good to use. Let’s summarize its usage method below.
Steps to use
1. Create a new generator.xml file
<?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> <!--Introduce configuration files--> <!--TODO Note! ! ! Automatically generate code to let go of this sentence, link to the database --> <properties resource="jdbc.properties"/> <!-- Specify the jar address of the data connection driver --> <classPathEntry location="F:/SVN_Info/cloudTree/trustzhyq/src/e3izm/src/main/webapp/WEB-INF/lib/mysql-connector-java-5.1.29.jar"/> <context id="context" targetRuntime="MyBatis3"> <commentGenerator> <!-- Whether to remove the automatically generated comments true: Yes: false: No--> <property name="suppressAllComments" value="true"/> <property name="suppressDate" value="true"/> </commentGenerator> <!-- Related configuration of database --> <jdbcConnection driverClass="${driverClass}" connectionURL="${jdbcUrl}" userId="${username}" password="${password}"/> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- Location of entity class generation --> <javaModelGenerator targetPackage="com.trust.e3izm.ressvc.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- *Mapper.xml file location, targetPackage: package name, targetProject: path under the project --> <sqlMapGenerator targetPackage="ressvc" targetProject="src/main/resources/mapper"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- Location of Mapper interface file --> <javaClientGenerator targetPackage="com.trust.e3izm.ressvc.dao" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!--Configuration table information-> <!--Third-party service type--> <table schema="e3iz" tableName="THIRDPTYSVC_TYPE" domainObjectName="Thirdptysvc_type" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" enableUpdateByExample="false" enableUpdateByExample="false"> </table> <!--xxtable--> <!--If n tables are generated, then copy the above table code n copies--> </context></generatorConfiguration> 2. Import dependency packages in pom.xml
<plugins> <plugin> <!--Mybatis-generator plugin, used to automatically generate Mapper and POJO--> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!--location of configuration files--> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> </dependencies> </plugin> </plugins>
Notice
1. This code needs to be put into
<build> <finalName>e3izm</finalName> <!-- Put the above code in this location of the pom.xml file--> </build>
2. Maven2 download about generator, the maven dependency package cannot be downloaded, it needs to be changed to maven3 to download it
maven3 changes.png
3. Create a new maven runner
maven.png
<!-- Configured run command -->mybatis-generator:generate -e
Okay, the job is done, just run the maven runner!
If you have any questions, you can read the official documentation.
MyBatis generator official documentation
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.