How to generate classes automatically by mybatis-plus
This article introduces the relevant knowledge of "how to generate classes automatically by mybatis-plus". 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!
Directory pom depends on executing sqldemo
Pom.xml
Org.springframework.boot spring-boot-starter-web com.baomidou mybatis-plus-generator 3.1.2 mysql mysql-connector-java 8.0.11 org.apache.velocity velocity-engine-core 2.0 org.projectlombok lombok 1.18.8
Execute sql
CREATE TABLE if not exists `category_pic_ int (11) NOT NULL AUTO_INCREMENT, `category_pic_ code `NOT NULL comment 'picture address id', `name` varchar' NOT NULL comment 'product category name, `status`bit (1) NOT NULL default 1 comment' 0 invalid 1 valid', `update_ time`timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `update_ time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP PRIMARY KEY (`id`) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 comment=' product category information'
Execute the code
Public static void main (String [] args) {String projectPath = System.getProperty ("user.dir"); String outputDir = projectPath + "/ src/main/java"; / / generate file output directory String author = "duanlsh"; / / comment author String entity = "ProductCategory"; / / generate file entity name String entityPrefix = "tbl_" / / prefix String [] tableName = "tbl_product_category" .split (","); / / multiple table names, split String packageName = "com.apple"; / / generate the package name of the file / / database String dbUrl = "jdbc:mysql://localhost:3306/grainfull?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC"; DataSourceConfig dataSourceConfig = new DataSourceConfig () DataSourceConfig.setDbType (DbType.MYSQL) .setUrl (dbUrl) .setUsername ("root") .setPassword ("root") .setDriverName ("com.mysql.cj.jdbc.Driver"); GlobalConfig config = new GlobalConfig (); config.setAuthor (author); config.setOutputDir (outputDir); config.setFileOverride (true) Config.setOpen (false); / / whether to open the directory config.setEntityName ("% sModel"); / / add Model config.setServiceName ("% sService") to the entity class; config.setControllerName ("% sController"); config.setServiceImplName ("% sServiceImpl"); config.setMapperName ("% sMapper"); config.setXmlName ("% sMapper"); StrategyConfig strategyConfig = new StrategyConfig () StrategyConfig .setCapitalMode (true) .setEntityLombokModel (true) / / sets whether to use lombook// .setDbColumnUnderline (true) .setNaming (NamingStrategy.underline_to_camel) .setColumnNaming (NamingStrategy.underline_to_camel) .setRestControllerStyle (true) .setControllerMappingHyphenStyle (true) / / .setSuperEntityClass ("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl") / / .setSuperMapperClass ("com.baomidou.mybatisplus.core.mapper.BaseMapper") .setInclude (tableName) / / modify and replace with the table name you need Multiple table name passing array .setTablePrefix (entityPrefix) InjectionConfig cfg = new InjectionConfig () {@ Override public void initMap () {/ / do nothing}}; PackageConfig packageConfig = new PackageConfig (); / / packageConfig.setModuleName ("ProductCategory"); / / the package name added under the parent package is eg: com.apple.ProductCategory packageConfig.setParent (packageName); packageConfig.setController ("controller") PackageConfig.setService ("service"); packageConfig.setServiceImpl ("service.impl"); packageConfig.setEntity ("model"); / / TemplateConfig templateConfig = new TemplateConfig (); / / templateConfig.setEntity ("/ templates/entity2.java"); / / templateConfig.setXml (null); AutoGenerator mpg = new AutoGenerator (); / / mpg.setTemplate (templateConfig); mpg.setGlobalConfig (config) Mpg.setDataSource (dataSourceConfig); mpg.setStrategy (strategyConfig); mpg.setCfg (cfg); mpg.setPackageInfo (packageConfig); mpg.execute ();} "how mybatis-plus automatically generates classes" ends here. Thank you for 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!