Mybatis Generator by Java Api

Usually we use Mybatis Generator to reversely generate Dao and other code, which is achieved through the Generator.xml configuration file. There are many descriptions on the Internet, so I won't repeat them here.

What we are talking about today is how to replace Generator.xml in the form of Java code, that is, to go to XML to realize code generation.

The following is what I found by accident in the brief book, which helped me a lot, and attached the original author address: https://www.jianshu.com/p/e9480ee6a9cc

Keywords:

Mybatis Generator (MBG)-Java configuration alternative xml file

package com.snowy.fastgov.generator;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GeneratorConfig {


    private String driverClass = "com.mysql.cj.jdbc.Driver";

    private String connectionURL = "jdbc:mysql://localhost:3306/any?useUnicode=true&characterEncoding=utf8";

    private String userId = "root";

    private String password = "root";


    private String targetPackage = "com.snowy.fastgov.common.entity";

    private String targetProject = "src/main/java";


    private String sqlTargetPackage = "mapping";

    private String sqlTargetProject = "src/main/resources";


    private String daoTargetPackage = "com.snowy.fastgov.common.mapper";

    private String daoTargetProject = "src/main/java";

    private String classpathEntry="E:\\IdeaProject\\fastgov\\src\\main\\resources\\webapp\\lib\\mysql-connector-java-5.1.43.jar";


    public static void main(String[] args){
        GeneratorConfig generatorConfig = new GeneratorConfig();
        TableEntity tableEntity = new TableEntity();
        tableEntity.setTableName("good");
        tableEntity.setDomainObjectName("Good");
        List<TableEntity> list = new ArrayList<>();
        list.add(tableEntity);
        generatorConfig.generatorConfig(list);
    }

    public void generatorConfig(List<TableEntity> tableList) {

        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        Context context = new Context(ModelType.CONDITIONAL);
        context.setTargetRuntime("MyBatis3");
        context.setId("mysql");
        

        CommentGeneratorConfiguration commentGeneratorConfiguration = new CommentGeneratorConfiguration();
        commentGeneratorConfiguration.addProperty("suppressDate","true");  // if you want to add the comments, change to false
        commentGeneratorConfiguration.addProperty("suppressAllComments","true");  // same as above

        / * Database link URL, username, password * / 
        JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration ();
        jdbcConnectionConfiguration.setDriverClass(driverClass);
        jdbcConnectionConfiguration.setConnectionURL(connectionURL);
        jdbcConnectionConfiguration.setUserId(userId);
        jdbcConnectionConfiguration.setPassword(password);

        JavaTypeResolverConfiguration javaTypeResolverConfiguration = new JavaTypeResolverConfiguration();
        javaTypeResolverConfiguration.addProperty("forceBigDecimals","false");

        / * Package name and location of the generated model * / 
        JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration ();
        javaModelGeneratorConfiguration.setTargetPackage(targetPackage);
        javaModelGeneratorConfiguration.setTargetProject(targetProject);
        javaModelGeneratorConfiguration.addProperty("enableSubPackages","true");
        javaModelGeneratorConfiguration.addProperty("trimStrings","true");

        / * Package name and location of the generated map file * / 
        SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration ();
        sqlMapGeneratorConfiguration.setTargetPackage (sqlTargetPackage);
        sqlMapGeneratorConfiguration.setTargetProject (sqlTargetProject);
        sqlMapGeneratorConfiguration.addProperty("enableSubPackages","true");

        / * Package name and location to generate DAO * / 
        JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration ();
        javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
        javaClientGeneratorConfiguration.setTargetPackage (daoTargetPackage);
        javaClientGeneratorConfiguration.setTargetProject (daoTargetProject);
        javaClientGeneratorConfiguration.addProperty("enableSubPackages","true");


        context.setCommentGeneratorConfiguration(commentGeneratorConfiguration);
        context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
        context.setJavaTypeResolverConfiguration(javaTypeResolverConfiguration);
        context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
        context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
        context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
        

        tableList.stream().forEach(tableEntity->{
            TableConfiguration tableConfiguration = new TableConfiguration(context);
            tableConfiguration.setTableName(tableEntity.getTableName());
            tableConfiguration.setDomainObjectName(tableEntity.getDomainObjectName());
       // If you want to remove the Example, do this
       tableConfiguration.setUpdateByExampleStatementEnabled(false);
       tableConfiguration.setCountByExampleStatementEnabled(false);
       tableConfiguration.setDeleteByExampleStatementEnabled(false);
       tableConfiguration.setSelectByExampleStatementEnabled(false);
       tableConfiguration.setSelectByExampleQueryId(false);
       
       // Sometiimes, your table' name like 't_teacher', how to make it to be a Camel style?
       tableConfiguration.addProperty("useActualColumnNames", "false");
context.addTableConfiguration(tableConfiguration); }); Configuration config
= new Configuration(); config.addClasspathEntry(classpathEntry); config.addContext(context); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = null; try { myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } catch (InvalidConfigurationException e) { e.printStackTrace (); } catch (InterruptedException e) { e.printStackTrace (); } catch (IOException e) { e.printStackTrace (); } catch (SQLException e) { e.printStackTrace (); } } }

TableEntity entity class

package com.snowy.fastgov.generator;

import lombok.Data;

@Data
public class TableEntity {
    private String tableName;
    private String domainObjectName;
}

 

Guess you like

Origin www.cnblogs.com/Night-Watch/p/12674924.html