Introduction
Mybatis generator is a code generator that can automatically generate corresponding entity classes, dao classes, and mapper mapping files in Java code based on the fields and types of database tables.
There are many ways to realize the function of Mybatis generator, such as plug-in mode and configuration file mode. Here I use the method that I have personally practiced and successfully operated.
Method: Based on Maven project construction, the method of XML file configuration.
1. Add dependencies
The first dependency is the core dependency of the mybatis-generator code generator
The second dependency is the dependency of mysql connecting to the database through JDBC
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
After adding dependencies, remember to refresh the maven project. Let maven automatically download the jar packages corresponding to these two dependencies.
2. Configure generatorConfig.xml
The generatorConfig.xml file is mainly used to configure basic information such as the path, location, and table name of the generated file . Of course, you can also configure information such as annotation customization, and only the most basic function implementation is done here.
Here are a few important parameters:
- javaModelGenerator : Generate entity class storage location (that is, POJO class storage location)
- targetPackage: the package name of the class
- targetProject: the address of the project in the computer
- sqlMapGenerator : Generate the storage location of the mapping file (that is, the storage location of the sql statement)
- ditto
- javaClientGenerator : Generate Dao class storage location (that is, dao file storage location)
- ditto
<?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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://数据库地址"
userId="username"
password="password">
</jdbcConnection>
<!--生成entity类存放位置-->
<javaModelGenerator targetPackage="包名(com.generator.test.entity)" targetProject="项目地址到\java (D:\workspace\src\main\java)">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="包名(com.generator.test.mapper)" targetProject="项目地址到\java (D:\workspace\src\main\java)">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="包名(com.generator.test.dao)"
targetProject="项目地址到\java (D:\workspace\src\main\java)">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="表名" domainObjectName="生成实体的类名">
</table>
</context>
</generatorConfiguration>
An example is posted below ( the project address of the code is the project address in the Mac system, which is different from Windows. The key project address of Windows is like D:\workspace\src\main\java) :
<?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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/myDataBase"
userId="lab_1941747"
password="4546091bded4">
</jdbcConnection>
<!--生成entity类存放位置-->
<javaModelGenerator targetPackage="org.example.beans" targetProject="/Users/zhanglong/IdeaProjects/test1/src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="mybatis" targetProject="/Users/zhanglong/IdeaProjects/test1/src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="org.example.dao" targetProject="/Users/zhanglong/IdeaProjects/test1/src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="students" domainObjectName="Students">
</table>
</context>
</generatorConfiguration>
3. Run
The general meaning of this code is to start MyBatisGenerator and automatically generate code according to the scheme of the configuration file
package org.example;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* 运行前提:添加mysql-connector-java依赖
*/
public class test {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/main/java/org/example/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}