mybatis plus generator工具集成(一)
配置分两步
1.添加依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.6</version>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency> <dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
2.新增CodeGenerator类
package com.sswchat; import com.baomidou.mybatisplus.annotation.IdType;
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.DateType;
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; public class CodeGenerator { public 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.isNotEmpty(ipt)){
return ipt;
}
}
throw new MybatisPlusException("请输入正确的"+tip+"!");
} 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.setAuthor("***");//开发人员
gc.setOpen(false);//文件生成后,是否需要打开所在路径
gc.setFileOverride(true);//是否覆盖已有文件
gc.setEnableCache(false);//是否在xml中添加二级缓存设置
gc.setActiveRecord(false);//开启ActiveRecord模式
gc.setBaseResultMap(true);//开启BaseResultMap
gc.setBaseColumnList(true);//开启BaseColumnList
gc.setDateType(DateType.TIME_PACK);//时间类型对应策略
gc.setControllerName("%sController");
gc.setEntityName("%s");//实体命名方式
gc.setServiceName("%sService");
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setIdType(IdType.AUTO); mpg.setGlobalConfig(gc); //数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/sina?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc); //包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.sswchat");//父包名,如果为空,将下面子包名必须写全部,否则就只需写子包名
pc.setModuleName(scanner("模块名"));//父包模块名称
mpg.setPackageInfo(pc); //自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() { }
}; // 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl"; //自定义输出配置
List<FileOutConfig> fileOutConfigs = new ArrayList<>();
//自定义配置会被优先输出
fileOutConfigs.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
//自定义输出文件名
return projectPath+"/src/main/com/sswwhat/mapper/mapping/"+tableInfo.getEntityName()+"Mapper"+ StringPool.DOT_XML;
}
}); cfg.setFileOutConfigList(fileOutConfigs);//配置FileOutConfig指定模板文件、输出文件达到自定义文件生成目的
mpg.setCfg(cfg);
mpg.setTemplate(new TemplateConfig().setXml(null)); StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setCapitalMode(true)
//这里结合Lombok使用,有true,无false
.setEntityLombokModel(true)
.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
strategyConfig.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
strategyConfig.setRestControllerStyle(true);
strategyConfig.setSuperControllerClass("com.baomidou.ant.common.BaseController");
//strategyConfig.setInclude();
strategyConfig.setSuperEntityColumns("id");
strategyConfig.setControllerMappingHyphenStyle(true);
strategyConfig.setTablePrefix(pc.getModuleName()+"_");
mpg.setStrategy(strategyConfig);
mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); }
}
mybatis plus generator工具集成(一)的更多相关文章
- mybatis generator工具集成(一)
第一步,pom中加入 <build> <plugins> <plugin> <groupId>org.springframework.boot</ ...
- springboot集成mybatis及mybatis generator工具使用
原文链接 前言mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernat ...
- springboot入门(三)-- springboot集成mybatis及mybatis generator工具使用
前言 mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernate这种完 ...
- MyBatis之七:使用generator工具
可以将mybatis理解成一种半自动化orm框架,通过注解或者配置xml映射文件来手写相关sql语句,不能像我之前介绍orm的文章那样全对象化操作数据库增删改查.其实你会发现,手写配置xml映射文件是 ...
- mybatis代码生成(generator工具生成代码)
generator工具生成代码 下载地址 http://pan.baidu.com/s/1bY8C0I
- mybatis generator工具的使用
mybatis反转数据库的配置文件: generatorConfig.xml: <?xml version="1.0" encoding="UTF-8"? ...
- 一步步学Mybatis-告别繁琐的配置之Mybatis配置文件生成工具 (7)
今年是2013年的杀青之日,前几天由于比较忙,没有及时更新本篇的最后一篇东西,前六篇中我们主要都是采用手动配置相关的Mybatis映射文件与相应的接口类与实体类.当然如果在真正的使用过程中,由于业务的 ...
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的坏习惯. 通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车 ...
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(转)
通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程! 本篇是在SSM框架基础上进行的. 参考文章: 1.Quartz学习——Qua ...
随机推荐
- npm 是干什么的?(非教程)
看了之后就很清楚什么叫NPM,以后它是干嘛的.谢谢楼主 网上的 npm 教程主要都在讲怎么安装.配置和使用 npm,却不告诉新人「为什么要使用 npm」.今天我就来讲讲这个话题. 本文目标读者是「不太 ...
- IntelliJ IDEA更新maven依赖包
问题: IntelliJ IDEA自动载入Maven依赖的功能很好用,但有时候会碰到问题,导致pom文件修改却没有触发自动重新载入的动作,此时需要手动强制更新依赖. 方法: 方法一: ①.右键单击项目 ...
- 左手Mongodb右手Redis redis操作
set key value 设置key的值 get key 取得key的值 decr key 值会减一 incr key 值会加一 decrby key value ,会让key的值减少value. ...
- 原生js深拷贝函数
function deepClone(data){ if(!data || !(data instanceof Object) || (typeof data=="function" ...
- 测开之路三十:Flask基础之jinja2模板继承
实现某些位置的内容固定,某些位置的内容动态展示,如: 中文文档地址:http://docs.jinkan.org/docs/jinja2/templates.html#template-inherit ...
- upc组队赛14 Floating-Point Hazard【求导】
Floating-Point Hazard 题目描述 Given the value of low, high you will have to find the value of the follo ...
- python 装饰器 第二步:扩展函数的功能(不修改原函数)
# 第二步:扩展函数的功能(不能修改原函数) # 用于扩展基本函数的函数 # 把一个函数(eat函数)作为一个整体传给另外一个函数(kuozhan函数) # 这个函数(kuozhan函数)用形参fun ...
- python 自动把mysql备份文件发送邮箱
import os import time import sched import smtplib from email.mime.text import MIMEText from email.he ...
- UVA1629_Cake slicing
Cake slicing 给你一个矩形大小,和每个樱桃的坐标,现在让你去切使得切之后的小矩形包含一个樱桃,每次切的代价是切痕的长度,问你最小代价是多少 思路: 首先要明白一点,不能切除一个不含樱桃的矩 ...
- elasticsearch 英文数字组合字符串模糊检索
不分词,然后用wildcard查询 { "query": { "wildcard": { "字段名": "*123*" ...