标签:
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,但是在执行查询的时候报错,具体日志如下:
单元测试用例代码如下:
studentDao中的getStudentCount方法代码如下:
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会自动封装成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
标签:
原文地址:http://www.cnblogs.com/softidea/p/5816446.html