當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot+Mybatis加载Mapper.xml文件的两种方式
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot+Mybatis加载Mapper.xml文件的两种方式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前言:我們在平常工作中用到mybatis去加載Mapper.xml文件,可能mapper文件放的路徑不一樣,由此我們需要配置多個路徑,幸運的是Mybatis支持我們配置多個不同路徑。現(xiàn)在介紹兩種方法。
一、配置文件:
?
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*.xmltype-aliases-package: com.urthink.upfs.springbootmybatis.entity#IDENTITY: MYSQL #取回主鍵的方式#notEmpty: false #insert和update中,是否判斷字符串類型!=''configuration:#進行自動映射時,數(shù)據(jù)以下劃線命名,如數(shù)據(jù)庫返回的"order_address"命名字段是否映射為class的"orderAddress"字段。默認為falsemap-underscore-to-camel-case: true# 輸出SQL執(zhí)行語句 (log4j2本身可以輸出sql語句)二、Javabean配置
主要用到的是SqlSessionFactoryBean的setMapperLocations(),這個方法需要傳入resource數(shù)組。
public SqlSessionFactory sqlSessionFactory() {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSourceOne());sqlSessionFactoryBean.setMapperLocations(resolveMapperLocations());return sqlSessionFactoryBean.getObject();}public Resource[] resolveMapperLocations() {ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();List<String> mapperLocations = new ArrayList<>();mapperLocations.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 = resourceResolver.getResources(mapperLocation);resources.addAll(Arrays.asList(mappers));} catch (IOException e) {// ignore}}}return resources.toArray(new Resource[resources.size()]);}參考文獻:https://blog.csdn.net/TreeShu321/article/details/104547973
基礎:請參考:
SpringBoot+Mybatis(JavaConfig方式配置)
https://blog.csdn.net/qq_37598011/article/details/80669608
?
總結
以上是生活随笔為你收集整理的SpringBoot+Mybatis加载Mapper.xml文件的两种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Springboot本地缓存和redis
- 下一篇: 网关和BFF是如何演进出来的?