准备工作:
1 需下载Gradle的bin压缩包,地址:http://services.gradle.org/distributions/ 【切勿下载高版本,推荐4.8.1】
2 从GIT上下载放置生成代码的Gradle->Mybatis-plus项目 git地址https://github.com/baomidou/mybatis-plus.git 【需要切换到2.x分支】
3 相关入门教程 https://www.cnblogs.com/sunny3096/p/9013704.html
https://www.cnblogs.com/liangzs/p/8855834.html
【 参考入门教程】
Idea打开Gradle项目的build.gradle文件即可,关键配置【gradle home地址要映射到下载bin压缩包的解压路径】 如图:
导入且部署完项目后编译代码 如图:
注意右边的gradle栏【如果下载4.8.1版本的gradle的bin包基本就没问题】,类似maven下载依赖jar包 如图:
那项目的基本配置就完成,下面要做的就是配置连接数据库的参数了,这有个Oracle的java代码:
/**
* Copyright (c) 2011-2016, hubin (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.test.generator;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.OracleTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* <p>
* 代码生成器演示
* </p>
*
* @author hubin
* @date 2016-12-01
*/
public class OrcaleGenerator extends GeneratorTest {
/**
* <p>
* Oracle 生成演示
* </p>
*/
public static void main(String[] args) {
int result = scanner();
// 代码生成器
AutoGenerator mpg = new AutoGenerator().setGlobalConfig(
// 全局配置
new GlobalConfig()
.setOutputDir("E:/gitideal/mybatis-plus/mybatis-plus-generate/develop/code/")//输出目录
.setFileOverride(true)// 是否覆盖文件
.setActiveRecord(true)// 开启 activeRecord 模式
.setEnableCache(false)// XML 二级缓存
.setBaseResultMap(true)// XML ResultMap
.setBaseColumnList(true)// XML columList
.setAuthor("lics")
).setDataSource(
// 数据源配置
new DataSourceConfig()
.setDbType(DbType.ORACLE)// 数据库类型
.setTypeConvert(new OracleTypeConvert() {
// 自定义数据库表字段类型转换【可选】
@Override
public DbColumnType processTypeConvert(String fieldType) {
System.out.println("转换类型:" + fieldType);
return super.processTypeConvert(fieldType);
}
})
.setDriverName("oracle.jdbc.driver.OracleDriver")
.setUsername("omsprd")
.setPassword("oms123prd")
.setUrl("jdbc:oracle:thin:@10.82.27.101:1521:omsbit")
).setStrategy(
// 策略配置
new StrategyConfig()
.setTablePrefix(new String[]{"SHOP", ""})// 此处可以修改为您的表前缀
// .setTableSuffix(new String[]{"T",""}) //此处可以修改为您的表后缀
.setNaming(NamingStrategy.underline_to_camel)// 表名生成策略
.setInclude(new String[] {"SHOP_MALL_SYN_TIME_T","SHOP_SYN_HIS_T","SHOP_TRADES_INFO_T","SHOP_TRADES_ORDER_T",
"SHOP_TRADES_PROMOTION_T","SHOP_TRADES_INFO_CHILD_T","WTC_PROMOTION_FSEND_T"
}) // 需要生成的表
).setPackageInfo(
// 包配置
new PackageConfig()
.setEntity("model")
.setParent("com.suneee.ep.order")// 自定义包路径
.setController("controller")// 这里是控制器包名,默认 web
.setXml("xml")
).setCfg(
// 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
}.setFileOutConfigList(Collections.<FileOutConfig>singletonList(new FileOutConfig(
"/templates/mapper.xml" + ((1 == result) ? ".ftl" : ".vm")) {
// 自定义输出文件目录
@Override
public String outputFile(TableInfo tableInfo) {
return "/develop/code/xml/" + tableInfo.getEntityName() + ".xml";
}
}))
).setTemplate(
// 关闭默认 xml 生成,调整生成 至 根目录
// new TemplateConfig().setXml(null)
//开启 xml 生成
new TemplateConfig()
);
// 执行生成
if (1 == result) {
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
}
mpg.execute();
// 打印注入设置,这里演示模板里面怎么获取注入内容【可无】
System.err.println(mpg.getCfg().getMap().get("abc"));
}
}
Mysql的java代码:
/**
* Copyright (c) 2011-2016, hubin (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.test.generator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.enums.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
/**
* <p>
* 代码生成器演示
* </p>
*
* @author hubin
* @date 2016-12-01
*/
public class MysqlGenerator extends GeneratorTest {
/**
* <p>
* MySQL 生成演示
* </p>
*/
public static void main(String[] args) {
int result = scanner();
// 自定义需要填充的字段
List<TableFill> tableFillList = new ArrayList<>();
tableFillList.add(new TableFill("ASDD_SS", FieldFill.INSERT_UPDATE));
// 代码生成器
AutoGenerator mpg = new AutoGenerator().setGlobalConfig(
// 全局配置
new GlobalConfig()
.setOutputDir("E:/gitideal/mybatis-plus-dev/mybatis-plus/mybatis-plus-generate/develop/code/")//输出目录
.setFileOverride(true)// 是否覆盖文件
.setActiveRecord(true)// 开启 activeRecord 模式
.setEnableCache(false)// XML 二级缓存
.setBaseResultMap(true)// XML ResultMap
.setBaseColumnList(true)// XML columList
//.setKotlin(true) 是否生成 kotlin 代码
.setAuthor("Yanghu")
// 自定义文件命名,注意 %s 会自动填充表实体属性!
// .setMapperName("%sDao")
// .setXmlName("%sDao")
// .setServiceName("MP%sService")
// .setServiceImplName("%sServiceDiy")
// .setControllerName("%sAction")
).setDataSource(
// 数据源配置
new DataSourceConfig()
.setDbType(DbType.MYSQL)// 数据库类型
.setTypeConvert(new MySqlTypeConvert() {
// 自定义数据库表字段类型转换【可选】
@Override
public DbColumnType processTypeConvert(String fieldType) {
System.out.println("转换类型:" + fieldType);
// if ( fieldType.toLowerCase().contains( "tinyint" ) ) {
// return DbColumnType.BOOLEAN;
// }
return super.processTypeConvert(fieldType);
}
})
.setDriverName("com.mysql.jdbc.Driver")
.setUsername("root")
.setPassword("suneee@mysqltest157")
.setUrl("jdbc:mysql://10.0.252.28:3306/wxshop?characterEncoding=utf8")
).setStrategy(
// 策略配置
new StrategyConfig()
.setTablePrefix(new String[]{"suneee", ""})// 此处可以修改为您的表前缀
// .setTableSuffix(new String[]{"suneee",""}) //此处可以修改为您的表后缀
.setNaming(NamingStrategy.underline_to_camel)// 表名生成策略
.setInclude(new String[] {"suneee_p_xianshi_goods","suneee_p_bundling_goods", "suneee_groupbuy","suneee_goods_gift","suneee_p_bundling","suneee_p_xianshi"
}) // 需要生成的表
).setPackageInfo(
// 包配置
new PackageConfig()
.setMapper("dao") // 这里是DAO包名,默认 mapper
.setEntity("model") // 这里是Entity包名,默认 entity
.setParent("com.suneee.ep.trade")// 自定义包路径
.setController("controller") // 这里是控制器包名,默认 web
.setXml("mapper") // 这里是XML映射文件, 默认 xml
).setCfg(
// 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
}.setFileOutConfigList(Collections.<FileOutConfig>singletonList(new FileOutConfig(
"/templates/mapper.xml" + ((1 == result) ? ".ftl" : ".vm")) {
// 自定义输出文件目录
@Override
public String outputFile(TableInfo tableInfo) {
return "E:/gitideal/mybatis-plus-dev/mybatis-plus/mybatis-plus-generate/develop/code/com/suneee/ep/trade/xml/" + tableInfo.getEntityName() + "Mapper.xml";
}
}))
).setTemplate(
// 关闭默认 xml 生成,调整生成 至 根目录
new TemplateConfig().setXml(null)
// 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy
// 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置:
// .setController("...");
// .setEntity("...");
// .setMapper("...");
// .setXml("...");
// .setService("...");
// .setServiceImpl("...");
);
// 执行生成
if (1 == result) {
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
}
mpg.execute();
// 打印注入设置,这里演示模板里面怎么获取注入内容【可无】
System.err.println(mpg.getCfg().getMap().get("abc"));
}
}
收工!!!