1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| package com.ifkal.demo.generate;
import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.toolkit.StringUtils; 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.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import java.util.Scanner;
public class CodeGenerator {
public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); System.out.println("请输入" + tip + ":"); 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(); String projectPath = System.getProperty("user.dir");
GlobalConfig gc = new GlobalConfig() .setOutputDir(projectPath + "/src/main/java") .setAuthor("ifkal") .setOpen(false); mpg.setGlobalConfig(gc);
DataSourceConfig dsc = new DataSourceConfig() .setUrl("jdbc:sqlite:" + projectPath + "/identifier.sqlite") .setDbType(DbType.SQLITE) .setDriverName("org.sqlite.JDBC"); mpg.setDataSource(dsc);
PackageConfig pc = new PackageConfig() .setModuleName(scanner("模块名")) .setParent("com.ifkal.demo"); mpg.setPackageInfo(pc);
StrategyConfig strategy = new StrategyConfig() .setNaming(NamingStrategy.underline_to_camel) .setColumnNaming(NamingStrategy.underline_to_camel) .setEntityLombokModel(true) .setRestControllerStyle(true) .setInclude(scanner("表名,多个英文逗号分割").split(",")) .setControllerMappingHyphenStyle(true) .setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new VelocityTemplateEngine()); mpg.execute(); } }
|