mybatis mapper namespace

http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#insert_update_and_delete

org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.  Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in tk.mybatis.springboot.model.Course matching [java.lang.Integer, java.lang.String]
### The error may exist in mapper/CourseMapper.xml
### The error may involve tk.mybatis.springboot.mapper.CourseMapper.selectAll
### The error occurred while handling results
### SQL: select * from course
### Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in tk.mybatis.springboot.model.Course matching [java.lang.Integer, java.lang.String]
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:122)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:113)
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:122)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:64)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
    at com.sun.proxy.$Proxy6.selectAll(Unknown Source)

 

 

 

org.apache.ibatis.jdbc.RuntimeSqlException: Error executing: create TABLE course (
id INTEGER ,
NAME VARCHAR(20)
)
.  Cause: org.apache.ibatis.jdbc.RuntimeSqlException: Line missing end-of-line terminator (;) => create TABLE course (
id INTEGER ,
NAME VARCHAR(20)
)

    at org.apache.ibatis.jdbc.ScriptRunner.executeLineByLine(ScriptRunner.java:141)
    at org.apache.ibatis.jdbc.ScriptRunner.runScript(ScriptRunner.java:101)

 

 

 

org.apache.ibatis.binding.BindingException: Type interface tk.mybatis.springboot.mapper.CourseMapper is not known to the MapperRegistry.
    at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
    at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:689)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:250)

mybatis中定义行为的接口,需要在xml文件中注册:

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="tk.mybatis.springboot.mapper.CourseMapper">

问题背景:

在Dao中使用MyBatis进行查询操作,参数是传的一个List:studentNameList,但是在执行查询的时候报错,具体日志如下:

Shell代码

  1. com.chenzhou.base.mybatis.IbatisSystemException: SqlSession operation; nested exception is org.apache.ibatis.exceptions.PersistenceException:
  2. ### Error querying database. Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
  3. ### Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
  4. at com.chenzhou.base.mybatis.SqlSessionTemplate.wrapException(SqlSessionTemplate.java:341)
  5. at com.chenzhou.base.mybatis.SqlSessionTemplate.execute(SqlSessionTemplate.java:127)
  6. at com.chenzhou.base.mybatis.SqlSessionTemplate.execute(SqlSessionTemplate.java:106)
  7. at com.chenzhou.base.mybatis.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:138)
  8. at com.chenzhou.dao.GenericMybatisDao.count(GenericMybatisDao.java:306)
  9. at com.chenzhou.cds.ps.dao.impl.StudentDao.getStudentCount(StudentDao.java:42)
  10. at com.chenzhou.cds.ps.dao.impl.StudentDao$$FastClassByCGLIB$$8819e766.invoke(<generated>)
  11. at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
  12. at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
  13. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
  14. at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80)
  15. at com.chenzhou.util.LogUtil.doMethodInfo(LogUtil.java:85)
  16. at com.chenzhou.util.LogUtil.doDebugMethodLog(LogUtil.java:36)
  17. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  18. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  19. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  20. at java.lang.reflect.Method.invoke(Method.java:597)
  21. at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
  22. at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
  23. at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
  24. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  25. at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
  26. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  27. at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
  28. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  29. at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
  30. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  31. at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
  32. at com.chenzhou.cds.ps.dao.impl.StudentDao$$EnhancerByCGLIB$$d4fcf513.getStudentCount(<generated>)
  33. at com.chenzhou.ps.dao.StudentDaoTest.testgetStudentCount(StudentDaoTest.java:44)
  34. ……

单元测试用例代码如下:

Java代码

  1. @Test
  2. public void testgetStudentCount(){
  3. List<String> studentNameList = new ArrayList<String>();
  4. studentNameList.add("chenzhou");
  5. studentNameList.add("zhangsan");
  6. studentNameList.add("lisi");
  7. int count = studentDao.getStudentCount(studentNameList);
  8. System.out.println(count);
  9. }

studentDao中的getStudentCount方法代码如下:

Java代码

  1. public int getStudentCount(List<String> studentNameList){
  2. return super.count("getStudentCount", studentNameList);
  3. }

MyBatis mapper.xml定义如下:

<!-- 查询学生数量  -->
<select id="Student.getStudentCount" parameterType="java.util.List" resultType="java.lang.Integer">
   <![CDATA[
   SELECT
       COUNT(*)
   FROM
       t_student WHERE 1=1
   ]]>
   <if test="studentNameList != null">
        AND student_name in
        <foreach collection="studentNameList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </if>
</select>  

 

根据报错日志分析,是MyBatis在解析xml时找不到其中声明的studentNameList,但是在Dao中明明传的参数就是studentNameList,怎么会报错呢?

查询了一下MyBatis官方的说明文档,终于找到了原因,在http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html#foreach里有一段说明:

写道

注意 你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。
当你这么做的时 候,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。
List 实例将会以“list” 作为键,
而数组实例将会以“array”作为键。

因为我传的参数只有一个,而且传入的是一个List集合,所以mybatis会自动封装成Map<"list",studentNameList>。在解析的时候会通过“list”作为Map的key值去寻找。但是我在xml中却声明成studentNameList了,所以自然会报错找不到。

 

解决办法:

第一种就是修改mapper.xml中foreach标签内容,把studentNameList修改为list

<if test="list != null">
    AND student_name in
    <foreach collection="list" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
</if>  

 

不过这种方式我个人不太建议,因为以后如果要扩展该方法,增加集合参数的时候,还得修改xml中的内容。

 

第二种方式,修改dao中的参数传入方式,手动封装成map,然后把map当参数传进去

Dao方法修改为:

public int getStudentCount(List<String> studentNameList){
     //把参数手动封装在Map中
     Map<String, Object> map = new HashMap<String, Object>();
     map.put("studentNameList", studentNameList);
     return super.count("getStudentCount", map);
 } 

 

然后修改mapper.xml中的parameterType类型为Map

<!--注意下面的parameterType类型必须修改为Map类型,foreach中引用的List名称不用改变-->
<select id="Student.getStudentCount" parameterType="java.util.Map" resultType="java.lang.Integer">
   <![CDATA[
   SELECT
       COUNT(*)
   FROM
       t_student WHERE 1=1
   ]]>
   <if test="studentNameList != null">
        AND student_name in
        <foreach collection="studentNameList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </if>
</select>  

 

修改完后,重新执行了一下测试用例,测试通过。

 http://www.cnblogs.com/winkey4986/p/3480328.html

 

时间: 2024-08-30 14:40:54

mybatis mapper namespace的相关文章

mybatis中namespace的问题

问题描述 mybatis中namespace的问题 List userList = sqlSession.selectList(""demo1.findUsersByName""%小明%"");为什么这句话我忘记加命名空间demo1依然能查询出结果来? 解决方案 看下你的mapper配置文件,,,

mybatis mapper xml里面问号的问题

问题描述 mybatis mapper xml里面问号的问题 我的项目中,有地方用到了oracle的正则表达式,里面含有懒惰匹配".+?",将它放在mybatis里面就出现了问题,问号直接被替换为其他值不能原样输出,我是写在<![CDATA[里面的,还是不能够原样输出,而且我设置属性值为"?",然后在mapper里面${var}还是不能原样输出 解决方案 问题已解决,"?"用{0,1}代替 解决方案二: http://blog.csdn.n

tk.mybatis.mapper.provider.SpecialProvider.&lt;init&gt;()

  2017-03-26 23:44:29.192 ERROR 48392 --- [p-nio-80-exec-5] c.t.global.exception.GlobalExHandler : nested exception is org.apache.ibatis.builder.BuilderException: Error invoking SqlProvider method (tk.mybatis.mapper.provider.SpecialProvider.dynamicSQ

mybatis mapper的配置

 除了通过class属性指定Mapper接口外,Mybatis还为我们提供了一个可以同时指定多个Mapper接口的方法.在现在的Mybatis版本中我们可以在mappers元素下面定义一个package子元素,用以指定Mapper接口所在的包,这样Mybatis就会把这个包下面的所有Mapper接口都进行注册. Xml代码   <mappers>      <package name="com.tiantian.mybatis.mapperinterface"/>

mybatis mapper接口,不同包下怎么配置

问题描述 <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.mybatis.UserDao"></property> <property name="sqlSessionFactory"

【springmvc+mybatis项目实战】杰信商贸-30.出口报运增删查修mapper+Dao+Service+Controller

我们接下来做我们项目的下一个环节,就是出口报运的业务,首先我们来看看两个设计方式 a)大型项目设计方式 传统设计方式,基于对象关联,数据量小时,系统无碍:当数据随着系统的使用,线性增长,系统变的越来越慢,到达一定数据量时,性能急剧下降. b)新的设计方式:打断设计 在表中增加一个字段,这个字段用来存储关联表的主键集合:在报运业务中要查询合同下的货物信息.直接通过这个关联字段,利用in子查询直接去查询货物表,这样查询效率提高很多.而且数据量越大,查询效率越高.这种方式,业务关联层级越多,这种设计方

【MyBatis框架】mapper配置文件-关于动态sql

动态sql 1.什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 2.需求 用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql. 对查询条件进行判断,如果输入参数不为空才进行查询条件拼接. 3.mapper.xml 原查询语句配置: <mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper"> <!-- 用户信息综合

【mybatis深度历险系列】深入浅出mybatis中原始dao的开发和mapper代理开发

使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.mybatis在进行dao开发的时候,涉及到三姐妹,分别是SqlSessionFactoryBuilder.SqlSessionFactroy.SqlSession. 小伙伴们都知道,SqlSession中封装了对数据库的操作,如:增删改查,通过SqlSessionFactory创建SqlSession,而SqlSessionFactory是通过SqlSessionFactoryBuilder进行创建的

深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap good

上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select.resultMap的用法.select无疑是我们最常用,也是最复杂的,mybatis通过resultMap能帮助我们很好地进行高级映射.下面就开始看看select 以及 resultMap的用法: 先看select的配置吧: <select <!-- 1. id (必须配置) id是命名空间中的