Mybatis framework -- mybatis reverse engineering

Posted by FastLaneHosting on Wed, 22 Apr 2020 18:14:12 +0200

< center > mybatis reverse engineering < / center >


The purpose of reverse engineering is to reduce our development time. The so-called Mybatis reverse engineering is that Mybatis will automatically generate pojo, mapper and mapper.xml according to the data table we have designed.

The next step is the project construction process. github source code: mybatis reverse engineering code

##< font color = ාffd700 > I. pom.xml file < / font >

    <!--Connect mysql drive-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.41</version>
    </dependency>

    <!--mapper-->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>1.2.4</version>
    </dependency>

    <!-- mybatis Reverse generation tool  -->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.2</version>
        <scope>compile</scope>
        <optional>true</optional>
    </dependency>

<br>

< font color = (ffd700 > 2. Generatorconfig. XML < / font >

This is configuration reverse engineering configuration information.

<?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>

    <!--One context Corresponding to a database-->
    <context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!--Set parent class mapper,In this way, all reverse engineering generated mapper Will inherit the mapper-->
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.binron.mapper.base.BaseMapper"/>
        </plugin>

        <!--Connection database information-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://47.99.888.55:3306/binron"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <!-- For generated pojo Package -->
        <javaModelGenerator targetPackage="com.binron.pojo" targetProject="src/main/java"/>

    	<!-- For generated mapper Directory -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>

		<!-- To configure mapper Corresponding java mapping -->
        <javaClientGenerator targetPackage="com.binron.mapper" targetProject="src/main/java" type="XMLMAPPER"/>


        <!--Table requiring reverse engineering-->
		<table tableName="users"></table>
		<table tableName="my_friends"></table>
		<table tableName="friends_request"></table>
		<table tableName="chat_msg"></table>
		 
    </context>
</generatorConfiguration>

<br>

##< font color = ාffd700 > 3. Parent class basemapper < / font >

In configuration information, the parent mapper is not necessary, but in general enterprise development, there must be a parent class, because you can't write addition, deletion, modification and query methods for each mapper, which can be extracted completely.

/**
 * @author All business tables of the underlying mapper inherit this mapper
 */
public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T> {
    //FIXME should pay special attention to that the interface cannot be scanned, otherwise an error will occur
    /**
     * Delete by primary key
     */
    int deleteByPrimaryKey(String id);

    /**
     * Insert Object
     */
    int insert(T record);

    /**
     * Find objects by K
     */
    T selectByPrimaryKey(String id);

    /**
     * Find all
     */
    List<T> selectAll();

    /**
     * Update objects
     */
    int updateByPrimaryKey(T record);
}

<br>

< font color = (ffd700 > IV) generatordisplay < / font >

public class GeneratorDisplay {

	public void generator() throws Exception{

		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		//Specify reverse engineering profile
		File configFile = new File("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);

	} 
	
	public static void main(String[] args) throws Exception {
		try {
			GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
			generatorSqlmap.generator();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

It's so simple in general.

Attention points

1. After a table has been reverse engineered, the main method cannot be executed again unless the table is deleted in generatorConfig.xml, because if the main is executed twice in turn, mapper.xml
 The content will overlap.
2. If your table changes, you can choose to delete the table first, so you can reverse engineer it, rebuild it, or modify the content manually.

Topics: Mybatis xml MySQL Java