ssm mybatis如何配置多个mapper目录
作者:信行合一 时间:2021-12-06 14:08:25
ssm mybatis配置多个mapper目录
通配符配置
比如目录的结构如下:
com/test/web/user/mapper
com/test/web/order/mapper
com/test/web//mapper
<!-- 扫描 xxxMapper.xml -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:spring/mybatis-config.xml"/>
<property name="mapperLocations" value="classpath*:com/xxxcompany/xxxproject/*/mapper/*.xml" />
</bean>
<!-- 扫描 xxxMapper -->
<bean id="daoScan" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxxcompany.xxxproject.*.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
除了上面这种目录结构一致的方式,我们还可以使用下面的配置方式,这个意思也就是说只要是符合mapper* 这样的命名规则的包都会被扫描到
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath*:com/dada/**/*Mapper.xml"/>
</bean>
<!-- 配置扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dada.test.mapper*"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
ssm项目 mappers标签
mappers标签标签的作用
为了让mybatis找到mapper.xml文件。
在后面的ssm整合项目中,有两种方式可以代替它
一个是使用MapperScannerConfigurer
一个是使用mapperLocation属性(位于SqlSessionFactoryBean中):mapperLocation属性,主要用于指定mapper.xml文件所处的位置。
MapperScannerConfigurer可以自动扫描到和接口同包同名的mapper.xml文件
mapperLocation属性可以指定扫描。
下面来个逆向思考!!!
在ssm项目中,将mapper.xml文件放进和dao接口不同的包中,这样的话MapperScannerConfigurer的自动扫描就扫描不到mapper.xml文件了。然后不使用mapperLocation属性,意思就是使用mappers标签来代替两个新方法,来验证一下。
但是结果却和我想的不一样!!!下面我来说我遇到的一些坑!!!
mappers标签的配置方式有4种
<!-- 1.使用相对于类路径的资源引用 -->
<mappers>
<mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
</mappers>
<!-- 2.使用完全限定资源定位符(URL) -->
<mappers>
<mapper url="file:///var/mappers/AuthorMapper.xml"/>
</mappers>
<!-- 3.使用映射器接口实现类的完全限定类名 -->
<mappers>
<mapper class="org.mybatis.builder.AuthorMapper"/>
<mapper class="org.mybatis.builder.BlogMapper"/>
<mapper class="org.mybatis.builder.PostMapper"/>
</mappers>
<!-- 4.将包内的映射器接口实现全部注册为映射器 -->
<mappers>
<package name="org.mybatis.builder"/>
</mappers>
使用第3种和第4种方法需要和MapperScannerConfigurer一样,将mapper.xml文件和dao接口同名同包。
对于第一种方法,我们需要注意的是:/ 和 .
蜿蜒曲折的探究道路
对于指定类路径的资源引用的方式,也就是第一种方式,我将mapper.xml文件所在包放进了dao接口所在包的子包。结果idea提示说找不到mapper.xml文件,然后我又调了一会(将mapper.xml文件所在包改变位置后)可以正常运行,然后我就以为mapper.xml文件所在包不能放进了dao接口所在包的子包。我又重新把mapper.xml文件所在包放进了dao接口所在包的子包,结果能正常运行了。
来源:https://blog.csdn.net/tengdazhang770960436/article/details/67640854
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java中transient关键字用法分析
spring boot 动态生成接口实现类的场景分析
![](https://img.aspxhome.com/file/2023/7/64207_0s.png)
多线程如何解决for循环效率的问题
![](https://img.aspxhome.com/file/2023/6/64056_0s.jpg)
java中方法递归的简单示例
![](https://img.aspxhome.com/file/2023/1/66041_0s.png)
java自定义ClassLoader加载指定的class文件操作
![](https://img.aspxhome.com/file/2023/8/60848_0s.jpg)
java 接口回调实例详解
SpringCloud分布式链路追踪组件Sleuth配置详解
![](https://img.aspxhome.com/file/2023/6/60666_0s.png)
SpringBoot使用SensitiveWord实现敏感词过滤
Java JDK动态代理(AOP)用法及实现原理详解
![](https://img.aspxhome.com/file/2023/2/69292_0s.png)
应用启动数据初始化接口CommandLineRunner和Application详解
![](https://img.aspxhome.com/file/2023/8/65608_0s.jpg)
mybatis中resultMap 标签的使用教程
![](https://img.aspxhome.com/file/2023/3/68533_0s.png)
Studio 编译报错:compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.的解决办法
![](https://img.aspxhome.com/file/2023/0/81320_0s.png)
从 JVM 中深入探究 Synchronized作用及原理
IDEA解决src和resource下创建多级目录的操作
![](https://img.aspxhome.com/file/2023/0/63690_0s.jpg)
Android Studio 多层级 Module 对 aar 引用问题解决方法
![](https://img.aspxhome.com/file/2023/2/86762_0s.png)
详解如何在SpringBoot项目中使用统一返回结果
![](https://img.aspxhome.com/file/2023/4/58714_0s.png)
Java Collection集合iterator方法解析
Spring Boot2发布调用REST服务实现方法
![](https://img.aspxhome.com/file/2023/1/71141_0s.png)
SpringAOP 如何通过JoinPoint获取参数名和值
![](https://img.aspxhome.com/file/2023/1/69351_0s.png)