mybatis mysql代码块_mybatis plus mysql 代码生成器 示例demo

packagecom.blackcat.blog.util;importcom.baomidou.mybatisplus.annotation.FieldFill;importcom.baomidou.mybatisplus.core.toolkit.StringPool;importcom.baomidou.mybatisplus.core.toolkit.StringUtils;importcom.baomidou.mybatisplus.generator.AutoGenerator;importcom.baomidou.mybatisplus.generator.InjectionConfig;import com.baomidou.mybatisplus.generator.config.*;importcom.baomidou.mybatisplus.generator.config.po.TableFill;importcom.baomidou.mybatisplus.generator.config.po.TableInfo;importcom.baomidou.mybatisplus.generator.config.rules.NamingStrategy;importcom.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import java.util.*;/*** mybatis plus mysql 代码生成器

*@author: blackcat

* @date: 2020/1/6 17:22

* @Param*/

public classMysqlGenerator {public static voidmain(String[] args) {

Map param = new HashMap<>();

param.put("projectPath","D:\\project\\blackcat-blog\\blog-base");//代码输出项目地址

param.put("author","blackcat");//作者

param.put("url","172.17.0.3:3306/blackcat_blog");//ip/数据库

param.put("driverName","com.mysql.jdbc.Driver");

param.put("username","root");//数据库用户

param.put("password","111111");//数据库密码

param.put("parent","com.blackcat.blog");//顶层包结构

param.put("mapper","mapper");//生成的mapper包名

param.put("entity","entity");//生成的entity包名

param.put("service","service");//生成的service包名

param.put("serviceImpl","service.impl");//生成的serviceImpl包名

param.put("xml","mappers");//生成的mapper.xml包名

param.put("model","");//生成的mapper.xml包名下的模块名称 空则无 如:mappers.shiro

param.put("xmlName","Mapper");//生成的mapper.xml的文件结尾名称如UserMapper.xml//设置模板 freemarker模板:/templates/mapper.xml.ftl velocity模板:/templates/mapper.xml.vm

param.put("templatePath","/templates/mapper.xml.ftl");// param.put("table","");//数据库表名 一张表时使用//多表时使用,当数组大于0时使用数组//示例:String[] tablse={"sys_menu","sys_role","sys_role_menu","sys_user_role","sys_user_role"};//shiro所需表//shiro所需表

String[] tablse={"sys_menu","sys_role","sys_role_menu","sys_user","sys_user_role"};

generator(param,tablse);

}/**

* 代码生成

* @author: blackcat

* @date: 2020/1/7 13:44

* @Param [param]*/

public static void generator(Mapparam,String[] tablse){//代码生成器

AutoGenerator mpg = newAutoGenerator();//全局配置

GlobalConfig gc = newGlobalConfig();

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

gc.setAuthor(param.get("author"));

gc.setOpen(false);//是否打开输出目录

gc.setServiceName("%sService");//service 命名方式

gc.setServiceImplName("%sServiceImpl");//service impl 命名方式

gc.setMapperName("%sMapper");//自定义文件命名,注意 %s 会自动填充表实体属性!

gc.setXmlName("%sMapper");//自定义文件命名,注意 %s 会自动填充表实体属性!

gc.setFileOverride(true);//是否覆盖已有文件

gc.setActiveRecord(true);//开启 ActiveRecord(活动记录) 模式

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

gc.setBaseResultMap(true);//XML 开启 BaseResultMap

gc.setBaseColumnList(false);//XML columList

mpg.setGlobalConfig(gc);//数据源配置

DataSourceConfig dsc = newDataSourceConfig();

dsc.setUrl("jdbc:mysql://"+param.get("url")+"?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");

dsc.setDriverName(param.get("driverName"));

dsc.setUsername(param.get("username"));

dsc.setPassword(param.get("password"));

mpg.setDataSource(dsc);//包配置

PackageConfig pc = newPackageConfig();if(!StringUtils.isEmpty(param.get("model"))){

pc.setModuleName(param.get("model"));

}

pc.setParent(param.get("parent"));

pc.setMapper(param.get("mapper"));

pc.setEntity(param.get("entity"));

pc.setService(param.get("service"));

pc.setServiceImpl(param.get("serviceImpl"));

pc.setXml(param.get("xml"));

mpg.setPackageInfo(pc);//自定义需要填充的字段

List tableFillList = newArrayList();//如 每张表都有一个创建时间、修改时间如下是配置

TableFill createField = new TableFill("gmt_create", FieldFill.INSERT);

TableFill modifiedField= new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);

tableFillList.add(createField);

tableFillList.add(modifiedField);//自定义配置

InjectionConfig cfg = newInjectionConfig() {

@Overridepublic voidinitMap() {//to do nothing

}

};//模板引擎

String templatePath = param.get("templatePath");//自定义输出配置

List focList = new ArrayList<>();//自定义配置会被优先输出 这里设置xml的存放路径

focList.add(newFileOutConfig(templatePath) {

@OverridepublicString outputFile(TableInfo tableInfo) {//自定义输出文件名

StringBuilder customPath = newStringBuilder();

customPath.append(param.get("projectPath"));

customPath.append("/src/main/resources/");

customPath.append(param.get("xml"));

customPath.append("/");if(!StringUtils.isEmpty(pc.getModuleName())){

customPath.append(pc.getModuleName());

}

customPath.append("/");

customPath.append(tableInfo.getEntityName());

customPath.append(param.get("xmlName"));

customPath.append(StringPool.DOT_XML);/*return param.get("projectPath") + "/src/main/resources/"+param.get("xml")+"/"

+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;*/

returncustomPath.toString();

}

});

cfg.setFileOutConfigList(focList);

mpg.setCfg(cfg);//策略配置

StrategyConfig strategy = newStrategyConfig();//数据库表映射到实体的命名策略,默认:不做任何改变,原样输出

strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略,未指定按照 naming 执行

strategy.setColumnNaming(NamingStrategy.underline_to_camel);

strategy.setEntityLombokModel(true);//是否使用lombok

if (tablse.length > 0) {

strategy.setInclude(tablse);//数据库表 多表

} else{

strategy.setInclude(param.get("table"));//数据库表 一张表

}

strategy.setControllerMappingHyphenStyle(true);//驼峰转连字符//strategy.setSuperControllerClass("com.sxt.BaseController");//公共父类//strategy.setSuperEntityColumns("person_id","person_name");//写于父类中的公共字段//strategy.setInclude(("表名,多个英文逗号分割").split(","));//要设置生成哪些表 如果不设置就是生成所有的表

mpg.setStrategy(strategy);//数据库表配置//选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有! 默认 Veloctiy

mpg.setTemplateEngine(newFreemarkerTemplateEngine());

mpg.execute();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值