Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to automatically generate entity classes according to the database by mybatis

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how mybatis automatically generates entity classes according to the database". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

1. Add dependencies

2. Write a code generator

3. Run the main program and enter the table name

1 、 Add dependency com.baomidou mybatis-plus-boot-starter 3.3.2 com.baomidou mybatis-plus-generator 3.3.2 Org.freemarker freemarker 2.3.30 2 、 Write code generator public class CodeGenerator {/ *

* read console contents *

* / public static String scanner (String tip) {Scanner scanner = new Scanner (System.in); System.out.println ("Please enter" + tip + ":"); if (scanner.hasNext ()) {String ipt = scanner.next (); if (StringUtils.isNotBlank (ipt)) {return ipt }} throw new MybatisPlusException ("Please enter correct" + tip + "!") ;} public static void main (String [] args) {/ / Code generator AutoGenerator mpg = new AutoGenerator (); / / Global configuration GlobalConfig gc = new GlobalConfig (); String projectPath = System.getProperty ("user.dir"); gc.setOutputDir (projectPath + "/ check-service/src/main/java"); gc.setAuthor ("raoyujie"); gc.setOpen (false) / / gc.setSwagger2 (true); entity attribute Swagger2 annotation mpg.setGlobalConfig (gc); / / data source configuration DataSourceConfig dsc = new DataSourceConfig (); dsc.setUrl ("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8"); dsc.setDriverName ("com.mysql.cj.jdbc.Driver"); dsc.setUsername ("root") Dsc.setPassword ("root"); mpg.setDataSource (dsc); / / package configuration PackageConfig pc = new PackageConfig (); pc.setParent ("com.hrbust.health.check.service"); mpg.setPackageInfo (pc) / / Custom configuration InjectionConfig cfg = new InjectionConfig () {@ Override public void initMap () {/ / to do nothing}}; / / if the template engine is freemarker String templatePath = "/ templates/mapper.xml.ftl"; / / Custom output configuration List focList = new ArrayList () / / Custom configuration will be given priority to output focList.add (new FileOutConfig (templatePath) {@ Override public String outputFile (TableInfo tableInfo) {/ / Custom output file name. If you set the prefix and suffix for Entity, note here that the name of xml will change accordingly! Return projectPath + "/ check-service/src/main/resources/mapper/" + pc.getModuleName () + "/" + tableInfo.getEntityName () + "Mapper" + StringPool.DOT_XML;}}); cfg.setFileOutConfigList (focList); mpg.setCfg (cfg); / / configuration template TemplateConfig templateConfig = new TemplateConfig (); templateConfig.setXml (null) Mpg.setTemplate (templateConfig); / / Policy configuration StrategyConfig strategy = new StrategyConfig (); strategy.setNaming (NamingStrategy.underline_to_camel); strategy.setColumnNaming (NamingStrategy.underline_to_camel); strategy.setEntityLombokModel (true); strategy.setRestControllerStyle (true); strategy.setInclude (scanner ("Table name, multiple English comma Separation"). Split (",")) Strategy.setControllerMappingHyphenStyle (true); strategy.setTablePrefix (pc.getModuleName () + "_"); mpg.setStrategy (strategy); mpg.setTemplateEngine (new FreemarkerTemplateEngine ()); mpg.execute ();} 3. Run the main program and enter the table name

This is the end of the content of "how mybatis automatically generates entity classes according to the database". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report