The main is to: 1. Assemble your own model name to generate the code with a number
2. Dynamically get path according to model name
3. Generate code according to model judgment, only generate the code of the current model object
1.maven dependent
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1-gamma</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
Note: After adding this dependency, you need to remove the original mybatis and mybatis-spring dependencies to avoid conflicts because Mybatis-plus contains both
2. Let's take a look at my project structure
2. Automatically generate Code tool class
2.1 Use a number to assemble their own model name to generate the code
String[] models = {"Ssm-mapper", "Ssm-model", "Ssm-service", "Ssm-web"};
2.2. Dynamically get path according to model name
2.3 Generate code according to model judgment, only generate the code of the current model object
3. Tool class complete code
Package com.spf.common.generator;
Import Com.baomidou.mybatisplus.generator.AutoGenerator;
Import Com.baomidou.mybatisplus.generator.InjectionConfig;
Import com.baomidou.mybatisplus.generator.config.*;
Import Com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
Import Com.baomidou.mybatisplus.generator.config.po.TableInfo;
Import Com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
Import Com.baomidou.mybatisplus.generator.config.rules.DbType;
Import Com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
Import Java.io.File;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map; /** * @Auther SHUPF * @Create 2017/7/5 0005 * * public class Mybatisplusutils {public static void main (string[] A
RGS) {string[] models = {"Ssm-mapper", "Ssm-model", "Ssm-service", "Ssm-web"};
for (String model:models) {shell (model);
}} private static void Shell (String model) { File File = new file (model);
String path = File.getabsolutepath ();
Path = path.substring (0, Path.lastindexof (file.separator));
Autogenerator mpg = new autogenerator ();
Global configuration GlobalConfig GC = new GlobalConfig ();
Gc.setoutputdir (path+ "/src/main/java");
Gc.setfileoverride (TRUE);
Gc.setactiverecord (TRUE); Gc.setenablecache (false);//XML Level two cache Gc.setbaseresultmap (true);//XML Resultmap Gc.setbasecolumnlist (False
)//XML columlist gc.setauthor ("SPF");
Custom file naming, note%s will automatically populate the table entity properties.
Gc.setmappername ("%smapper");
Gc.setxmlname ("%smapper");
Gc.setservicename ("I%sservice");
Gc.setserviceimplname ("I%sserviceimpl");
Gc.setcontrollername ("%scontroller");
Mpg.setglobalconfig (GC);
Data Source Configuration Datasourceconfig DSC = new Datasourceconfig ();
Dsc.setdbtype (Dbtype.mysql);
Dsc.settypeconvert (New Mysqltypeconvert () { Custom database table field type conversion "optional" @Override public dbcolumntype Processtypeconvert (String fieldtype)
{System.out.println ("conversion type:" + FieldType); Attention..
Processtypeconvert There is a default type conversion, if it is not the effect you want please customize the return, not the following direct return.
Return Super.processtypeconvert (FieldType);
}
});
Dsc.setdrivername ("Com.mysql.jdbc.Driver");
Dsc.setusername ("root");
Dsc.setpassword ("SPF940805.");
Dsc.seturl ("Jdbc:mysql:///test?characterencoding=utf8");
Mpg.setdatasource (DSC);
Policy configuration Strategyconfig strategy = new Strategyconfig (); Strategy.setcapitalmode (TRUE);//Global uppercase naming ORACLE note Strategy.settableprefix (new string[] {"Tlog_", "Tsys_"}); Here you can modify your table prefix strategy.setnaming (namingstrategy.underline_to_camel);//table name generation Policy Strategy.setinclude (new Stri Ng[] {"User_info"}); The table//strategy.setexclude (New string[]{"test"}) that needs to be generated; Exclude generated table/self-setSemantic Entity Parent class//strategy.setsuperentityclass ("com.spf.model.Entity");
Custom entity, public field//strategy.setsuperentitycolumns (new string[] {"test_id", "Age"});
Custom Mapper Parent class//Strategy.setsupermapperclass ("Com.baomidou.demo.TestMapper");
Custom Service Parent class//strategy.setsuperserviceclass ("Com.baomidou.demo.TestService");
Custom Service implementation Class parent class//strategy.setsuperserviceimplclass ("Com.baomidou.demo.TestServiceImpl");
Custom Controller Parent class//strategy.setsupercontrollerclass ("Com.baomidou.demo.TestController");
Whether entity generates field constants (default false)//public static final String ID = "test_id";
Strategy.setentitycolumnconstant (TRUE);
Whether "entity" is a builder model (default false)//public User SetName (String name) {this.name = name;
Strategy.setentitybulidermodel (TRUE);
Mpg.setstrategy (strategy);
Package configuration Packageconfig pc = new Packageconfig (); Pc.setpArent ("COM.SPF");
Pc.setcontroller ("Controller");
Pc.setentity ("entity");
Pc.setmapper ("Mapper");
Pc.setservice ("service");
Pc.setserviceimpl ("Impl");
Pc.setmodulename ("test");
Mpg.setpackageinfo (PC);
Inject custom configuration, you can use CFG.ABC in VM injectionconfig cfg = new Injectionconfig () {@Override
public void Initmap () {map<string, object> Map = new hashmap<string, object> ();
Map.put ("abc", This.getconfig (). Getglobalconfig (). Getauthor () + "-mp");
This.setmap (map);
}
};
Custom xxlist.jsp Generate list<fileoutconfig> foclist = new arraylist<fileoutconfig> (); Foclist.add (New Fileoutconfig ("/TEMPLATE/LIST.JSP.VM") {//@Override//Public String outputfile (tablei NFO tableinfo) {////Custom input file name/return "D://my_" + tableinfo.getentityname () + ". JSP";//}
// });
Cfg.setfileoutconfiglist (foclist);
Mpg.setcfg (CFG); Adjust XML build directory demo Foclist.add (new Fileoutconfig ("/templates/mapper.xml.vm") {@Override pub Lic String outputfile (Tableinfo tableinfo) {return "/develop/code/xml/" + tableinfo.getentityname () + ". x
ML ";
}
});
Cfg.setfileoutconfiglist (foclist);
Mpg.setcfg (CFG);
Closes the default XML build, adjusts the build to the root directory templateconfig TC = new Templateconfig ();
if ("Ssm-mapper". Equals (model)) {Tc.setcontroller (null);
Tc.setentity (NULL);
Tc.setservice (NULL);
Tc.setserviceimpl (NULL);
else if ("Ssm-model". Equals (model)) {Tc.setcontroller (null);
Tc.setservice (NULL);
Tc.setserviceimpl (NULL);
Tc.setmapper (NULL);
Tc.setxml (NULL);
else if ("Ssm-service". Equals (model)) {Tc.setcontroller (null); Tc.setmapper (NULL);
Tc.setxml (NULL);
Tc.setentity (NULL);
else if ("Ssm-web". Equals (model)) {tc.setmapper (null);
Tc.setxml (NULL);
Tc.setservice (NULL);
Tc.setserviceimpl (NULL);
Tc.setentity (NULL);
} mpg.settemplate (TC); Custom template configuration, you can copy the source code mybatis-plus/src/main/resources/template the following content modification,//placement of their own items in the Src/main/resources/template directory,
The default name can not be configured, you can also customize the template name//templateconfig TC = new Templateconfig ();
Tc.setcontroller ("...");
Tc.setentity ("...");
Tc.setmapper ("...");
Tc.setxml ("...");
Tc.setservice ("...");
Tc.setserviceimpl ("...");
If you set NULL OR NULL on any of the modules, the module will not be generated.
Mpg.settemplate (TC);
Execute generated mpg.execute ();
The print injection settings "can be No" System.err.println (Mpg.getcfg (). Getmap (). Get ("abc")); }
}
OK.