8 Reverse engineering of Mybatis

Forward engineering: First create Java entity classes, and the framework is responsible for generating database tables based on the entity classes. Hibernate supports forward engineering
Reverse engineering: create a database table, and the framework is responsible for reversely generating the following resources based on the database table

  • java entity class
  • Mapper interface
  • Mapper mapping file

Steps to create reverse engineering (clear and concise version)

  • Create new module
  • Add mybatis, mysql-connector dependencies
  • Install the mybatis-generator-core plugin in the plugin tag in the bulid tag
"> <!-- Plug-ins used in the build process -->
        <plugins>
            <!-- Specific plug-ins, reverse engineering operations appear in the form of plug-ins during the build process -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.0</version>

                <!-- Plug-in dependencies -->
                <dependencies>

                    <!-- Core dependencies of reverse engineering -->
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>

                    <!-- Database connection pool -->
                    <dependency>
                        <groupId>com.alibaba</groupId>
                        <artifactId>druid</artifactId>
                        <version>1.1.12</version>
                    </dependency>

                    <!-- MySQL driver -->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.27</version>
                    </dependency>

                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • Create mybatis core configuration file, jdbc configuration file, etc.
  • Create a reverse engineering configuration file, the name must be generatorConfig.xml
<?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>
    <!--
    targetRuntime: execute the generated reverse engineering version
           1. MyBatis3Simple: Generate basic CRUD (fresh and simple version)
           2. MyBatis3: Generate conditional CRUD (luxury exclusive version)
    -->
    <context id="DB2Tables" targetRuntime="MyBatis3Simple">
        <!-- Database connection information -->
<!-- There’s nothing much to say about this, I’m very familiar with it-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/create"
                        userId="root"
                        password="root">
        </jdbcConnection>
        <!-- javaBean generation strategy -->
<!-- targetPackage represents the name of the generated package, targetProject represents where the generated package is placed, " . " represents the current directory, which is generated in java in the main of src -->
        <javaModelGenerator targetPackage="com.cn.mybatis.pojo" targetProject=".\src\main\java">
<!-- This enableSubPackages means generating sub-packages. If the value is true, it means that the . in the previous package name can be generated. If it is false, those dots will not work and they are the dots in the package name -->
            <property name="enableSubPackages" value="true" />
<!-- trimStrings is true, which means that the spaces before and after the fields in the table are removed to generate attributes in the entity class -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- SQL mapping file generation strategy -->
<!-- The first few tags are similar to the above, so I won’t explain them -->
        <sqlMapGenerator targetPackage="com.cn.mybatis.mapper"
                         targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- Mapper interface generation strategy -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.cn.mybatis.mapper" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- Table of reverse analysis -->
        <!-- tableName is set to *, which can correspond to all tables. Do not write domainObjectName at this time -->
        <!-- The domainObjectName attribute specifies the class name of the generated entity class. After it is specified, both the related mapper interface and the mapping file will have this name + Mapper -->
        <table tableName="t_emp" domainObjectName="Emp"/>
        <table tableName="t_dept" domainObjectName="Dept"/>
    </context>
</generatorConfiguration>
  • Use plug-ins to automatically create entity classes, mapping files, and mapper interfaces
    • Use the generator plugin in the maven plugin

Mybatis Reverse Engineering (Luxury Exclusive Edition)

  • The steps are the same as above, except that the value of targetRuntime in the reverse engineering file is changed to MyBatis3
  • It can generate conditional CRUD
  • different places
    • For example, if my entity classes are Dept and Emp, then there will be DeptExample and EmpExample classes. The one with Example is generally used as a condition. If Example is null, all records will be queried. There will be a method with Example in the Mapper interface.
    • for example
      • int countByExample(DeptExample example);
      • int deleteByExample(DeptExample example);
    • Count the number of records and delete them based on conditions respectively.
    • besides
      • int insertSelective(Dept record); Selective addition. If an attribute in Dept is null, the field will not be assigned a value when inserted into the table, and then the empty attribute of dept will be assigned the default value in the table.
      • There will be other selective additions, deletions, and modifications in the method, which all have the same meaning. If there is null in the object attribute, what measures should be taken?
    • About how to use Example conditions
      • Give an exampleList selectByExample(EmpExample example);
      • The conditional query here requires an example object
      • Let me show you how to use
        • SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        • EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        • EmpExample empExample = new EmpExample();
        • empExample.createCriteria().andEmpIdBetween(6, 8);
        • List emps = mapper.selectByExample(empExample);
        • We can create corresponding condition objects. For example, for Emp query conditions, create EmpExample objects, and then use empExample.createCriteria(). This method is used to set conditions in mybatis, followed by createCriteria().andxxx, xxx is in this class. The attribute name, you can also select the corresponding conditions later
        • andEmpIdBetween(6, 8) means that EmpId is between 6 and 8. This sets up an example and uses it to query
      • In fact, the method in empExample.createCriteria() is the corresponding sql statement, and it can even be spliced, such as this
        • empExample.createCriteria().andAgeEqualTo(16);
        • empExample.or().andEmpIdBetween(6, 8);
        • Note that or can only be used after empExample and cannot be used directly after Criteria.