• mybatis 整合spring之mapperLocations配置的问题


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

    复制代码
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="datasource"></property>
    <property name="typeAliasesPackage" value="com.fan.entity"/>
    <!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径。 *是个通配符,代表所有的文件,**代表所有目录下 -->
    <property name="mapperLocations" value="classpath:com/fan/mapper/*.xml" />
    <span style="color: #008000;">&lt;!--</span><span style="color: #008000;">也可以引入mybatis配置文件 </br>
        &lt;property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"&gt;&lt;/property&gt; </span><span style="color: #008000;">--&gt;</span></br>
    

    </bean>


    <!-- 通过扫描的模式,扫描目录在com.lanyuan.mapper目录下的mapper-->

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

    </bean>


    复制代码

    结论是:如果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的。

  • 相关阅读:
    php函数名后冒号(:)+数据类型(返回值类型限制/php新特性)
    qBittorrent 任务数
    TDDFT软件 octopus 编译
    visual studio code, latex workshop, setting.json
    Tex插入图片/插入tikz流程图
    记录一下讨厌的东西,就当黑名单了
    安装mingw-w64
    win7 和 win10窗口的小区别
    lattice, reciprocal lattice, OUTCAR
    v_sim 个人用户编译 无root权限
  • 原文地址:https://www.cnblogs.com/jpfss/p/8118004.html
Copyright © 2020-2023  润新知