关键词:
前言
代码生成器,也叫逆向工程,是根据数据库里的表结构,自动生成对应的实体类、映射文件和接口。
看到很多小伙伴在为数据库生成实体类发愁,现分享给大家,提高开发效率。
一、pom依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
二、工具类
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);
//包配置
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<FileOutConfig> 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();
结尾
- 感谢大家的耐心阅读,如有建议请私信或评论留言。
- 如有收获,劳烦支持,关注、点赞、评论、收藏均可,博主会经常更新,与大家共同进步
电商项目后端框架springbootmybatisplus(代码片段)
后端框架基础1.代码自动生成工具mybatis-plus(1)首先需要添加依赖文件<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.2</version></dependency><dependency><groupId>mysql&l... 查看详情
springbootmybatisplus集成shiro实现权限控制(代码片段)
1.创建数据库表。由于时间仓促,数据库表设计不太合理,后期会更改/*NavicatPremiumDataTransferSourceServer:本地SourceServerType:MySQLSourceServerVersion:80021SourceHost:localhost:3306SourceSchema:logindatabaseTargetServerType:MySQLTarge 查看详情
先让springbootmybatisplus跑起来(代码片段)
架空一切前提条件,只讲怎么让它跑起来,实现基本的增删改查操作!架空一切前提条件,只讲怎么让它跑起来,实现基本的增删改查操作!假设:1、MySQL里已经有一张user表;2、已经安装IDEA;3、已经新建一个SpringBoot项目;4、... 查看详情
springbootmybatisplus动态查询
参考技术A最近做的项目中用到了mp,以前以为只能单表查呢,现在发现多表也可以,下面使用注解型sql做示例说明mapper-end- 查看详情
springbootmybatisplus报错,依赖冲突问题。
今天使用springBoot热部署插件,引入了依赖,然后发现 查询mybatis报错,描述是 一个字段不存在,各种检查相关配置和数据库,都没有问题。然后深入查找,发现下面的依赖和mybatisplus冲突了。&n... 查看详情
eclipse代码自动提示,eclipse设置代码自动提示
eclipse代码自动提示,eclipse设置代码自动提示eclipse是很多JAVA开发者基本上都用的工具,用它可以很方便的开发JAVA代码,当编写JAVA代码时,大部分人都是按组合键【Alt+/】来获取代码提示的,其实可以设置只要输入代码就自动提... 查看详情
sql修改自动增量自动增量(代码片段)
手写代码自动实现自动布局,即autolayout的使用
手写代码自动实现自动布局,即AutoLayout的使用,有需要的朋友可以参考下。这里要注意几点:对子视图的约束,若是基于父视图,要通过父视图去添加约束。对子视图进行自动布局调整,首先对UIView的一个属性设置,这是因为... 查看详情
phpmaterialisecss|自动生成自动填充功能(代码片段)
phpmaterialisecss|自动生成自动填充功能(代码片段)
自动化打包之fastlane--代码签名和项目配置(代码片段)
自动化打包之fastlane–(1)研究之必须提前了解的几点自动化打包之fastlane–(2)fastlaneinit图文教程自动化打包之fastlane–(3)安装fir插件图文教程自动化打包之fastlane–(4)安装其他插件自动化打包之fastlane–(5)自动打包到蒲公英自动化... 查看详情
自动曝光修复算法附完整c代码(代码片段)
众所周知,图像方面的3A算法有:AF自动对焦(AutomaticFocus)自动对焦即调节摄像头焦距自动得到清晰的图像的过程AE自动曝光(AutomaticExposure)自动曝光的是为了使感光器件获得合适的曝光量AW自动白平衡(AutomaticWhiteBalance)白平衡的本质... 查看详情
saltstack(代码片段)
自动化运维,可实现日常设备监控、主动发现问题、自动分析定位、基于标准化流程工具规范化处理、通过自动化运维操作工具处理修复等功能,最终实现监管治自动化运维。监控自动化数据采集自动化数据分析自动化日常巡检... 查看详情
自动轨迹绘制(代码片段)
...1.1问题分析自动轨迹绘制需求:根据脚本来绘制图形不是代码实现而是读取文件中数据绘制轨迹数据脚本是自动化最终要的第一步1.2自动轨迹绘制300,0,144,1,0,0300,0,144,0,1,0300,0,144,0,0,1300,0,144,1,1,0300,0,108,0,1,1二、自动轨迹绘制实例讲... 查看详情
vscode如何设置自动保存时自动格式化代码(代码片段)
Vscode快速格式化代码:shift+alt+f也可以设置当我们保存页面的时候自动格式化代码:1)文件------.>【首选项】---------->【设置】;2)搜索emmet.include;3)在settings.json下的【工作区设置】中添加以下语句:&... 查看详情
zabbix自动发现自动注册(代码片段)
一、自动发现1. 2自动注册详细配置 二、自动注册1..2.自动注册详细配置 三自动安装zabbix客户端脚本#!/bin/bash#robinpath=‘/etc/zabbix/zabbix_agentd.d/‘#安装zabbix源rpm-ivhhttp://repo.zabbix.com/zabbix/3.4/rhel/ 查看详情
百度自动推送代码的作用
...资源平台下的站点支持模块中有一个连接提交,下有一段代码叫做自动推送代码,只要在这个平台下认证了你的网站就可以,将自动推送代码加进去。我在https://www.jianzhumuju.com方圆扣这个站点上做了一下尝试,看看这段代码到底... 查看详情
运维自动化方案(代码片段)
...统安装系统优化系统监控日志监控和收集应用自动化部署代码自动化部署自动化测试自动化更新自动化扩容配置文件管理系统自动化安装和优化系统的自动化安装和优化,可以使用到自动化工具cobbler结合kickstart完成。优点:可... 查看详情