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,但是在执行查询的时候报错,具体日志如下:
- com.chenzhou.base.mybatis.IbatisSystemException: SqlSession operation; nested exception is org.apache.ibatis.exceptions.PersistenceException:
- ### Error querying database. Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
- ### Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
- at com.chenzhou.base.mybatis.SqlSessionTemplate.wrapException(SqlSessionTemplate.java:341)
- at com.chenzhou.base.mybatis.SqlSessionTemplate.execute(SqlSessionTemplate.java:127)
- at com.chenzhou.base.mybatis.SqlSessionTemplate.execute(SqlSessionTemplate.java:106)
- at com.chenzhou.base.mybatis.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:138)
- at com.chenzhou.dao.GenericMybatisDao.count(GenericMybatisDao.java:306)
- at com.chenzhou.cds.ps.dao.impl.StudentDao.getStudentCount(StudentDao.java:42)
- at com.chenzhou.cds.ps.dao.impl.StudentDao$$FastClassByCGLIB$$8819e766.invoke(<generated>)
- at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
- at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
- at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80)
- at com.chenzhou.util.LogUtil.doMethodInfo(LogUtil.java:85)
- at com.chenzhou.util.LogUtil.doDebugMethodLog(LogUtil.java:36)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
- at java.lang.reflect.Method.invoke(Method.java:597)
- at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
- at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
- at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
- at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
- at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
- at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
- at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
- at com.chenzhou.cds.ps.dao.impl.StudentDao$$EnhancerByCGLIB$$d4fcf513.getStudentCount(<generated>)
- at com.chenzhou.ps.dao.StudentDaoTest.testgetStudentCount(StudentDaoTest.java:44)
- ……
单元测试用例代码如下:
- @Test
- public void testgetStudentCount(){
- List<String> studentNameList = new ArrayList<String>();
- studentNameList.add("chenzhou");
- studentNameList.add("zhangsan");
- studentNameList.add("lisi");
- int count = studentDao.getStudentCount(studentNameList);
- System.out.println(count);
- }
studentDao中的getStudentCount方法代码如下:
- public int getStudentCount(List<String> studentNameList){
- return super.count("getStudentCount", studentNameList);
- }
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里有一段说明:
当你这么做的时 候,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
mybatis mapper namespace的更多相关文章
- 关于mybatis 的mapper namespace 作用及解析
因为语言惯性,大部分的namespace 在语言级别*来说是作为一种限定性标识来用,起到唯一或一类的标识.来看看语言(以PHP语言为例)上的namespace的作用实例 一.namespace 在PH ...
- mybatis mapper association collection
1.Question Description: sometimes, POJO bean contains another bean or collection as property, it's s ...
- MyBatis Mapper 接口如何通过JDK动态代理来包装SqlSession 源码分析
我们以往使用ibatis或者mybatis 都是以这种方式调用XML当中定义的CRUD标签来执行SQL 比如这样 <?xml version="1.0" encoding=& ...
- MyBatis Mapper 文件例子
转载:http://blog.csdn.net/ppby2002/article/details/20611737 <?xml version="1.0" encoding= ...
- XML CDATA(Mybatis mapper and XML)
Tip:must be followed by either attribute specifications, ">" or "/>". 所有 X ...
- mybatis mapper.xml 写关联查询 运用 resultmap 结果集中 用 association 关联其他表 并且 用 association 的 select 查询值 报错 java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mybatis.map
用mybaits 写一个关联查询 查询商品表关联商品规格表,并查询规格表中的数量.价格等,为了sql重用性,利用 association 节点 查询 结果并赋值报错 商品表的mapper文件为Gooo ...
- 初识 tk.mybatis.mapper
在博客园发表Mybatis Dynamic Query后,一位园友问我知不知道通用mapper,仔细去找了一下,还真的有啊,比较好的就是abel533写的tk.mybatis.mapper. 本次例子 ...
- Java DB 访问之 mybatis mapper xml 配置方式
1 项目说明 项目采用 maven 组织 ,jdbc 唯一的依赖就是 mysql-connector-java pom 依赖如下: mysql 数据连接 : mysql-connector-java ...
- mybatis mapper映射文件全解
目录 select.update.delete.insert 设置参数类型以及取值 基本数据类型 对象数据类型 map数据类型 #{ } 和 ${ } 的区别 ResultMap Auto-map ...
随机推荐
- java笔记14之private
private: 1 是一个权限修饰符 2 可以修饰成员变量和成员方法 被其修饰的成员只能在本类中被访问 class Demo { //int num = 1 ...
- PHP读取文件内容的三种方式
<?php // 第一种读取方式 header("content-type:text/html;charset=utf-8"); // 文件路径 $fileA = " ...
- html表格合并(行,一排)
<table> <tr> <td colspan="2">失败的例子:</td> </tr> {% for ip , j ...
- 《AngularJS》--指令的相互调用
转载自http://blog.csdn.net/zhoukun1008/article/details/51296692 人们喜欢AngularJS,因为他很有特色,其中他的指令和双向数据绑定很吸引着 ...
- 【移动开发】Android中WIFI开发总结(一)
WIFI就是一种无线联网技术,常见的是使用无线路由器.那么在这个无线路由器的信号覆盖的范围内都可以采用WIFI连接的方式进行联网.如果无线路由器连接了一个ADSL线路或其他的联网线路,则又被称为“热 ...
- 向量的叉积 POJ 2318 TOYS & POJ 2398 Toy Storage
POJ 2318: 题目大意:给定一个盒子的左上角和右下角坐标,然后给n条线,可以将盒子分成n+1个部分,再给m个点,问每个区域内有多少各点 这个题用到关键的一步就是向量的叉积,假设一个点m在 由ab ...
- Volley 百财帮封装
Activity public class MainActivity extends Activity implements OnClickListener { private Context ...
- C# 面向对象 , 类与对象
一,类的字段 类的字段, 就是类里面的 数据. 二,类的方法 1 , 函数的重载 , 如何判断: 是由两个或多个同名函数组成的,但是函数要有不同的参数.或个数.(参数 是函数的输入的东西) shu ...
- Mysql group_concat
select p.id,p.parent_id,group_concat(distinct(CONCAT("分类名称:",c.name)) order by c.id desc s ...
- YII框架路由和URL生成
路由和URL生成 当一个YII应用开始处理一个请求的时候,它首先要做的便是将请求的URL转化成一个路由.路由的作用是用于后续实例化相应的控制器和操作,以便处理请求,整个处理过程便叫做路由.路由的逆过程 ...