【MyBatis】Study Minutes Eleven: Reverse Engineering

words written in front

I learned the operation principle of MyBatis before. I just finished learning it. Looking back, it is still very complicated. Because I have not analyzed the source code in this way before, this section is stuck, and it may take a certain amount of time. In order not to get stuck in learning progress, so we continue to learn and write plugins later.

Introduction to MyBatis Generator

1. I will first tell you the official website address: MyBatis Generator

2. I found this problem when I wrote the test earlier. I have to write dao, model, mpper, which is painful when writing a project. How to solve this problem? - The answer is: mybatis generator.

3. As you know from point 2, the main function of MyBatis generator is to create dao, model, mpper.

Quick Start Guide

1. Add dependencies

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator</artifactId>
            <version>1.3.6</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.6</version>
        </dependency>

2. WritegeneratorConfig.xml

In order to successfully complete the writing of this file, we refer to the XML Configuration Reference section of the official website for writing.

In addition, it is recommended to write more perfect through the search engine, or what you want.

3. Directory

directory.png

(because I use git, the red one is not an error)

4、Running MyBatis Generator

Here Running MyBatis Generator , you can find various running modes.

If you go through a search engine, you may find commands like this

java -jar mybatis-generator-core-x.x.x.jar -configfile \temp\generatorConfig.xml -overwrite

If you are looking at open source projects, you will pom.xmlsee the following code:

   <project ...>
     ...
     <build>
       ...
       <plugins>
        ...
        <plugin>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.5</version>
          <executions>
            <execution>
              <id>Generate MyBatis Artifacts</id>
              <goals>
                <goal>generate</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        ...
      </plugins>
      ...
    </build>
    ...
  </project>

Either way, you can find instructions on the official website.

So how do we use it?

package com.fengwenyi.demo.mybatis.generatorconfig;

import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Wenyi Feng
 */
public class RunGenerator {

    @Test
    public void test () {
        try {
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            File configFile = new File("src/main/resources/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);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Let's take a look at the effect:

run location.png

run-generator.png

new-directory.png

let's try

1. Scan dao, two solutions, scan all mapper interfaces under dao, or scan the specified interface.

2. Write the test code:

@Autowired
private CityMapper cityMapper;

@Test
public void testCity () {
    List<City> cities = cityMapper.selectByExample(null);
    for (City city : cities)
        System.out.println(city);
}

Check out the test results:

test result.png

It's almost here, write a postscript

postscript

1. You can refer to the code in this section ( generator-config ), it will be very convenient to generate the relevant files and codes you want.

2. If you are in the basic part of MyBatis (such as Dynamic SQL ), you can refer to the Mapper.xmlfile to learn.

material

1、generator

2、 MyBatis Generator

3. Test code in this section: generator-config

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326988063&siteId=291194637