Four Ways of MyBatis_Generator (MBG) Reverse Engineering

Posted by hip_hop_x on Tue, 20 Aug 2019 09:06:08 +0200

mybatis is a popular persistence framework, and its reverse engineering greatly reduces our development time. Interested can see the document.

Document address: http://www.mybatis.org/generator/index.html

Not much nonsense. Let's summarize some of his configurations (for example, Eclipse):

 

1. Installation of Eclipse plug-ins

Installation here is divided into online installation and manual installation. Let's start with manual installation.

1. Manual Installation

Download address: https://dl.bintray.com/mybatis/mybatis-generator

Unzip and copy the jar package under the corresponding folder to the same folder under the installation directory of eclipse, restart eclipse.

2. On-line Installation

Eclipse installs mybatis generator online, opens eclipse, and finds help - > Eclispe Mapketplace

Enter mybatis generator in the search box, and then click go.

Find the corresponding version of mybatis generator, download it and restart eclipse

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration
 3   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 4   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 5 
 6 <generatorConfiguration>
 7     <context id="testTables" targetRuntime="MyBatis3">
 8         <commentGenerator>
 9             <!-- Whether to remove automatically generated comments true: Yes: false:no -->
10             <property name="suppressAllComments" value="true" />
11         </commentGenerator>
12         <!--Information about database connection: driver class, connection address, user name, password -->
13         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
14             connectionURL="jdbc:mysql:///db_demo" userId="root"
15             password="123">
16         </jdbcConnection>
17         <!-- default false,hold JDBC DECIMAL and NUMERIC Type resolution is Integer,by true Shi Ba JDBC DECIMAL and 
18             NUMERIC Type resolution is java.math.BigDecimal -->
19         <javaTypeResolver>
20             <property name="forceBigDecimals" value="false" />
21         </javaTypeResolver>
22 
23         <!-- targetProject:generate PO Class Location -->
24         <javaModelGenerator targetPackage="com.demo.pojo"
25             targetProject=".\src">
26             <!-- enableSubPackages:Whether to let schema Suffixes for packages -->
27             <property name="enableSubPackages" value="false" />
28             <!-- Spaces before and after values returned from the database are cleaned up -->
29             <property name="trimStrings" value="true" />
30         </javaModelGenerator>
31         <!-- targetProject:mapper Location of mapping file generation -->
32         <sqlMapGenerator targetPackage="com.demo.mapper" 
33             targetProject=".\src">
34             <!-- enableSubPackages:Whether to let schema Suffixes for packages -->
35             <property name="enableSubPackages" value="false" />
36         </sqlMapGenerator>
37         <!-- targetPackage: mapper Location of interface generation -->
38         <javaClientGenerator type="XMLMAPPER"
39             targetPackage="com.demo.mapper" 
40             targetProject=".\src">
41             <!-- enableSubPackages:Whether to let schema Suffixes for packages -->
42             <property name="enableSubPackages" value="false" />
43         </javaClientGenerator>
44         <!-- Specify database tables -->
45         <table schema="" tableName="tb_user"></table>
46         <table schema="" tableName="tb_area"></table>
47         
48     </context>
49 </generatorConfiguration>
50 
51 Author: Lazy Month
52 Links: https://www.jianshu.com/p/74ab984b4fee
53 Source: Brief Book
54 The copyright of the brief book belongs to the author. For any form of reprinting, please contact the author for authorization and indicate the source.

After the plug-in is installed, create the reverse engineering file generatorConfig.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration
 3   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 4   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 5 
 6 <generatorConfiguration>
 7 <!--targetRuntime="MyBatis3"Can not write-->
 8     <context id="testTables">
 9         <commentGenerator>
10             <!-- Whether to remove automatically generated comments true: Yes: false:no -->
11             <property name="suppressAllComments" value="true" />
12         </commentGenerator>
13         <!--Information about database connection: driver class, connection address, user name, password -->
14         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
15             connectionURL="jdbc:mysql:///db_demo" userId="root"
16             password="123">
17         </jdbcConnection>
18         <!-- default false,hold JDBC DECIMAL and NUMERIC Type resolution is Integer,by true Shi Ba JDBC DECIMAL and 
19             NUMERIC Type resolution is java.math.BigDecimal -->
20         <javaTypeResolver>
21             <property name="forceBigDecimals" value="false" />
22         </javaTypeResolver>
23 
24        <!-- Generated po class:targetPackage targetProject:project name+src -->
25         <javaModelGenerator targetPackage="com.demo.pojo"
26             targetProject="project name\src">
27             <!-- enableSubPackages:Whether to let schema Suffixes for packages -->
28             <property name="enableSubPackages" value="false" />
29             <!-- Spaces before and after values returned from the database are cleaned up -->
30             <property name="trimStrings" value="true" />
31         </javaModelGenerator>
32         <!-- targetProject:mapper Location of mapping file generation -->
33         <sqlMapGenerator targetPackage="com.demo.mapper" 
34             targetProject="project name/src/com/demo/mapper">
35             <!-- enableSubPackages:Whether to let schema Suffixes for packages -->
36             <property name="enableSubPackages" value="false" />
37         </sqlMapGenerator>
38         <!-- targetPackage: mapper Location of interface generation -->
39         <javaClientGenerator type="XMLMAPPER"
40             targetPackage="com.demo.mapper" 
41             targetProject="project name/src/com/demo/mapper">
42             <!-- enableSubPackages:Whether to let schema Suffixes for packages -->
43             <property name="enableSubPackages" value="false" />
44         </javaClientGenerator>
45         <!-- Specify database tables -->
46      <!-- Configuration table name domainObjectName="SysEmpBean" Setting the name of entity class default table name, removing underscore and using hump naming -->
47         <table schema="" tableName="tb_user"></table>
48         <table schema="" tableName="tb_area"></table>
49         
50     </context>
51 </generatorConfiguration>
Right-click on the xml file and run with plug-in

Running in Java

Add jar mybatis-generator-core-1.3.7.jar
Create the configuration file generatorConfig.xml as above, but it's important to note that the targetProject in the configuration file uses the eclipse path with the project name Java code.
Do not process the name of the project (that is, targetProject="./src/com/demo/mapper").
Create a java class
 1 import java.io.File;
 2 import java.io.IOException;
 3 import java.sql.SQLException;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 import org.mybatis.generator.api.MyBatisGenerator;
 8 import org.mybatis.generator.config.Configuration;
 9 import org.mybatis.generator.config.xml.ConfigurationParser;
10 import org.mybatis.generator.exception.InvalidConfigurationException;
11 import org.mybatis.generator.exception.XMLParserException;
12 import org.mybatis.generator.internal.DefaultShellCallback;
13 
14 public class MBGTest {
15     public static void main(String args[])
16             throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
17            List<String> warnings = new ArrayList<String>();
18            boolean overwrite = true;
19            File configFile = new File("generatorConfig.xml");
20            ConfigurationParser cp = new ConfigurationParser(warnings);
21            Configuration config = cp.parseConfiguration(configFile);
22            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
23            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
24            myBatisGenerator.generate(null);
25     }
26 
27 }

Execute in Java Application mode

III. Maven approach

The configuration of mybatis generator on eclipse mainly needs to be modified in the following files: pom.xml, the same need to create and write the configuration file generator Config. xml.

Configuration to be added to pom.xml

 1 <build>
 2         <finalName>taotao</finalName>
 3         <pluginManagement>
 4             <plugins>
 5                 <plugin>
 6                     <groupId>org.mybatis.generator</groupId>
 7                     <artifactId>mybatis-generator-maven-plugin</artifactId>
 8                     <version>1.3.2</version>
 9                     <configuration>
10                         <configurationFile>src/main/resources/generator.xml</configurationFile>
11                         <verbose>true</verbose>
12                         <overwrite>true</overwrite>
13                     </configuration>
14                     <executions>
15                         <execution>
16                             <id>Generate MyBatis Artifacts</id>
17                             <goals>
18                                 <goal>generate</goal>
19                             </goals>
20                         </execution>
21                     </executions>
22                     <dependencies>
23                         <dependency>
24                             <groupId>org.mybatis.generator</groupId>
25                             <artifactId>mybatis-generator-core</artifactId>
26                             <version>1.3.2</version>
27                         </dependency>
28                     </dependencies>
29                 </plugin>
30                 <plugin>
31                     <groupId>org.apache.maven.plugins</groupId>
32                     <artifactId>maven-surefire-plugin</artifactId>
33                     <version>2.19.1</version>
34                     <configuration>
35                         <skipTests>true</skipTests>
36                     </configuration>
37                 </plugin>
38 
39                 <plugin>
40                     <groupId>org.apache.maven.plugins</groupId>
41                     <artifactId>maven-resources-plugin</artifactId>
42                     <version>3.0.1</version>
43                     <configuration>
44                         <encoding>UTF-8</encoding>
45                     </configuration>
46                 </plugin>
47             </plugins>
48         </pluginManagement>
49     </build>

Adding various dependencies, such as database dependencies

1  <dependency>
2             <groupId>mysql</groupId>
3             <artifactId>mysql-connector-java</artifactId>
4             <version>5.1.35</version>
5             <scope>runtime</scope>
6         </dependency>

You can see the plug-in in the maven panel:

 

Similarly, you need to create and write the configuration file generatorConfig.xml.

(Just skip here)

Generating Reverse Engineering by maven Instruction

Right-click on the project - > run as - > maven build..., Goals: Enter mybatis-generator:generate (this step can add Maven)

 

Finally, I'll talk about the solutions to the following problems when I use commands

[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: 

Dependency must be the only reason I add two identical dependencies to the pom file and delete one.

Non-resolvable parent POM for com.siyuan:siyuan-web:0.0.1-SNAPSHOT: Could not find artifa

It turned out that the parent project was not registered, and the right-click parent project - run as - maven install could solve the problem.

IV. In command-line mode

Configuration (add dependencies or jar s, write files)

1. Open the command prompt and enter the lib directory (that is, enter the folder where generatorConfig.xml is located, and modify the configuration)

 

2. Input command: java-jar mybatis-generator-core-1.3.2. jar-config file generator.xml-overwrite

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

3. Mybatis Generator completed successful code generation

Then we just need to copy the files into the project. For a long time, we don't need to create a new project for the generated files.

Topics: Java Mybatis xml Maven