Mybatis-Plus依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1.9</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.0</version>
</dependency>
package com.regs.tms.common.mybatis;
数据库配置
@Configuration
public class DataSourceConfig {
@Bean(name = "dataSource")
@Qualifier("dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
}
Mybatis配置
package com.regs.tms.common.mybatis;
/**
* mybatis配置类
*
* @author Souler
* @date 2016/8/12
*/
@Configuration
@EnableTransactionManagement
@AutoConfigureAfter(DataSourceConfig.class)
@MapperScan("com.regs.tms.**.mapper")
public class MybatisPlusConfig implements TransactionManagementConfigurer {
@Autowired
@Qualifier("dataSource")
DataSource dataSource;
/**
* mybatis-plus 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor interceptor = new PaginationInterceptor();
interceptor.setDialectType(DBType.MYSQL.getDb());
return interceptor;
}
/**
* mybatis-plus 详细配置
*
* @return
*/
@Bean
public SqlSessionFactory sqlSessionFactory() {
MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
mybatisPlus.setDataSource(dataSource);
mybatisPlus.setTypeAliasesPackage("com.regs.tms.**.entity");
mybatisPlus.setPlugins(new Interceptor[]{paginationInterceptor()});
mybatisPlus.setVfs(SpringBootVFS.class);
// MP 全局配置,更多内容进入类看注释
GlobalConfiguration globalConfig = new GlobalConfiguration();
globalConfig.setDbType(DBType.MYSQL.getDb());
globalConfig.setDbColumnUnderline(true);
globalConfig.setCapitalMode(true);
// ID 策略 AUTO->`0`("数据库ID自增")
// INPUT->`1`(用户输入ID") ID_WORKER->`2`("全局唯一ID") UUID->`3`("全局唯一ID")
globalConfig.setIdType(3);
globalConfig.setRefresh(false);//设置xml热加载(禁用)
// 逻辑删除
globalConfig.setSqlInjector(new com.baomidou.mybatisplus.mapper.LogicSqlInjector());
globalConfig.setLogicDeleteValue(DelFlagEnum.STATUS_DEL.getType());
globalConfig.setLogicNotDeleteValue(DelFlagEnum.STATUS_NORMAL.getType());
// 配置公共字段自动填写
globalConfig.setMetaObjectHandler(new MyMetaObjectHandler());
mybatisPlus.setGlobalConfig(globalConfig);
// MP mybatis属性配置,更多内容进入类看注释
MybatisConfiguration config = new MybatisConfiguration();
// 增加MP的xml
// 打包成jar时,setTypeAliasesPackage("xxx")找不到类的问题。MyBatis通过VFS来扫描
config.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
config.setVfsImpl(SpringBootVFS.class);
// 配置驼峰功能
config.setMapUnderscoreToCamelCase(true);
config.setCacheEnabled(true);
// 本地调试设置日志
config.setLogImpl(StdOutImpl.class);
mybatisPlus.setConfiguration(config);
try {// 添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath*:mapping/**/*.xml");
mybatisPlus.setMapperLocations(resources);
return mybatisPlus.getObject();
} catch (Exception e) {
throw new RuntimeException("sqlSessionFactory init fail", e);
}
}
/**
* 事务管理,具体使用在service层加入@Transactional注解
*/
@Bean
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
}
package com.regs.tms.common.mybatis;
public enum DelFlagEnum {
/** 有效的 */
STATUS_NORMAL("0", "有效的"),
/** 无效的 */
STATUS_DEL("1", "无效的");
DelFlagEnum(String type, String name) {
this.type = type;
this.name = name;
}
public String type;
public String name;
public String getType() {
return type;
}
public String getName() {
return name;
}
}
自动填充字段配置
package com.regs.tms.common.mybatis;
/** 自定义填充公共 name 字段 */
public class MyMetaObjectHandler extends MetaObjectHandler {
/**
* 测试 user 表 name 字段为空自动填充
*/
public void insertFill(MetaObject metaObject) {
// 更多查看源码测试用例
System.out.println("*************************");
System.out.println("insert fill");
System.out.println("*************************");
// 测试下划线
Object create_time = getFieldValByName("createTime", metaObject);
Object del_flag = getFieldValByName("delFlag", metaObject);
Object update_time = getFieldValByName("updateTime", metaObject);
if (null == update_time) {
setFieldValByName("updateTime", new Date(), metaObject);// mybatis-plus版本2.0.9+
}
if (null == create_time) {
setFieldValByName("createTime", new Date(), metaObject);// mybatis-plus版本2.0.9+
}
if (null == del_flag) {
setFieldValByName("delFlag", DelFlagEnum.STATUS_NORMAL.getType(),
metaObject);// mybatis-plus版本2.0.9+
}
}
@Override
public void updateFill(MetaObject metaObject) {
// 更多查看源码测试用例
System.out.println("*************************");
System.out.println("update fill");
System.out.println("*************************");
Object update_time = getFieldValByName("updateTime", metaObject);
if (null == update_time) {
setFieldValByName("updateTime", new Date(), metaObject);// mybatis-plus版本2.0.9+
}
}
}
## 自动填充实体父类运用注解
package com.regs.tms.service.logic.entity;
@Data
public class BaseMode<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间")
@TableField(value = "create_time", fill = FieldFill.INSERT)
protected Date createTime;
/**
* 修改时间
*/
@ApiModelProperty(value = "创修改时间")
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
protected Date updateTime;
/**
* 0-正常,1-删除
*/
@ApiModelProperty(value = "删除标记:0-正常,1-删除")
@TableLogic
@TableField(value = "del_flag", fill = FieldFill.INSERT)
protected String delFlag;
protected Serializable pkVal() {
return null;
}
}