1. Today Bookmark
SpringBoot MyBatis integrated projects, how to use sqlSessionFactoryBean.setMapperLocations
wildcards to configure multiple packages trails?
Or, through MyBatis JavaBean
a form, how to use setMapperLocations load xml file multiple paths?
Or also, use sqlSessionFactoryBean.setMapperLocations
or mybatis.mapper-locations
wildcards to configure multiple paths mapper What are the two ways to set?
The project technology stack upgrades SpringBoot 2 + MyBatis 3. Talk about the pits that have been stepped on, and look at the sword in the dream.
2. Look at the sword
1) Use JavaBean configuration
Mainly 使用 org.mybatis.spring.SqlSessionFactoryBean 的 setMapperLocations(Resource... mapperLocations) 方法
, the participation is 一组 Resource
, that is, we 需要加载的 xml 文件路径数组
.
@Slf4j
@Configuration
@MapperScan(basePackages = {
"com.meiwei.tan.dao.mall", "com.meiwei.ping.dao.crm"})
public class MyBatisConfiguration {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(resolveMapperLocations());
return sqlSessionFactoryBean.getObject();
}
public Resource[] resolveMapperLocations() {
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
List<String> mapperLocations = new ArrayList<>();
mapperLocations.add("classpath*:com/meiwei/tan/dao/**/*Dao.xml");
mapperLocations.add("classpath*:com/meiwei/ping/dao/**/*Mapper.xml");
List<Resource> resources = new ArrayList();
if (!CollectionUtils.isEmpty(mapperLocations)) {
for (String mapperLocation : mapperLocations) {
try {
Resource[] mappers = resourceResolver.getResources(mapperLocation);
resources.addAll(Arrays.asList(mappers));
} catch (IOException e) {
log.error("Get myBatis resources happened exception", e);
}
}
}
return resources.toArray(new Resource[resources.size()]);
}
2) Use application.yml configuration
Now application.yml 配置中集成 mybatis
, you may have to write multiple copies.
spring:
application:
name: meiwei
profiles:
active: dev
apollo:
bootstrap:
enabled: true
namespaces: application,db,dubbo,redis,datasource,zk,mq
gatewayapi:
client:
autoRegistry:
enabled: true
connectStr: ${
zk.address} #${zk.address} 来自于 apollo 里面的 zk
root: /gateway/api/services
mybatis:
mapper-locations: classpath*:com/meiwei/tan/dao/**/*Dao.xml,classpath*:com/meiwei/ping/dao/**/*Mapper.xml
type-aliases-package: com.meiwei.tan.dao.mall,com.meiwei.ping.dao.crm
IDENTITY: MYSQL #取回主键的方式
notEmpty: false #insert和update中,是否判断字符串类型!=''
configuration: #进行自动映射时,数据以下划线命名,如数据库返回的"order_total"命名字段是否映射为class的"orderTotal"字段。默认为false
map-underscore-to-camel-case: true
For MyBatis, here you can write an empty custom MyBatisConfiguration class without implementation, just add @MapperScan and @Configuration annotations so that the MyBatisConfiguration class can be used as the mybatis configuration file to be loaded, and to scan the dao interface under the specified package path. Then start the main startup class.
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan(value = {
"com.meiwei.tan.dao.mall", "com.meiwei.ping.dao.crm"})
public class MyBatisConfiguration {
}
It is also possible not to write this custom MyBatisConfiguration class, and directly annotate the @MapperScan configuration to the main startup class.
However, it is not difficult to see that the unit test of the business layer module is not easy to read the yml configuration of the web or remote module, so the second method uses application.yml to configure the integration of mybatis. After starting the main program method, it is to run in the module where the main program is located Test cases (consumed through RPC services) instead of running test cases in the business layer module.