http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#insert_update_and_deletehtml
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文件中註冊:java
<!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">
問題背景:git
在Dao中使用MyBatis進行查詢操做,參數是傳的一個List:studentNameList,可是在執行查詢的時候報錯,具體日誌以下:github
單元測試用例代碼以下:spring
studentDao中的getStudentCount方法代碼以下:sql
MyBatis mapper.xml定義以下:apache
<!-- 查詢學生數量 --> <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裏有一段說明:springboot
由於我傳的參數只有一個,並且傳入的是一個List集合,因此mybatis會自動封裝成Map<"list",studentNameList>。在解析的時候會經過「list」做爲Map的key值去尋找。可是我在xml中卻聲明成studentNameList了,因此天然會報錯找不到。session
解決辦法:
第一種就是修改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