Now code management is basically managed by Maven. I won’t talk about the benefits of Maven here. You will introduce it with Baidu search. This article introduces how to use Maven tools to generate Mybatis code and mapping files.
1. Configure the Maven pom.xml file
Add the following plugins to pom.xml:
<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>
Configure the Maven plug-in, the following needs to configure the plug-in and configure the configuration file
2. Plug-in configuration file
The path to the project configuration file storage of Maven is as follows:
By default, the plug-in will read the generatorConfig.xml file in the src/main/resources directory.
The specific configuration 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> <classPathEntry location="D:/repo/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar" /> <context id="context1" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://xxxxxxx:8406/CL_DEMO?useUnicode=true&characterEncoding=UTF-8" userId="root" password="password" /> <javaModelGenerator targetPackage="xxx.account.model" targetProject="D:/workspace/Project name/src/main/java" /> <sqlMapGenerator targetPackage="xxxx.account.persistence" targetProject="D:/workspace/project name/package name/src/main/resources" /> <javaClientGenerator targetPackage="xxxx.account.persistence targetProject="D://workspace/project name/src/main/java" type="XMLMAPPER" /> <table schema="CL_DEMO" tableName="tb_user" /> <table schema="CL_DEMO" tableName="tb_role" /> <table schema="CL_DEMO" tableName="tb_permission" /> <table schema="CL_DEMO" tableName="tb_role_user" /> <table schema="CL_DEMO" tableName="tb_permission_role" /> </context></generatorConfiguration>
In this example, the MySQL database is used, and the JDBC driver of the MySQL database needs to be specified.
1. Specify the URL to connect to the database;
2. Specify the package name of the generated data model object, such as com.xxx.xxx.model, targetProject Specifies the project and the directory where the model is stored.
3. SQLMapGenerator needs to set the package name and the path to store the mapped file. If managed with maven, the xml file is usually placed in the src/main/resources directory.
4. The javaClientGenerator needs to set the package name and path.
6. Next, you need to configure the table name you need to generate.
3. Generate code
After the configuration is completed, you can now generate the code
If it is in eclipse, select the pom.xml file, right-click to select Run AS->Maven Build…->Enter: mybatis-generator:generate in the Goals box
If you just enter the Maven command on the command line, please note: the command must be run in the current project directory:
mvn mybatis-generator:generate
The code has been generated and the goodbye has been done.