美文网首页我爱编程
mybatis 整合spring之mapperLocations

mybatis 整合spring之mapperLocations

作者: 秋名山车神_f776 | 来源:发表于2018-04-12 20:12 被阅读0次

今天尝试spring整合mybatis时遇到这么一个问题,就是在配置sqlSessionFactory时是否要配置mapperLocations的问题。

[ 复制代码

](javascript:void(0); "复制代码")

<pre><bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"></property>
<property name="typeAliasesPackage" value="com.fan.entity"/>

<property name="mapperLocations" value="classpath:com/fan/mapper/*.xml" />

<!--也可以引入mybatis配置文件 
    <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property> -->

</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fan.mapper"></property>
</bean></pre>

[ 复制代码

](javascript:void(0); "复制代码")

结论是:如果Mapper.xml与Mapper.class在同一个包下且同名,spring扫描Mapper.class的同时会自动扫描同名的Mapper.xml并装配到Mapper.class。

如果Mapper.xml与Mapper.class不在同一个包下或者不同名,就必须使用配置mapperLocations指定mapper.xml的位置。

此时spring是通过识别mapper.xml中的 <mapper namespace="com.fan.mapper.UserDao"> namespace的值来确定对应的Mapper.class的。

相关文章

网友评论

    本文标题:mybatis 整合spring之mapperLocations

    本文链接:https://www.haomeiwen.com/subject/snbfkftx.html