pom.xml配置:
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1.8</version>
</dependency>
生成代码main方法:
package com.zc.www.utils;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.entity.TableInfo;
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.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
public class MybatisPlusUtils {
public static void main(String[] args) {
String[] models = { "java"};
for (String model : models) {
shell(model);
}
}
private static void shell(String model) {
File file = new File(model);
String path = file.getAbsolutePath();
System.out.println(path);
path = path.substring(0, path.lastIndexOf(File.separator));
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(path + "/src/main/java");
gc.setFileOverride(true);
gc.setActiveRecord(true);
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
gc.setAuthor("SPF");
// 自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setControllerName("%sController");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.POSTGRE_SQL);
dsc.setTypeConvert(new MySqlTypeConvert() {
// 自定义数据库表字段类型转换【可选】
@Override
public DbColumnType processTypeConvert(String fieldType) {
System.out.println("转换类型:" + fieldType);
// 注意!!processTypeConvert 存在默认类型转换,如果不是你要的效果请自定义返回、非如下直接返回。
return super.processTypeConvert(fieldType);
}
});
dsc.setDriverName("org.postgresql.Driver");
dsc.setUsername("zhencai");
dsc.setPassword("123123");
dsc.setUrl("jdbc:postgresql://60.10.135.153:30000/test?charSet=utf-8");
mpg.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
// strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
strategy.setTablePrefix(new String[] { });// 此处可以修改为您的表前缀
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
strategy.setInclude(new String[] {"zc_ambient_info","zc_boiler_info","zc_bulk_material_carrier_info","zc_furnacepit_info"
,"zc_moving_machinery_info","zc_solvent_info","zc_storage_tank_id","zc_work_info","zc_yard_info"}); // 需要生成的表,{}中不写表名则全部生成-------------------------
// strategy.setExclude(new String[]{"test"}); // 排除生成的表
// 自定义实体父类
// strategy.setSuperEntityClass("com.spf.model.Entity");
// 自定义实体,公共字段
// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
// 自定义 mapper 父类
// strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
// 自定义 service 父类
// strategy.setSuperServiceClass("com.baomidou.demo.TestService");
// 自定义 service 实现类父类
// strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
// 自定义 controller 父类
// strategy.setSuperControllerClass("com.baomidou.demo.TestController");
// 【实体】是否生成字段常量(默认 false)
// public static final String ID = "test_id";
// strategy.setEntityColumnConstant(true);
// 【实体】是否为构建者模型(默认 false)
// public User setName(String name) {this.name = name; return this;}
// strategy.setEntityBuliderModel(true);
mpg.setStrategy(strategy);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.zc.www");
//pc.setController("web");
pc.setEntity("model");
pc.setMapper("mapper");
//pc.setService("service");
//pc.setServiceImpl("service.impl");
pc.setXml("mapper");
// pc.setModuleName("test");
mpg.setPackageInfo(pc);
// 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
};
// 自定义 xxList.jsp 生成
List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
// focList.add(new FileOutConfig("/template/list.jsp.vm") {
// @Override
// public String outputFile(TableInfo tableInfo) {
// // 自定义输入文件名称
// return "D://my_" + tableInfo.getEntityName() + ".jsp";
// }
// });
// cfg.setFileOutConfigList(focList);
// mpg.setCfg(cfg);
// 调整 xml 生成目录演示
/*focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return "/develop/code/xml/" + tableInfo.getEntityName() + ".xml";
}
});*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 关闭默认 xml 生成,调整生成 至 根目录
/*TemplateConfig tc = new TemplateConfig();
if ("ssm-mapper".equals(model)) {
tc.setController(null);
tc.setEntity(null);
tc.setService(null);
tc.setServiceImpl(null);
} else if ("ssm-model".equals(model)) {
tc.setController(null);
tc.setService(null);
tc.setServiceImpl(null);
tc.setMapper(null);
tc.setXml(null);
} else if ("ssm-service".equals(model)) {
tc.setController(null);
tc.setMapper(null);
tc.setXml(null);
tc.setEntity(null);
} else if ("ssm-web".equals(model)) {
tc.setMapper(null);
tc.setXml(null);
tc.setService(null);
tc.setServiceImpl(null);
tc.setEntity(null);
}
mpg.setTemplate(tc);*/
// 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/template 下面内容修改,
// 放置自己项目的 src/main/resources/template 目录下, 默认名称一下可以不配置,也可以自定义模板名称
// TemplateConfig tc = new TemplateConfig();
// tc.setController("...");
// tc.setEntity("...");
// tc.setMapper("...");
// tc.setXml("...");
// tc.setService("...");
// tc.setServiceImpl("...");
// 如上任何一个模块如果设置 空 OR Null 将不生成该模块。
// mpg.setTemplate(tc);
// 执行生成
mpg.execute();
// 打印注入设置【可无】
System.err.println(mpg.getCfg().getMap().get("abc"));
}
}
spring配置文件:
<!-- MyBatis sqlSessionFactory 配置 mybatis -->
<bean id="sqlSessionFactory"
class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<property name="configLocation"
value="classpath:mybatis-configuration.xml" />
<property name="dataSource" ref="dataSource" />
<property name="plugins">
<array>
<!-- 分页插件配置 -->
<bean id="paginationInterceptor"
class="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
<property name="dialectType" value="postgresql" />
</bean>
</array>
</property>
<property name="globalConfig" ref="globalConfig"></property>
</bean>
<bean id="globalConfig"
class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!-- AUTO->`0`("数据库ID自增") INPUT->`1`(用户输入ID") ID_WORKER->`2`("全局唯一ID")
UUID->`3`("全局唯一ID") -->
<property name="idType" value="1" />
<!-- Oracle需要添加该项 -->
<property name="dbType" value="postgresql" />
<!-- 全局表为下划线命名设置 true -->
<!-- <property name="dbColumnUnderline" value="true" /> -->
<!-- 公共字段填充处理器 -->
<!-- <property name="metaObjectHandler" ref="myMetaObjectHandler" /> -->
</bean>
<!-- 自动扫描注入类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zc.www.mapper" />
<property name="sqlSessionFactoryBeanName"
value="sqlSessionFactory" />
</bean>
注意:
!!如果将<property name="idType" value="1" />
这里设置为0,自动自增,那么所有的insert添加方法,都会自动将id看为自增,就算你指定参数包含id也没有用,可能会因为你的id参数类型不同而报错.
~所以解决办法就是这里设置为1,用户设置id,然后在实体类中自己指定id类型,如@TableId(type=IdType.AUTO,value="boiler_id")