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

Example Analysis of mybatis plus automatic Generator

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the example analysis of mybatis plus automatic generator for you. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Parsing of mybatis plus automatic Generator

Use this to generate entity service controller layers very quickly

1. Add dependency

The dependencies of the template engine should also be imported, otherwise the operation will report an error.

Com.baomidou mybatis-plus-generator 3.3.1.tmp org.freemarker freemarker 2.3.30 2. Write a class as an entry to the automatic generator

When you are here, be sure to copy the import package, otherwise it is easy to make mistakes.

Package van.generator;import java.util.ArrayList;import java.util.List;import java.util.Scanner;import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;import com.baomidou.mybatisplus.core.toolkit.StringPool;import com.baomidou.mybatisplus.core.toolkit.StringUtils;import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.InjectionConfig;import com.baomidou.mybatisplus.generator.config.DataSourceConfig;import com.baomidou.mybatisplus.generator.config.FileOutConfig;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.TemplateConfig;import com.baomidou.mybatisplus.generator.config.po.TableInfo;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;/** * @ author Van * @ date 2020-5-1-15:43 * / public class CodeGenerator {/ * *

* read console contents *

* / public static String scanner (String tip) {Scanner scanner = new Scanner (System.in); StringBuilder help = new StringBuilder (); help.append ("Please enter" + tip + ":"); System.out.println (help.toString ()); if (scanner.hasNext ()) {String ipt = scanner.next () If (StringUtils.isNotEmpty (ipt)) {return ipt;}} throw new MybatisPlusException ("Please enter correct" + tip + "!") ;} / * RUN THIS * / 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 + "/ ad-sponsor/src/main/java/van") Gc.setAuthor ("van"); gc.setOpen (false); mpg.setGlobalConfig (gc); / / data source configuration DataSourceConfig dsc = new DataSourceConfig (); dsc.setUrl ("jdbc:mysql://localhost:3306/ad?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8"); / / dsc.setSchemaName ("public"); dsc.setDriverName ("com.mysql.jdbc.Driver") Dsc.setUsername ("root"); dsc.setPassword ("321asd"); mpg.setDataSource (dsc); / / package configuration PackageConfig pc = new PackageConfig (); pc.setModuleName (scanner ("module name")); pc.setParent ("com.baomidou.mybatisplus.samples.generator"); mpg.setPackageInfo (pc) / / Custom configuration InjectionConfig cfg = new InjectionConfig () {@ Override public void initMap () {/ / to do nothing}}; List focList = new ArrayList () FocList.add (new FileOutConfig ("/ templates/mapper.xml.ftl") {@ Override public String outputFile (TableInfo tableInfo) {/ / Custom input file name return projectPath + "/ mybatis-plus-sample-generator/src/main/resources/mapper/" + pc.getModuleName () + "/" + tableInfo .getEntityName () + "Mapper" + StringPool.DOT_XML }}); cfg.setFileOutConfigList (focList); mpg.setCfg (cfg); mpg.setTemplate (new TemplateConfig (). SetXml (null)); / / StrategyConfig strategy = new StrategyConfig (); strategy.setNaming (NamingStrategy.underline_to_camel); strategy.setColumnNaming (NamingStrategy.underline_to_camel) Strategy.setSuperEntityClass ("com.baomidou.mybatisplus.samples.generator.common.BaseEntity"); strategy.setEntityLombokModel (true); strategy.setSuperControllerClass ("com.baomidou.mybatisplus.samples.generator.common.BaseController"); strategy.setInclude (scanner ("Table name")); strategy.setSuperEntityColumns ("id"); strategy.setControllerMappingHyphenStyle (true); strategy.setTablePrefix (pc.getModuleName () + "_"); mpg.setStrategy (strategy) / / to select a freemarker engine, you need to specify the following. Note that there must be pom dependencies! Mpg.setTemplateEngine (new FreemarkerTemplateEngine ()); mpg.execute ();}} 3. Modify the code

1. Global configuration

two。 Data source configuration

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