1、引入pom
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.6</version>
</defendency>
2、配置达梦数据库连接
spring.datasource.driver-class-name = dm.jdbc.driver.DmDriver
spring.datasource.url = jdbc:dm//10.*.*.*:5236?STU&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&localTimezone=480
spring.datasource.username = username
spring.datasource.password = password
3、数据源配置
@Configuration
@MapperScan(basePackages = {
"com.puppa.mapper"},
sqlSessionTemplateRef = "mySqlSessionTemplate"
)
public class DataSourceConfig {
public static final String MAPPER_LOCATION_PATTERN = "classpath:mapper/*.xml";
@Bean
public DataSourceTransactionManager myTransactionManager (@Autowired DataSource myDataSource) {
return new DataSourceTransactionManager (myDataSource);
}
public SqlSessionFactory mySqlSessionFactory (@Autowired DataSource myDataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(myDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION_PATTERN));
// 全局配置
bean.setGlobalConfig(globalConfig);
// 配置打印sql语句
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setLogImpl(StdOutImpl.class);
return bean.getObject();
}
@Bean
public SqlSessionTemplate mySqlSessionTemplate(@Qualifier("mySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
public GlobalConfig globalConfig() {
GlobalConfig conf = new GlobalConfig();
GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
dbConfig.setKeyGenerator(new OracleKeyGenerator());
dbConfig.setIdType(IdType.values()[1]);
conf.setDbConfig(dbConfig);
// 全局自动填充配置
conf.setMetaObjectHandler(new CustomMetaObjectHandler())
return conf;
}
}
4、字段全局自动填充配置
/**
* 字段自动填充处理类
*/
public class CustomMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
String currentUserId = getCurrentUserId();
setValue("addTime", new Date(), metaObject);
setValue("updateTime", new Date(), metaObject);
if(StringUtil.isNotEmpty(currentUserId)) {
setValue("addBy", currentUserId, metaObject);
setValue("updateBy", currentUserId, metaObject);
} else {
setValue("addBy", 0L, metaObject);
setValue("updateBy", 0L, metaObject);
}
}
@Override
public void updateFill(MetaObject metaObject) {
String currentUserId = getCurrentUserId();
if(StringUtil.isNotEmpty(currentUserId)){
setValue("updateBy", currentUserId, metaObject);
} else {
setValue("updateBy", 0L, metaObject);
}
setValue("updateTime", new Date(), metaObject);
}
private void setValue(String fieldName, Object value, MetaObject metaObject) {
Object field = getFieldValByName(fieldName, metaObject);
if(field == null && value != null) {
setFieldValByName(fieldName, value, metaObject);
}
}
/**
* 获取当前登录用户id
*/
private String getCurrentUserId(){
RequestAttributes requestAttributes = null;
try {
requestAttributes = RequestContextHolder.currentRequestAttributes();
Object obj = requestAttributes.getAttribute("userId", RequestAttributes.SCOPE_SESSION);
return obj == null ? "" : obj.toString();
} catch(Exception ex) {
return "";
}
}
}