项目中MyBatis配置
@Slf4j
@ConditionalOnResource(resources = {"classpath:mapper"})
@ConditionalOnClass({SqlSessionFactory.class})
public class MyBatisAutoConfiguration {
@Autowired
private Interceptor[] interceptors;
@Bean(name = "sqlSessionFactory")
@ConditionalOnMissingBean(value = SqlSessionFactory.class, name = {"sqlSessionFactory", "SqlSessionFactory"})
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource,
MapperHelper mapperHelper) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// 设置mybatis XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
//配置通用mapper
Configuration configuration = new Configuration();
configuration.setMapperHelper(mapperHelper);
bean.setConfiguration(configuration);
//配置插件
bean.setPlugins(interceptors);
log.info("db plugins is set");
SqlSessionFactory sqlSessionFactory = bean.getObject();
//-自动使用驼峰命名属性映射字段 userId user_id
assert sqlSessionFactory != null;
sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
log.info("SqlSessionFactory is ready to inject.");
return sqlSessionFactory;
}
/**
* 通用mapper,配置说明详见https://github.com/abel533/Mapper/wiki/3.config
*/
@Bean
public MapperHelper mapperHelper() {
Config config = new Config();
config.setIDENTITY("MYSQL");
config.setNotEmpty(false);
config.setOrder("AFTER");
config.setEnableMethodAnnotation(true);
config.setCheckExampleEntityClass(true);
config.setUseSimpleType(true);
config.setEnumAsSimpleType(true);
config.setWrapKeyword("`{0}`");
config.setSafeDelete(true);
config.setSafeUpdate(true);
MapperHelper mapperHelper = new MapperHelper();
mapperHelper.setConfig(config);
log.info("tk.db.mapper is ready to inject.");
return mapperHelper;
}
@Bean(name = "SqlSessionTemplateForFramework")
@ConditionalOnMissingBean(value = SqlSessionTemplate.class, name = {"sqlSessionTemplateForFramework", "SqlSessionTemplateForFramework"})
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
mapper扫描
@Slf4j
@ConditionalOnResource(resources = {"classpath:mapper"})
@ConditionalOnClass({SqlSessionFactory.class})
public class MapperScannerAutoConfiguration {
@Bean(name = "MapperScannerConfigurerForFramework")
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
// 设置mybatis mapper接口目录
mapperScannerConfigurer.setBasePackage("com.**.*.mapper");
return mapperScannerConfigurer;
}
}
MyBatisPlus配置
@Slf4j
@Configuration
@ConditionalOnBean(MysqlAutoConfiguration.class)
public class MyBatisPluginAutoConfiguration {
/**
* 分页插件pageHelper 配置
*/
@Bean
public PageInterceptor pageHelper() {
log.info("PageInterceptor is ready to inject.");
Properties properties = new Properties();
//reasonable:分页合理化参数,默认值为false。
// 当该参数设置为 true 时,pageNum<=0 时会查询第一页, p
// ageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。
properties.setProperty("reasonable", "true");
//supportMethodsArguments:支持通过 Mapper 接口参数来传递分页参数,默认值false,
// 分页插件会从查询方法的参数值中,自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页。
properties.setProperty("supportMethodsArguments", "true");
//指定为MySQL数据库
properties.setProperty("helperDialect", "mysql");
PageInterceptor pageInterceptor = new PageInterceptor();
pageInterceptor.setProperties(properties);
return pageInterceptor;
}
}
一些条件注解的使用,配置文件位置指定注解。如需更多了解,请百度之。