mybatis-plus代码生成,实体类不生成父类属性
一、参考文档:
官方文档其实说的很清楚了,可能有个别地方有点不太清楚。
mybatis-plus官方: https://mp.baomidou.com/guide/generator.html
模版引擎用的beetl,之前没怎么接触过这块,不过感觉beetl有点像是写jsp一样,上手快。
beetl官方: http://ibeetl.com/guide/#/beetl/
二、具体实现,说明可以看注释,更详细的配置可以看官方文档。
依赖:
<!--代码生成-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
<!--代码生成使用的模版引擎-->
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.9.3</version>
</dependency>
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
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.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine; import java.util.HashMap;
import java.util.Map; public class Main {
//核心类,所有操作配置都围绕该类展开。
private static final AutoGenerator mpg = new AutoGenerator();
//获取项目目录
private static final String projectPath = System.getProperty("user.dir");
//项目基础包
private static final String Package = "com.mz.mzservice";
//子模块包名,最终生成的是类似 com.mz.mzservice.dev 这样的
private static final String ModuleName = "dev"; public static void main(String[] args) {
//数据库连接配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8");
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("root");
mpg.setDataSource(dataSourceConfig);
//公用的一些配置,一看就懂的就不加注释了。
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(projectPath + "/src/main/java");
globalConfig.setAuthor("sun");
//日期类型的字段使用哪个类型,默认是 java8的 日期类型,此处改为 java.util.date
globalConfig.setDateType(DateType.ONLY_DATE);
//是否覆盖 已存在文件,默认 false 不覆盖
globalConfig.setFileOverride(false);
//mapper.xml 是否生成 ResultMap,默认 false 不生成
globalConfig.setBaseResultMap(true);
//mapper.xml 是否生成 ColumnList,默认 false 不生成
globalConfig.setBaseColumnList(true);
//生成的实体类名字,增加前后缀的 ,下面的mapper xml 同理,另外还有controller和service的名称配置
// globalConfig.setEntityName("%sEntity");
globalConfig.setMapperName("%sMapper");
globalConfig.setXmlName("%sMapper");
//是否生成完成后打开资源管理器
globalConfig.setOpen(false);
mpg.setGlobalConfig(globalConfig); PackageConfig pc = new PackageConfig();
pc.setParent(Package);
pc.setModuleName(ModuleName);
mpg.setPackageInfo(pc); StrategyConfig strategy = new StrategyConfig();
//支持正则表达式,字符串数组。 和 Exclude 二选一
// TODO 此处使用正则生成时,提示有些bug。
// 被正则过滤的表会提示 “^dev_.*$” 表不存在,其实需要生成代码的表已经正常生成完毕了。
strategy.setInclude("^dev_.*$");
// 不需要生成的表
// strategy.setExclude("sequence");
//此处配置为 下划线转驼峰命名
strategy.setNaming(NamingStrategy.underline_to_camel);
//生成的字段 是否添加注解,默认false
strategy.setEntityTableFieldAnnotationEnable(true);
//表前缀,配置后 生成的的代码都会把前缀去掉
// strategy.setTablePrefix("dev_");
//实体类的基础父类。没有可以不配置。
strategy.setSuperEntityClass("com.mz.mzservice.entity.BaseEntity");
//这里本来以为配置上 生成的实体类就没有父类的属性了,但其实不是。
//如何去掉父类属性,下面有说明。
strategy.setSuperEntityColumns("creater","createTime","editor","editTime","remark");
//controller,基础父类
strategy.setSuperControllerClass("com.mz.mzservice.controller.BaseController");
//是否启用 Lombok
strategy.setEntityLombokModel(true);
//是否启用 builder 模式 例:new DevDevice().setDealerId("").setDeviceCode("");
strategy.setEntityBuilderModel(true);
mpg.setStrategy(strategy); InjectionConfig in = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<String, Object>();
//自定义配置,在模版中cfg.superColums 获取
// TODO 这里解决子类会生成父类属性的问题,在模版里会用到该配置
map.put("superColums", this.getConfig().getStrategyConfig().getSuperEntityColumns());
this.setMap(map);
}
};
in.setFileOutConfigList(CollectionUtil.newArrayList(new FileOutConfig("/templates/mapper.xml.btl") {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义mapper xml输出目录
return projectPath + "/src/main/resources/mapper/" + ModuleName
+ "/" + tableInfo.getMapperName() + StringPool.DOT_XML;
}
}));
mpg.setCfg(in);
TemplateConfig templateConfig = new TemplateConfig();
//自定义模版
templateConfig.setController("/templates/btl/controller.java");
templateConfig.setEntity("/templates/btl/entity.java");
//关闭默认的mapper xml生成
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
//使用beetl模版引擎
mpg.setTemplateEngine(new BeetlTemplateEngine());
mpg.execute();
} }
三、自定义模版,这里我只粘贴出来,子类不生成父类属性的地方。修改是基于官方模版的。
说明:大概逻辑是判断字段名是否包含在之前在代码里配置的父类字段。如果包含则直接continue。
entity.java.btl
四、官方的默认模版位置
mybatis-plus代码生成,实体类不生成父类属性的更多相关文章
- fastjson JSON.toJavaObject() 实体类首字母大写属性无法解析问题
fastjson JSON.toJavaObject() 实体类首字母大写属性无法解析问题
- JSon实体类快速生成插件 GsonFormat 1.2.0
写在前头:本插件只适用 android studio和 Intellij IDEA 工具,eclipse 的少年无视我吧!!! 这是一个根据JSONObject格式的字符串,自动生成实体类参数. gi ...
- MyBatis探究-----为实体类Bean取别名,配置typeAliases
1.单个实体类设置别名 1.1 不使用alias <typeAliases> <!-- typeAlias:为某个java类型起别名 ; type:指定要起别名的类型全类名; 默认别 ...
- mybatis 一对一 映射实体类、嵌套查询
一对一 在SysUser 类中增加SysRole字段.1.sql语句将role.role_name映射到role.roleName上. 2.还可以在XML 映射文件中配置结果映射.<result ...
- ADO.NET 根据实体类自动生成添加修改语句仅限Oracle使用
话不多说直接上代码,新手上路,高手路过勿喷,请多多指教. /// <summary> /// 等于号 /// </summary> ) + Convert.ToChar() + ...
- 使用springboot mybatis 查询时实体类中的驼峰字段值为null
看到返回结果以后主要分析了一下情况: 实体类的get.set方法确实 mapper.xml文件中的resultMap.resultType等原因导致 数据库中数据存在问题 经过检查与验证发现以上都不存 ...
- DAO层使用mybatis框架有关实体类的有趣细节
1.根据个人习惯,将储存那些数据库查询结果集有映射关系的实体类的Package包名有如下格式: cn.bjut.domain cn.bjut.pojo cn.bjut.model cn.bjut.en ...
- 阶段3 1.Mybatis_12.Mybatis注解开发_5 mybatis注解建立实体类属性和数据库表中列的对应关系
创建新项目,一对多 复制刚才关闭的项目的文件 复制到们的新项目里面 复制包的依赖 删减相关代码.只保留这三个查询的方法 模糊查询改成传统的占位符的方式 之前是可以自定义实体类的属性字段,和数据库的字典 ...
- 按照list中实体类的某一属性排序
传进一个装有实体类的list public void sort(List<MedicalPracticesDetail> mpdList){ Collections.sort(mpdLis ...
随机推荐
- Linux下WebLogic的启动、停止和后台运行的方法
Linux下WebLogic的启动.停止和后台运行的方法 进入目录:/home/weblogic/user_projects/domains/base_domain/bin 查看目录下的命令,如图: ...
- tomcat启动报错java.lang.OutOfMemoryError:PermGen space解决办法
tomcat启动错误提示: 严重: Error waiting for multi-thread deployment of WAR files to completejava.util.concur ...
- Oracle定时任务执行存储过程备份日志记录表
写在前面 需求 1.备份系统日志表T_S_LOG, 按照操作时间字段OPERATETIME, 将每天的日志增量备份到另一张表. 思路 1.创建一张数据结构完全相同的表T_S_LOG_BAK作为备份表 ...
- C# 获取文件扩展信息-应用名称/作者等
方案一:使用微乳封装的Shell包 添加nuget包:Microsoft.WindowsAPICodePack.Shell using Microsoft.WindowsAPICodePack.She ...
- Python 初级 6 循环 (三)
一.复习 1 计算循环(for循环) for looper in [1, 2, 3, 4, 5]: print("hello") 1) looper的值从第0个数1开始 2) 对应 ...
- [IR] Inverted Index & Boolean retrieval
教材:<信息检索导论> 倒排索引 How to build Inverted Index? 1. Token sequence. 2. Sort by terms. 3. Dictiona ...
- 开发日记:常用BAT批处理
备份文件:BackupSourceCode.bat ::自动备份当前文件夹 ::by luomg, 21:15 2010-10-13 ::minguiluo@163.com @echo off tit ...
- xshell修改配色方案为白色
- html中使用mathjax数学公式
测试用例: test.html: <!DOCTYPE html> <html> <head> <link rel="stylesheet" ...
- LeetCode,3. 无重复字符的最长子串
看了各位大神的,真是难堪,尤其是各种c++动不动就击败99%...我用python,换了三次算法,改了十几次bug,才击败5%....贴出来纪念下吧. 题目如下: 给定一个字符串,请你找出其中不含有重 ...