wang3048
wang3048
2017-06-13 05:17

spring、mybatis整合遇到的问题

  • spring
  • mybatis
  • 单元测试

单元测试报错:
Invalid bound statement (not found): com.cn.mysite
.mybatisdemo.dao.IUserDao.queryUserList
下面相关代码和配置文件:
1、先放上结构图:
图片说明
2、spring-mybatis.xml配置文件代码如下:

<context:component-scan base-package="com.cn.mysite.mybatisdemo" />

<!-- jdbc配置文件导入 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <!-- 连接池配置 -->
    <property name="driverClassName" value="${database.driverClassName}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>

    <!-- 初始化连接大小 -->
    <property name="initialSize" value="0"></property>
    <!-- 连接池最大数量 -->
    <property name="maxActive" value="20"></property>
    <!-- 连接池最大空闲 -->
    <property name="maxIdle" value="20"></property>
    <!-- 连接池最小空闲 -->
    <property name="minIdle" value="1"></property>
    <!-- 获取连接最大等待时间 -->
    <property name="maxWait" value="30000"></property>
</bean>

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mapperLocations" value="classpath*:com/cn/mysite/mybatisdemo/mapping/*.xml"/>
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.cn.mysite.mybatisdemo.dao" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

3、IUserDao接口代码:

public interface IUserDao {

List<User> queryUserList();

}


 4、mapping中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.cn.mysite.mybatisdemo.dao.IUserDao" >
      <resultMap type ="com.cn.mysite.mybatisdemo.model.User" id="usermap">
             <result property ="id" column="id" />
             <result property ="name" column="name" />
             <result property ="age" column="age" />
             <result property ="email" column="email" />
             <result property ="address" column="address" />
      </resultMap>
      <!-- 查询所有用户记录 -->
      <select id ="queryUserList" resultMap= "usermap">
            select * from person
      </select >
</mapper>

5、测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class MybatisTest {

    @Resource
    private IUserDao dao;

    @Test
    public void test() {
        dao.queryUserList();
        System.out.println("Hello World!");
    }
}
单元测试就开始报错,请大家帮看一下是哪一步出错了,感觉好奇怪啊,实在查不出问题了



  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答