web123456

Configuration of MyBatis-Plus automatic code generation tool

  • public class JavaGenerator {
  • /**
  • * <p>
  • * Read the console content, used to enter the module to be generated (in the form of a folder after generation) and table name
  • * </p>
  • */
  • public static String scanner(String tip) {
  • Scanner scanner = new Scanner(System.in);
  • StringBuilder help = new StringBuilder();
  • ("Please enter" + tip + ":");
  • (());
  • if (()) {
  • String ipt = scanner.next();
  • if ((ipt)) {
  • return ipt;
  • }
  • }
  • throw new MybatisPlusException("Please enter the correct one" + tip + "!");
  • }
  • public static void main(String[] args) {
  • //Code Generator
  • AutoGenerator mpg = new AutoGenerator();
  • //Global configuration
  • GlobalConfig gc = new GlobalConfig();
  • String projectPath = ("");//Get the path to the current project
  • (projectPath + "/src/main/java");//Configure the generated code directory
  • ((""));//Get the current username
  • (false);
  • ("%sDAO");//Configure the naming method of the mapper after generation
  • ("%sBO");//Configure the naming method of entity classes after generation
  • (gc);
  • //Data source configuration (fill in according to your actual database configuration information)
  • DataSourceConfig dsc = new DataSourceConfig();
  • ();
  • ("jdbc:mysql://localhost:3306/itsys_bastion?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC");
  • // ("public");
  • ("");
  • ("root");
  • ("shugege");
  • (dsc);
  • //Package configuration
  • PackageConfig pc = new PackageConfig();
  • String moduleName = scanner("Role Name");//The module name can be determined according to the actual situation.
  • (".mybatis_plus");//Your own bag name
  • //Configure path
  • ("");
  • ("mapper." + moduleName);
  • ("service." + moduleName);
  • ("service." + moduleName + ".impl");
  • //Do not generate two named packages, Controller and xml (not**and*.xml file)
  • //Because our project does not have a Controller layer and uses MyBatis annotations, there is no need to generate Controller and XML files
  • ("");
  • ("");
  • (pc);
  • //Custom configuration
  • InjectionConfig cfg = new InjectionConfig() {
  • @Override
  • public void initMap() {
  • // to do nothing
  • }
  • };
  • (new IFileCreate() {
  • @Override
  • public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
  • //This configures the file type you don't want to generate
  • //Do not generate controller files
  • if (fileType == ) {
  • return false;
  • }
  • //No xml file generated
  • if (fileType == ) {
  • return false;
  • }
  • //Allows to generate template files
  • return true;
  • }
  • });
  • (cfg);
  • //Configuration template I don't use a custom template here so I can't use it
  • //TemplateConfig templateConfig = new TemplateConfig();
  • //Configure custom output templates
  • //("");
  • //("");
  • //Policy Configuration
  • StrategyConfig strategy = new StrategyConfig();
  • (NamingStrategy.underline_to_camel);//Table name generation strategy
  • (NamingStrategy.underline_to_camel);
  • //Custom entity parent class
  • // ("");
  • (true);
  • (true);
  • // (true);
  • //Public parent class
  • // ("If you don't have your own parent controller, don't need to set it!");
  • //Custom entities, public fields
  • // ("id");
  • (scanner("Table name, multiple English commas split").split(","));
  • // (true);
  • //Here you can modify to your table prefix
  • // (() + "_");
  • ("t_");
  • (strategy);
  • (new FreemarkerTemplateEngine());
  • ();
  • }
  • }