@toc
1. POM中增加依赖
mybatis-plus-boot-starter包含mybatis-plus-boot-starter,需要直接覆盖。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!--自动生成-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
<!--生成模板-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
2. 生成DO、mapper、server、controller
public class MysqlGenerator {
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(true);
//实体类名称
gc.setEntityName("%sDO");
//server名称
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sMapper");
gc.setFileOverride(true);
gc.setActiveRecord(true);
gc.setEnableCache(false);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
mpg.setGlobalConfig(gc);
// TODO 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/huihua_saas_demo?characterEncoding=utf8");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
//父路径
pc.setParent("com.miya.hh.order");
pc.setEntity("data");
pc.setService("service");
pc.setMapper("mapper");
pc.setServiceImpl("service.impl");
mpg.setPackageInfo(pc); //自增时间
List<TableFill> tableFillList = new ArrayList<>();
TableFill createField = new TableFill("create_time", FieldFill.INSERT);
TableFill modifiedField = new TableFill("update_time", FieldFill.INSERT_UPDATE);
tableFillList.add(createField);
tableFillList.add(modifiedField);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
// 设置逻辑删除键
strategy.setLogicDeleteFieldName("deleted");
// TODO 指定生成的bean的数据库表名
strategy.setInclude("message","message_template");
// 驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
mpg.setStrategy(strategy);
// 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute(); }
}
3. 配置
#集成mybats-plus
mybatis-plus:
# 实体类所在package
type-aliases-package: com.miya.hh.order.data
type-aliases-super-type: java.lang.Object
#逻辑删除
global-config:
db-config:
logic-delete-value: 0
logic-not-delete-value: 1
# 枚举所在package
type-enums-package: com.miya.hh.order.common
# 是否开启自动驼峰命名规则 MyBatis默认值false configuration:
map-underscore-to-camel-case: false
4. 分页配置、数据填充
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
public GlobalConfig globalConfig(){
GlobalConfig globalConfig=new GlobalConfig();
globalConfig.setMetaObjectHandler(new Metahandler());
return globalConfig;
}
}
//自动填充时间
@Component public class Metahandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
String field1 = "createTime";
String field2 = "updateTime";
fillField(metaObject, field1, field2);
}
@Override
public void updateFill(MetaObject metaObject) {
String field = "updateTime";
fillField(metaObject, field);
}
private void fillField(MetaObject metaObject, String... fields) {
for (String field : fields) {
if (metaObject.hasSetter(field) && metaObject.getValue(field) == null) {
setFieldValByName(field, LocalDateTime.now(), metaObject);
}
}
}
}
使用方式:
@TableField(fill = FieldFill.INSERT) (插入时填充)
@TableField(fill = FieldFill.INSERT_UPDATE) (插入编辑时填充)
5.枚举的使用
public enum CancelType {
SYSTEM(0, "系统取消"),
MCH(1, "商家取消"),
USER(2, "用户取消");
@EnumValue //在需要在数据库插入的数据上添加注解
public final Integer code;
public final String desc;
public Integer getCode() {
return code;
}
CancelType(Integer code, String desc) {
this.code = code;
this.desc = desc;
}
6. 多数据源的使用
//mybatis的SqlSessionFactoryBean需要替换为mybatisplus的MybatisSqlSessionFactoryBean
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(allInOneDs);
//枚举所在package factoryBean.setTypeEnumsPackage("com.miya.huihua.coupon.common");
//全局配置
factoryBean.setGlobalConfig(new GlobalConfig().setMetaObjectHandler(new Metahandler()));
//手动设置session工厂时,需要手动添加分页插件
Interceptor[] plugins = new Interceptor[1];
plugins[0] = paginationInterceptor();
factoryBean.setPlugins(plugins);
return factoryBean.getObject();
网友评论