In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to automatically generate and configure MybatisPlus code. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
AutoGenerator is the code generator of MyBatis-Plus. Through AutoGenerator, we can quickly generate the code of various modules such as Entity, Mapper, Mapper XML, Service, Controller and so on, which greatly improves the development efficiency.
AutoGenerator mpg = new AutoGenerator (); / / Select freemarker engine, default Veloctiy / / mpg.setTemplateEngine (new FreemarkerTemplateEngine ())
/ / Global configuration GlobalConfig gc = new GlobalConfig (); gc.setOutputDir (System.getProperty ("user.dir") + "/ src/main/java"); gc.setAuthor ("lqh"); gc.setFileOverride (true); / / whether to override gc.setActiveRecord (true); / / change to false gc.setEnableCache (false) if you don't need the ActiveRecord feature; / / XML second-level cache gc.setBaseResultMap (true); / / XML ResultMap gc.setBaseColumnList (true) / / XML columList / / Custom file naming, note that% s will automatically populate the table entity attributes! / / gc.setMapperName ("% sDao"); / / gc.setXmlName ("% sMapper"); / / gc.setServiceName ("MP%sService"); / / gc.setServiceImplName ("% sServiceDiy"); / / gc.setControllerName ("% sAction"); 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! 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.cj.jdbc.Driver"); dsc.setUsername ("root"); dsc.setPassword ("nndog2003"); dsc.setUrl ("jdbc:mysql://127.0.0.1:3306/auto_card?characterEncoding=utf8"); mpg.setDataSource (dsc)
/ / Policy configuration StrategyConfig strategy = new StrategyConfig (); / / strategy.setCapitalMode (true); / / Global uppercase naming ORACLE Note
/ / strategy.setTablePrefix (new String [] {"tb_", "tsys_"}); / / here you can modify your table prefix strategy.setNaming (NamingStrategy.underline_to_camel); / / Table name generation policy / / 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 [] {"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"); / / [entity] whether to generate field constants (default false) / / public static final String ID = "test_id" / / strategy.setEntityColumnConstant (true); / / whether the 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 ("com.mybatisplus"); pc.setModuleName ("mybatis"); pc.setController ("controler"); pc.setEntity ("entity"); pc.setMapper ("mapper"); pc.setService ("service"); pc.setServiceImpl ("serviceImpl"); pc.setXml ("mapperXml"); mpg.setPackageInfo (pc) / / to inject a custom configuration, you can use cfg.abc [but not] InjectionConfig cfg = new InjectionConfig () {[@ Override] (https://my.oschina.net/u/1162528) public void initMap () {Map map = new HashMap (); map.put ("abc", this.getConfig (). GetGlobalConfig (). GetAuthor () + "- mp") in VM) 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 "tableInfo.getEntityName () +" .jsp "; / /} / /}); / / cfg.setFileOutConfigList (focList); / / mpg.setCfg (cfg)
/ / adjust xml generation directory demo focList.add (new FileOutConfig ("/ templates/mapper.xml.vm") {[@ Override] (https://my.oschina.net/u/1162528) public String outputFile (TableInfo tableInfo) {return System.getProperty ("user.dir") + tableInfo.getEntityName () + "Mapper.xml";}}); cfg.setFileOutConfigList (focList); mpg.setCfg (cfg) / / disable 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 [can be left empty]
/ / System.err.println (mpg.getCfg () .getMap () .get ("abc"))
This is how the MybatisPlus code shared by the editor is automatically generated and configured. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.