spring整合mybatis中mapper.xml加载问题

img

mybatis-config.xml



configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    settings>

    <mappers>
        <mapper class="com.mytest.dao">mapper>
    mappers>
configuration>

spring.xml



<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    
    <context:property-placeholder location="classpath:jdbc.properties"/>

    
    <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    bean>


    
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="myDataSource"/>
        
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="typeAliasesPackage" value="com.mytest.pojo">property>
    bean>


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


beans>

img

在mybatis-config.xml文件加上这句,代码就会报错 不加就可以正常运行。

我感到奇怪的是 spring配置文件和mybatis配置文件都没加载 mapper.xml文件呀,还是自动加载的呢?

你的配置写的不对,正确的写法应该是下面这种,class这种需要是 类的全路径名

    <mappers>
        <package name="com.mytest.dao"></package >
    </mappers>

可以参考 mybatis中文官方文档 https://mybatis.net.cn/configuration.html#mappers

在spring配置文件中 在注入

 <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="myDataSource"/>
        
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="typeAliasesPackage" value="com.mytest.pojo"></property>
    </bean>

 <property name="mapperLocations"  value="classpath:com/mytest/dao/*.xml"/> 

这个属性注入是不是可以省略呀