This article introduces how to use the Maven tool to generate Mybatis code and mapping files.
1. Configure the Maven pom.xml file
Add the following plugins to pom.xml:
<build> <finalName>zsxt</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins></build>
Configure the Maven plug-in, the following needs to configure the plug-in and configure the configuration file
2. Create a project configuration file storage path called Maven in the src/main/resources directory under the maven project as shown in the figure below: generatorConfig.xml and generator.properties configuration files.
The path to the project configuration file storage of Maven is as follows:
GeneratorConfig.xml code is as follows:
<?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> <!--Import property configuration--> <properties resource="generator.properties"></properties> <!--Specify the location of the jdbc driver jar package for a specific database--> <classPathEntry location="${jdbc.driverLocation}"/> <context id="default" targetRuntime="MyBatis3"> <!-- optional, aiming to control comments when creating class --> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--jdbc database connection--> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}"> </jdbcConnection> <!-- Non-essential, type processor, conversion control between database type and java type--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- Model model generator, used to generate classes containing primary key keys, record classes, and query Example class targetPackage Specifies the package name where the generated model is generated. TargetProject Specifies the path under the project--> <javaModelGenerator targetPackage="com.slx.zsxt.model" targetProject="src/main/java"> <!-- Whether to allow subpackages, that is, targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false"/> <!-- Whether to add constructors to model --> <property name="constructorBased" value="true"/> <!-- Whether to trim the data of columns of CHAR type--> <property name="trimStrings" value="true"/> <!-- Whether to create a Model object that cannot be changed, that is, the generated Model object will not have a setter method, only the constructor--> <property name="immutable" value="false"/> </javaModelGenerator> <!--The directory where the Mapper mapping file is generated generates the corresponding SqlMap file for each database table--> <sqlMapGenerator targetPackage="com.slx.zsxt.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- Client code, generates easy-to-use code for Model objects and XML configuration files type="ANNOTATEDMAPPER", generates Java Model and annotation-based Mapper object type="MIXEDMAPPER", generate annotation-based Java Model and corresponding Mapper object type="XMLMAPPER", generate SQLMap XML files and independent Mapper interface--> <javaClientGenerator targetPackage="com.slx.zsxt.dao" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="reguser" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="adminuser" domainObjectName="Admin" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="configinfo" domainObjectName="Confinfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="grade" domainObjectName="Grade" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="gradelog" domainObjectName="Gradelog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="reginfo" domainObjectName="Reginfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context></generatorConfiguration>The generator.propertites code is as follows:
jdbc.driverLocation=E://mvn_home//mysql//mysql-connector-java//5.1.20//mysql-connector-java-5.1.20.jarjdbc.driverClass=com.mysql.jdbc.Driverjdbc.connectionURL=jdbc:mysql:///zsxtjdbc.userId=rootjdbc.password=123456
3. Add a "Run Run" option to Intellij IDEA, and use maven to run the mybatis-generator-maven-plugin plugin
Click Edit Configurations in menu run, and it will appear
Click the + sign and select maven, and it will appear
Fill in the name and Common line as shown in the above figure, apply and ok
Finally click generator to generate model, mapper, dao
The results of reverse engineering are as follows:
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.