Development tools: Mybatis.Plus . plug in three ways of reverse engineering

Time:2021-1-11

Source code:GitHub. Click here || Gitee. Click here

1、 Introduction to reverse engineering

In java development, the most commonly used framework of persistence layer is mybatis, which needs to write SQL statements. Mybatis officially provides reverse engineering, which can automatically generate the basic code needed for execution of data table, such as mapper interface, SQL mapping file, POJO entity class, etc., avoiding the complicated process of basic code maintenance.

In practical use, the common reverse engineering methods are as follows: mybatis framework, mybatis plus framework, plug-in mode.

2、 Mybatis mode

1. Basic description

Based on XML configuration, mybatis basic code is generated, including mapper interface, mapper mapping file, POJO entity class, pojoexample conditional tool class.

2. Configuration file

Note that targetproject needs to configure a custom path location.

3. Startup class

Read the configuration file and execute.

public class GeneratorMybatis {

    public void generator() throws Exception {
        List warnings = new ArrayList();
        boolean overwrite = true;
        File configFile = Resources.getResourceAsFile("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 {
            GeneratorMybatis generatorMybatis = new GeneratorMybatis();
            generatorMybatis.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3、 Mybatisplus mode

1. Basic description

Mybatisplus provides more enhanced capabilities than mybatis. Single table operations are basically encapsulated, so the generated mapper mapping file is much simpler. You need to pay attention to the key classes of serviceimpl and the basemapper interface.

2. Core startup class

The configuration here can be based on many custom policies. The code generated by the case has been sent to the warehouse and can be downloaded and viewed by yourself.

public class GeneratorMybatisPlus {

    public static void main(String[] args) {
        //Code generator
        AutoGenerator autoGenerator = new AutoGenerator();
        //Global configuration
        GlobalConfig globalConfig = new GlobalConfig();
        //The output directory of the makefile
        String path = storage path;
        globalConfig.setOutputDir(path);
        //Author setting author
        globalConfig.setAuthor("mybatis-plus");
        //File overlay
        globalConfig.setFileOverride(true);
        //Open file after build
        globalConfig.setOpen(false);
        //Custom file name style,% s auto fill table entity attribute
        globalConfig.setMapperName("%sMapper");
        globalConfig.setXmlName("%sMapper");
        globalConfig.setServiceName("%sDao");
        globalConfig.setServiceImplName("%sDaoImpl");
        globalConfig.setEntityName("%s");
        globalConfig.setControllerName("%sController");
        autoGenerator.setGlobalConfig(globalConfig);

        //Data source configuration
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setTypeConvert(new MySqlTypeConvert());
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/defined-log?tinyInt1isBit=false");
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        autoGenerator.setDataSource(dataSourceConfig);

        //Package name configuration
        PackageConfig packageConfig = new PackageConfig();
        //Parent and child package names are handled separately
        packageConfig.setParent("com.generator.mybatis.plus");
        packageConfig.setController("web");
        packageConfig.setEntity("pojo");
        packageConfig.setMapper("mapper");
        packageConfig.setService("dao");
        packageConfig.setServiceImpl("dao.impl");
        autoGenerator.setPackageInfo(packageConfig);

        //Build policy configuration
        StrategyConfig strategy = new StrategyConfig();
        //Format naming
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //If the entity is a Lombok model, the default is false
        strategy.setEntityLombokModel(true);
        //Generate @ restcontroller controller
        strategy.setRestControllerStyle(true);
        //Hump to hyphen
        strategy.setControllerMappingHyphenStyle(true);
        //Table and prefix processing
        strategy.setInclude("dt_defined_log".split(","));
        String[] tablePre = new String[]{"dt_"};
        strategy.setTablePrefix(tablePre);
        autoGenerator.setStrategy(strategy);
        //The above parameters can be obtained based on dynamic input
        autoGenerator.execute();
    }
}

This method is the most popular development method of mybatis framework, and the code will be much simpler.

4、 Plug in tools

1. Configuration database

Select the MySQL data source here, and then download the driver configuration according to the prompt.

2. Connection configuration

URL address, account number, password, get connection.

3. Plug in usage

Here you choose to install the EASYCODE plug-in.

According to the configuration, the reverse engineering file is generated, and the overall idea is consistent with the above two ways.

5、 Source code address

GitHub · address
https://github.com/cicadasmile/data-manage-parent
Gitee · address
https://gitee.com/cicadasmile/data-manage-parent

Recommended reading: programming system arrangement

Serial number entry name GitHub address Gitee address Recommended index
01 Java describes design patterns, algorithms, and data structures GitHub. Click here Gitee. Click here ☆☆☆☆☆
02 Java foundation, concurrency, object oriented, web development GitHub. Click here Gitee. Click here ☆☆☆☆
03 Spring cloud microservice basic component case explanation GitHub. Click here Gitee. Click here ☆☆☆
04 A case study of springcloud microservice architecture GitHub. Click here Gitee. Click here ☆☆☆☆☆
05 Basic application of springboot framework GitHub. Click here Gitee. Click here ☆☆☆☆
06 Integration and development of common middleware based on springboot framework GitHub. Click here Gitee. Click here ☆☆☆☆☆
07 Basic cases of data management, distributed and architecture design GitHub. Click here Gitee. Click here ☆☆☆☆☆
08 Big data series, storage, components, computing and other frameworks GitHub. Click here Gitee. Click here ☆☆☆☆☆