Springboot Mybatis Plus自动生成工具类详解代码

网友投稿 708 2022-11-20

Springboot Mybatis Plus自动生成工具类详解代码

Springboot Mybatis Plus自动生成工具类详解代码

前言

代码生成器,也叫逆向工程,是根据数据库里的表结构,自动生成对应的实体类、映射文件和接口。

看到很多小伙伴在为数据库生成实体类发愁,现分享给大家,提高开发效率。

一、pom依赖

com.baomidou

mybatis-plus-boot-starter

3.4.1

com.baomidou

mybatis-plus-generator

3.4.1

org.freemarker

freemarker

2.3.30

二、工具类

package com.his.utils;

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.*;

import com.baomidou.mybatisplus.generator.config.po.TableInfo;

import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

/**

* Mybatis plus代码自动生成

*/

public class MybatisPlusUtil {

public static final String AUTHOR = "dd";

/** 类命名 */

/**

* Entity命名

*/

public static final String FILE_NAME_ENTITY = "%sEntity";

/**

* MAPPER命名

*/

public static final String FILE_NAME_MAPPER = "%sMapper";

/**

* xml命名

*/

public static final String FILE_NAME_XML = "%sMapper";

/**

* Service命名

*/

public static final String FILE_NAME_SERVICE = "%sService";

/**

* ServiceImpl命名

*/

public static final String FILE_NAME_SERVICE_IMPL = "%sDO";

/**

* Controller命名

*/

public static final String FILE_NAME_CONTROLLER = "%sController";

/**

包命名,可以根据自己的项目情况自定义生成后的存放路径

entity默认路径为父目录.entity

mapper默认路径为父目录.mapper

service默认路径为父目录.service

serviceImpl默认路径为父目录.service.impl

controller默认路径为父目录.controller

*/

/**

* PARENT命名

*/

public static final String PACKAGE_NAME_PARENT = "com.his";

/**

* Entity命名

*/

public static final String PACKAGE_NAME_ENTITY = "repository.entity.control";

/**

* MAPPER命名

*/

public static final String PACKAGE_NAME_MAPPER = "repository.mapper.control";

/**

* xml命名

*/

public static final String PACKAGE_NAME_XML = "sys";

/**

* Service命名

*/

public static final String PACKAGE_NAME_SERVICE = "domain.control";

/**

* ServiceImpl命名

*/

public static final String PACKAGE_NAME_SERVICE_IMPL = "domain.control";

/**

* Controller命名

*/

public static final String PACKAGE_NAME_CONTROLLER = "facade.controller.control";

/**

* 读取控制台内容

*/

private static String scanner(String tip) {

Scanner scanner = new Scanner(System.in);

StringBuilder help = new StringBuilder();

help.append("请输入" + tip + ":");

System.out.println(help.toString());

if (scanner.hasNext()) {

String ipt = scanner.next();

if (StringUtils.isNotBlank(ipt)) {

return ipt;

}

}

throw new MybatisPlusException("请输入正确的" + tip + "!");

}

/**

* 运行这个main方法进行代码生成

*/

public static void main(String[] args) {

// 代码生成器

AutoGenerator mpg = new AutoGenerator();

// 全局配置

GlobalConfig gc = new GlobalConfig();

String projectPath = System.getProperty("user.dir");

gc.setOutputDir(projectPath + "/src/main/java");

gc.setFileOverride(true);

gc.setAuthor(AUTHOR);

gc.setOpen(false);

gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false

gc.setEnableCache(false);// XML 二级缓存

gc.setSwagger2(true); // 实体属性 Swagger2 注解

gc.setBaseResultMap(true);

gc.setBaseColumnList(true);

gc.setEntityName(FILE_NAME_ENTITY);

gc.setMapperName(FILE_NAME_MAPPER);

gc.setXmlName(FILE_NAME_XML);

gc.setServiceName(FILE_NAME_SERVICE);

gc.setServiceImplName(FILE_NAME_SERVICE_IMPL);

gc.setControllerName(FILE_NAME_CONTROLLER);

mpg.setGlobalConfig(gc);

// 数据源配置

DataSourceConfig dsc = new DataSourceConfig();

dsc.setUrl("jdbc:oracle:thin:@ip:port/test");

dsc.setDriverName("oracle.jdbc.OracleDriver");

dsc.setUsername("user");

dsc.setPassword("pass");

mpg.setDataSource(dsc);

//包配置

http:// PackageConfig pc = new PackageConfig();

pc.setModuleName(null);

pc.setParent(PACKAGE_NAME_PARENT);

pc.setController(PACKAGE_NAME_CONTROLLER);

pc.setService(PACKAGE_NAME_SERVICE);

pc.setServiceImpl(PACKAGE_NAME_SERVICE_IMPL);

pc.setMapper(PACKAGE_NAME_MAPPER);

pc.setEntity(PACKAGE_NAME_ENTITY);

pc.setXml(PACKAGE_NAME_XML);

mpg.setPackageInfo(pc);

// 策略配置

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("表名,多个英文逗号分割").split(","));

strategy.setControllerMappingHyphenStyle(true);

// 设置表前缀

strategy.setTablePrefix("IEMR_");

mpg.setStrategy(strategy);

mpg.setTemplateEngine(new FreemarkerTemplateEngine());

// 自定义配置

InjectionConfig cfg = new InjectionConfig() {

@Override

public void initMap() {

// to do nothing

}

};

// 如果模板引擎是 freemarker

String templatePath = "/templates/mapper.xml.ftl";

// 如果模板引擎是 velocity

// String templatePath = "/templates/mapper.xml.vm";

// 自定义输出配置

List focList = new ArrayList<>();

// 自定义配置会被优先输出

focList.add(new FileOutConfig(templatePath) {

@Override

public String outputFile(TableInfo tableInfo) {

// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!

return projectPath + "/src/main/resources/mapper/"

+ "/" + tableInfo.getMapperName() + StringPool.DOT_XML;

}

});

cfg.setFileOutConfigList(focList);

mpg.setCfg(cfg);

// 配置模板

TemplateConfig templateConfig = new TemplateConfig();

templateConfig.setXml(null);

mpg.setTemplate(templateConfig);

mpg.execute();

}

}

结尾

感谢大家的耐心阅读,如有建议请私信或评论留言。如有收获,劳烦支持,关注、点赞、评论、收藏均可,博主会经常更新,与大家共同进步

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:安卓实训之开发环境搭建
下一篇:吴恩达机器学习系列课程-基础知识
相关文章

 发表评论

暂时没有评论,来抢沙发吧~