MybatisPlus自动生成映射文件

MybatisPlus自动生成映射文件

一、pom.xml

pom.xml添加以下依赖:

		
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.1
        
		
            com.baomidou
            mybatis-plus-boot-starter
            3.0.3
        
        
            com.baomidou
            mybatis-plus
            3.0.3
        
        
            org.mybatis
            mybatis-spring
            1.3.2
        
        
            org.freemarker
            freemarker
            2.3.23
        

二、MybatisPlusUtil工具类

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据库生成JAVA类
 */
public class MybatisPlusUtils {

    public static String[] getAllTables() {
        List list = new ArrayList<>();
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hats?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "root");
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet tables = metaData.getTables(null, null, null, new String[]{"TABLE"});
            while (tables.next()) {
                list.add(tables.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list.toArray(new String[]{});
    }

    /**
     * 自动代码生成类
     *
     * @param includeTables
     * @param excludeTables
     */
    public static void generateMysql(String[] includeTables, String[] excludeTables, Boolean db1) {
        AutoGenerator autoGenerator = new AutoGenerator();
        /**
         * 数据库配置
         */

        buildDb(autoGenerator);
        /**
         * 数据库表配置
         */
        StrategyConfig strategyConfig = new StrategyConfig();
        // 驼峰命名
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        // 需要生成的表名
//        includeTables = new String[]{"user"};
        // 不需要生成的表名
//        excludeTables = new String[]{};
        strategyConfig.setInclude(includeTables);
        strategyConfig.setExclude(excludeTables);
        strategyConfig.setTablePrefix("");
        autoGenerator.setStrategy(strategyConfig);

        /**
         * 全局配置
         */
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setAuthor("Du Ronghong");
        globalConfig.setDateType(DateType.ONLY_DATE);
        globalConfig.setActiveRecord(true);
        globalConfig.setIdType(IdType.INPUT);
        globalConfig.setOutputDir("D:/test/video");
        globalConfig.setMapperName("%sMapper");
        globalConfig.setServiceName("I%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setXmlName("%sMapper");
        globalConfig.setSwagger2(true);
        globalConfig.setFileOverride(true);
        globalConfig.setBaseColumnList(false);
        globalConfig.setBaseResultMap(false);
        globalConfig.setEnableCache(false);
        autoGenerator.setGlobalConfig(globalConfig);

        /**
         * 包名配置
         */
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("");
        packageConfig.setEntity("com.hanergy.out.entity");
        packageConfig.setMapper("com.hanergy.out.dao");
        packageConfig.setXml("mapper");
        packageConfig.setService("com.hanergy.out.service");
        packageConfig.setServiceImpl("com.hanergy.out.service.impl");
        packageConfig.setController("com.hanergy.out.controller");

        autoGenerator.setPackageInfo(packageConfig);
        // 采用默认模板,自定义模板参考@see com.baomidou.mybatisplus.generator.config.TemplateConfig
//        TemplateConfig templateConfig = new TemplateConfig();
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
        autoGenerator.execute();
    }


    private static void buildDb(AutoGenerator autoGenerator) {
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        dataSourceConfig.setUsername("root");			//用户名
        dataSourceConfig.setPassword("root123");	//密码
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/hanergy_system?useUnicode=true&characterEncoding=utf-8&useSSL=false");
        autoGenerator.setDataSource(dataSourceConfig);
    }
    //com.hanergy.modules.activiti.entity
    public static void main(String[] args) {
        //String[] includeTables = {"hr_kpi_dept_person","sys_user"};
        // 要生成映射的表名,可设置多个
        String[] includeTables = {"middle_protection"};
        generateMysql(includeTables,null,false);
    }
}

右键 Run 运行之后就会生成一堆映射文件,将之copy到自己的项目中就可以了
MybatisPlus自动生成映射文件_第1张图片
注意: 生成的dao层文件需要自己添加@Mapper注解

你可能感兴趣的:(代码文档)