In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the mybatis-plus code generator configuration example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Recently, when I was playing with a project, I found that I wasted a lot of time writing ~ so I thought of the code generator, mybatis-plus, which I used once before, and then reimplemented it.
Just make sure to modify the corresponding configuration.
Dependence:
Com.baomidou mybatis-plus-boot-starter 2.2.0 mysql mysql-connector-java 5.1.47 freemarker freemarker 2.3.8
Code:
Package cn.zytao.taosir.generator; 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.converts.MySqlTypeConvert;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 com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; / *
* mybaits-plus code generator *
* / public class Generator {/ *
* MySQL generation demo *
* / public static void main (String [] args) {AutoGenerator mpg = new AutoGenerator (); / / Select freemarker engine, default Veloctiy mpg.setTemplateEngine (new FreemarkerTemplateEngine ()); / / Global configuration GlobalConfig gc = new GlobalConfig (); gc.setAuthor ("taosir"); gc.setOutputDir ("Flavado /"); gc.setFileOverride (false); / / whether to overwrite files with the same name. Default is false gc.setActiveRecord (true). / / if you don't need the ActiveRecord feature, change it to false gc.setEnableCache (false); / / XML second-level cache gc.setBaseResultMap (false); / / XML ResultMap gc.setBaseColumnList (false); / / XML columList / * Custom file naming, note that% s will automatically populate the table entity attributes! * / gc.setMapperName ("% sMapper"); / / gc.setXmlName ("% sDao"); gc.setServiceName ("% sService"); gc.setServiceImplName ("% 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); / / Note! There is a default type conversion in processTypeConvert. If it is not the effect you want, please customize the return, otherwise return directly as follows. Return super.processTypeConvert (fieldType);}}); dsc.setDriverName ("com.mysql.jdbc.Driver"); dsc.setUsername ("root"); dsc.setPassword ("root"); dsc.setUrl ("jdbc:mysql://localhost:3306/taosir?useUnicode=true&characterEncoding=utf8"); mpg.setDataSource (dsc); / / Policy configuration StrategyConfig strategy = new StrategyConfig (); / / strategy.setCapitalMode (true) / / Global uppercase naming ORACLE Note / / strategy.setTablePrefix (new String [] {"user_"}); / / table prefix strategy.setNaming (NamingStrategy.nochange); / / Table name generation strategy strategy.setInclude (new String [] {"user"}); / / tables to be generated / / strategy.setExclude (new String [] {"test"}) / / exclude generated tables / / Custom entity parent class / / strategy.setSuperEntityClass ("com.baomidou.demo.TestEntity"); / / Custom entity, public field / / strategy.setSuperEntityColumns (new String [] {"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"); / / [entity] whether to generate field constants (default false) / / public static final String ID = "id" / / strategy.setEntityColumnConstant (true); / / [entity] is the builder model (default false) / / public User setName (String name) {this.name = name; return this;} / / strategy.setEntityBuilderModel (true); mpg.setStrategy (strategy); / / package configuration PackageConfig pc = new PackageConfig (); pc.setParent ("cn.zytao.taosir"); / / pc.setModuleName ("test"); mpg.setPackageInfo (pc) / / injection custom configuration, you can use cfg.abc [but not] / / InjectionConfig cfg = new InjectionConfig () {/ / @ Override// public void initMap () {/ / Map map = new HashMap () in VM; / / map.put ("abc", this.getConfig (). GetGlobalConfig (). GetAuthor () + "- mp"); / / this.setMap (map) / /} / Custom xxList.jsp generation / / List focList = new ArrayList (); / / focList.add (new FileOutConfig ("/ template/list.jsp.vm") {/ / @ Override// public String outputFile (TableInfo tableInfo) {/ Custom input file name / / return "DGRAPR _" + tableInfo.getEntityName () + ".jsp" / /} / /}); / / cfg.setFileOutConfigList (focList); / / mpg.setCfg (cfg); / adjust the xml generation directory demonstration / / focList.add (new FileOutConfig ("/ templates/mapper.xml.vm") {/ / @ Override// public String outputFile (TableInfo tableInfo) {/ / return "/ develop/code/xml/" + tableInfo.getEntityName () + ".xml" / /} / /}); / / cfg.setFileOutConfigList (focList); / / mpg.setCfg (cfg); / turn off the default xml generation and adjust the generation to the root directory / / TemplateConfig tc = new TemplateConfig (); / / tc.setXml (null); / / mpg.setTemplate (tc) / / Custom template configuration, which can be modified under copy source code mybatis-plus/src/main/resources/templates. / / place it under the src/main/resources/templates directory of your own project. You can either not configure the default name or customize the template name / / TemplateConfig tc = new TemplateConfig (); / / tc.setController ("..."); / / tc.setEntity ("...") / / tc.setMapper ("..."); / / tc.setXml ("..."); / / tc.setService ("..."); / / tc.setServiceImpl ("..."); / / if any of the above modules are set to empty OR Null, the module will not be generated. / / mpg.setTemplate (tc); / / generate mpg.execute (); / / print injection settings [nonexistent] / / System.err.println (mpg.getCfg (). GetMap (). Get ("abc"));}}
Build a project and drop it, modify the relevant configuration, and run is done.
Thank you for reading this article carefully. I hope the article "sample Analysis of mybatis-plus Code Generator configuration" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.