SpringBoot+Mybatis加载l⽂件的两种⽅式
前⾔:我们在平常⼯作中⽤到mybatis去加载l⽂件,可能mapper⽂件放的路径不⼀样,由此我们需要配置多个路径,幸运的是Mybatis⽀持我们配置多个不同路径。现在介绍两种⽅法。
最近在整合shardingsphere ⽤到所以总结⼀下。
⼀、配置⽂件:
SpringBoot和Mybatis整合已经天然⽀持这种⽅式,只需要在配置⽂件添加多个路径⽤逗号隔开
mybatis:
mapper-locations: classpath*:com/pab/cc/fas/mapper/*Mapper*.xml,classpath*:com/pab/cc/ces/mapper/*Mapper*.xml,classpath*:com/pab/cc/ams/mapper/*Mapper*.xml type-aliases-package: com.urthink.ity
#IDENTITY: MYSQL #取回主键的⽅式
#notEmpty: false #insert和update中,是否判断字符串类型!=''
configuration:
#进⾏⾃动映射时,数据以下划线命名,如数据库返回的"order_address"命名字段是否映射为class的"orderAddress"字段。默认为false
map-underscore-to-camel-case: true
# 输出SQL执⾏语句(log4j2本⾝可以输出sql语句)
⼆、Javabean配置
主要⽤到的是SqlSessionFactoryBean的setMapperLocations(),这个⽅法需要传⼊resource数组。
public SqlSessionFactory sqlSessionFactory() {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSourceOne());
sqlSessionFactoryBean.setMapperLocations(resolveMapperLocations());
Object();
}
public Resource[] resolveMapperLocations() {
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
List<String> mapperLocations = new ArrayList<>();
springboot切换log4j2mapperLocations.add("classpath*:com/pab/cc/fas/mapper/*Mapper*.xml");
mapperLocations.add("classpath*:com/pab/cc/ces/mapper/*Mapper*.xml");
mapperLocations.add("classpath*:com/pab/cc/ams/mapper/*Mapper*.xml");
List<Resource> resources = new ArrayList();
if (mapperLocations != null) {
for (String mapperLocation : mapperLocations) {
try {
Resource[] mappers = Resources(mapperLocation);
resources.addAll(Arrays.asList(mappers));
} catch (IOException e) {
// ignore
}
}
}
Array(new Resource[resources.size()]);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论