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 ...
随机推荐
- Python爬虫之抓取豆瓣影评数据
脚本功能: 1.访问豆瓣最受欢迎影评页面(http://movie.douban.com/review/best/?start=0),抓取所有影评数据中的标题.作者.影片以及影评信息 2.将抓取的信息 ...
- hdu 1007 Quoit Design (经典分治 求最近点对)
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
- 【HDU6609】Find the answer【线段树】
题目大意:给你一个序列,对于每个i,你可以选择1~i-1中任意多的数并将它删去,剩余的数(包括i)∑≤m,问对于每个i最少删几个数可以达到要求 题解: 考虑朴素的思想,对于每个i,我只需要删去最大的若 ...
- linux0.11源码内核——系统调用,int80的实现细节
linux0.11添加系统调用的步骤 假设添加一个系统调用foo() 1.修改include/linux/sys.h 添加声明 extern int foo(); 同时在sys_call_table数 ...
- AcWing 312. 乌龟棋 (简单DP)打卡
题目:https://www.acwing.com/problem/content/description/314/ 题意:有一段路,每个格子都有个价值,然后有m张卡牌,四种类型,走1,2,3,4步, ...
- CoffeeScript编写简单新闻页(仅UI)
CoffeeScript编写简单新闻页(仅UI) 1. 配置(在公司搭建好的环境下配置) omnisocials-backend/src/backend/modules/member/config/m ...
- python删除列表中得重复得数据
解决思想:将列表转换为 集合,利用集合删除重复数据得特性删除重复数据,然后将集合转换为列表 #删除列表中得重复元素 def delect_1 (lt): s = set(lt) lt = list(s ...
- django-1-新手如何使用django
工具: python,django 流程: 1.安装python 安装python,配置python环境变量, 2.安装django django安装有两种方式: 1.通过pip安装(python2叫 ...
- smb.conf免密登录文件
# This is the main Samba configuration file. You should read the# smb.conf(5) manual page in order t ...
- 配置访问公网主机上的jupyter notebook
文章结构: 一.安装python 二.安装并配置jupyter并配置jupyter 三.第一个python程序 一.安装python 1.1下载python安装包 # wget https://www ...