代码生成器——java代码自动生成MAVEN包和类文件

package cn.ps;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
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.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

maven代码生成器

public class GodeGenerator {
	  /* public static String scanner(String tip) {
	        Scanner scanner = new Scanner(System.in);
	        StringBuilder help = new StringBuilder();
	        help.append("请输入" + tip + ":");
	        System.out.println(help.toString());
	        if (scanner.hasNext()) {
	            String ipt = scanner.next();
	            if (StringUtils.isNotEmpty(ipt)) {
	                return ipt;
	            }
	        }
	        throw new MybatisPlusException("请输入正确的" + tip + "!");
	    }
	*/
	public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();//mybatis plus包

//全局配置
        GlobalConfig gc = new GlobalConfig();
        final String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("欧阳**");
        gc.setOpen(false);
        mpg.setGlobalConfig(gc);

//数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://192.168.0.253:3306/school?			   useUnicode=true&useSSL=false&characterEncoding=utf8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");	//用户名
        dsc.setPassword("ps123456");//密码
        mpg.setDataSource(dsc);

//包配置
        final PackageConfig pc = new PackageConfig();
       //数据库名
        pc.setModuleName("users");	//表名
        pc.setParent("com.baomidou.ant");
        mpg.setPackageInfo(pc);

//自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));
        
// 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
//        strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        strategy.setInclude("users");
        strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
	}
}


//依赖文件
com.baomidou mybatis-plus 3.0.5
	
		mysql
		mysql-connector-java
		5.1.14
	
	
		org.springframework
		spring-core
		4.3.20.RELEASE
	
	
		org.freemarker
		freemarker
		2.3.28
	
	
		org.projectlombok
		lombok
		1.18.4
		provided
	
	
		org.springframework
		spring-webmvc
		4.3.20.RELEASE
	

	
		org.slf4j
		slf4j-api
		1.7.22
	
	
		org.slf4j
		slf4j-log4j12
		1.7.22
	
	

```

你可能感兴趣的:(mybatisPlus,代码生成器,maven自动生成类,java快速生成代码)