如何优雅地使用Mybatis逆向工程生成类

文/朱季谦

1.环境:SpringBoot

2.在pom.xml文件里引入相关依赖:

 1 <plugin>
 2    <groupId>org.mybatis.generatorgroupId>
 3    <artifactId>mybatis-generator-maven-pluginartifactId>
 4    <version>1.3.6version>
 5    <configuration>
 6       <verbose>trueverbose>
 7       <overwrite>trueoverwrite>
 8    configuration>
 9    <dependencies>
10       <dependency>
11          <groupId>mysqlgroupId>
12          <artifactId>mysql-connector-javaartifactId>
13          <version>5.1.36version>
14       dependency>
15    dependencies>
16 
17 plugin>
18 
19 
20 
21 <dependency>
22    <groupId>org.mybatis.generatorgroupId>
23    <artifactId>mybatis-generator-coreartifactId>
24    <version>1.3.6version>
25 dependency>

3.创建generatorConfig.xml文件

 1 xml version="1.0" encoding="UTF-8"?>
 2 
 3 DOCTYPE generatorConfiguration
 4         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 5         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 6 
 7 <generatorConfiguration>
 8     <context id="testTables" targetRuntime="MyBatis3">
 9         <commentGenerator>
10             
11             <property name="suppressAllComments" value="true" />
12         commentGenerator>
13         
14         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
15                         connectionURL="jdbc:mysql://127.0.0.1:3306/example?useUnicode=true&characterEncoding=utf8"
16                         userId="root"
17                         password="123456">
18         jdbcConnection>
19 
20         
22         <javaTypeResolver>
23             <property name="forceBigDecimals" value="false" />
24         javaTypeResolver>
25 
26         
27         <javaModelGenerator targetPackage="com.zhu.text.domain"
28                             targetProject=".\src\main\java">
29             
30             <property name="enableSubPackages" value="false" />
31             
32             <property name="trimStrings" value="true" />
33         javaModelGenerator>
34         
35         <sqlMapGenerator targetPackage="com.zhu.text.dao"
36                          targetProject=".\src\main\java">
37             
38             <property name="enableSubPackages" value="false" />
39         sqlMapGenerator>
40         
41         <javaClientGenerator type="XMLMAPPER"
42                              targetPackage="com.zhu.text.dao" targetProject=".\src\main\java">
43             
44             <property name="enableSubPackages" value="false" />
45         javaClientGenerator>
46         
47         <table tableName="text_product" domainObjectName="Product">table>
48     context>
49 generatorConfiguration>

4.创建main类

 1 import java.io.File;
 2 import java.util.ArrayList;
 3 import java.util.List;
 4 import org.mybatis.generator.api.MyBatisGenerator;
 5 import org.mybatis.generator.config.Configuration;
 6 import org.mybatis.generator.config.xml.ConfigurationParser;
 7 import org.mybatis.generator.internal.DefaultShellCallback;
 8 
 9 public class MBGenerator {
10     public static void main(String[] args) throws Exception{
11 
12         List warnings = new ArrayList();
13         boolean overwrite = true;
14         File configFile = new File("例:generatorConfig.xml的存放路径");
15         ConfigurationParser cp = new ConfigurationParser(warnings);
16         Configuration config = cp.parseConfiguration(configFile);
17         DefaultShellCallback callback = new DefaultShellCallback(overwrite);
18         MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
19         myBatisGenerator.generate(null);
20     }
21 }

 

5.执行maven方法里的mybatis-generator:generate

如何优雅地使用Mybatis逆向工程生成类_第1张图片

完成以上步骤,就可以自定生成需要的xml文件与类以及对应的java bean类。

你可能感兴趣的:(如何优雅地使用Mybatis逆向工程生成类)