当前位置: 代码迷 >> Java Web开发 >> Spring-Mybatis框架找不到Mapper下的方法的有关问题
  详细解决方案

Spring-Mybatis框架找不到Mapper下的方法的有关问题

热度:43   发布时间:2016-04-13 22:17:57.0
Spring-Mybatis框架找不到Mapper下的方法的问题
小弟想学习一下Mybatis,于是照着这篇大神的博文依样画葫芦,但是每一步都几乎相同了,最后总会报这个错
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.gcoreinc.dao.BoxDetailMapper.selectBoxDetailByBoxId

先贴一下我的代码,编辑器用的Intellij
项目目录如下:

spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 引入dbconfig.properties属性文件 -->
    <context:property-placeholder location="classpath:dbconfig.properties" />
    <!-- 自动扫描(自动注入),扫描com.gcoreinc.service这个包以及它的子包的所有使用@Service注解标注的类 -->
    <context:component-scan base-package="com.gcoreinc.service" />
</beans>


spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">

    <!--配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driverClassName}"/>
        <property name="url" value="${jdbc_url}"/>
        <property name="username" value="${jdbc_username}"/>
        <property name="password" value="${jdbc_password}"/>
    </bean>
    <!-- ========================================针对myBatis的配置项============================== -->
    <!-- 配置sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 -->
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:*Mapper.xml"/>
    </bean>
    <!-- 配置扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描me.gacl.dao这个包以及它的子包下的所有映射接口类 -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.gcoreinc.dao"/>
    </bean>

    <!-- ========================================分隔线========================================= -->
    <!-- 配置Spring的事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 注解方式配置事物 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.gcoreinc.dao.BoxDetailMapper">
    <resultMap type="BoxDetail" id="BoxDetailMap">
        <result property="boxId" column="BOX_ID" jdbcType="VARCHAR2"/>
        <result property="itemId" column="ITEM_ID" jdbcType="VARCHAR2"/>
        <result property="itemNum" column="ITEM_NUM" jdbcType="VARCHAR2"/>
        <result property="itemType" column="ITEM_TYPE" jdbcType="VARCHAR2"/>
        <result property="boxType" column="BOX_TYPE" jdbcType="VARCHAR2"/>
        <result property="userId" column="USER_ID" jdbcType="VARCHAR2"/>
        <result property="deptid" column="DEPTID" jdbcType="VARCHAR2"/>
        <result property="actionTime" column="ACTION_TIME" jdbcType="VARCHAR2"/>
        <result property="eqptid" column="EQPTID" jdbcType="VARCHAR2"/>
        <result property="dataVal1" column="DATA_VAL1" jdbcType="VARCHAR2"/>
        <result property="dataVal2" column="DATA_VAL2" jdbcType="VARCHAR2"/>
        <result property="dataVal3" column="DATA_VAL3" jdbcType="VARCHAR2"/>
        <result property="dataVal4" column="DATA_VAL4" jdbcType="VARCHAR2"/>
        <result property="dataVal5" column="DATA_VAL5" jdbcType="VARCHAR2"/>
        <result property="dataVal6" column="DATA_VAL6" jdbcType="VARCHAR2"/>
        <result property="dataVal7" column="DATA_VAL7" jdbcType="VARCHAR2"/>
        <result property="dataVal8" column="DATA_VAL8" jdbcType="VARCHAR2"/>
    </resultMap>
    <select id="selectBoxDetailByBoxId" parameterType="String" resultMap="BoxDetailMap">
        SELECT * FROM BOX_DETAIL
        WHERE BOX_ID = #{boxID}
    </select>
</mapper>


Mapper.java
package com.gcoreinc.dao;

import com.gcoreinc.domain.BoxDetail;

import java.util.List;

public interface BoxDetailMapper {
    public List<BoxDetail> selectBoxDetailByBoxId(String boxId);
}

最后是测试类,Service里面只有一个方法,就不用贴了吧
package com.gcoreinc.service;

import com.gcoreinc.domain.BoxDetail;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-mybatis.xml"})
public class BoxDetailServiceTest {

    @Autowired
    private BoxDetailService service;

    @Test
    public void getBoxDetailByBoxIdTest(){
        List<BoxDetail> boxDetailList = service.getBoxDetailByBoxId("VSH130403393");
        for (BoxDetail boxDetail : boxDetailList) {
            System.out.println(boxDetail);
        }
    }
}

运行测试方法之后就会报错,确实不知道什么地方配置错了或怎么样,请各路大神帮忙看看,多谢多谢
------解决思路----------------------
如果你没有配置别名,使用resultMap 时 type=“类” 用全路径。
  相关解决方案