Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 18:12:43 09月17日 Update

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!

Tags: Configure generate input output entity data code content generator database automatic generation main program more templates knowledge run practical learned and then global Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Xiaomi Microsoft Shulou Technology