MyBatis -- BUG集
bug 1、
提示、
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in mybatis/mappers/empMapper.xml
### The error occurred while processing mapper_resultMap[deptRM]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'mybatis/mappers/empMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'deptRM'. Cause: java.lang.ClassNotFoundException: Cannot find class: deptRM
原因、
在 xml 映射文件的 select 标签中 ,错误使用了 resultType="deptRM"
<select id="selectDeptById" resultType="deptRM">
select dept_id,dept_name from dept
</select>
解决办法、
把 resultType="deptRM" 改为 resultMap="deptRM"
<select id="selectDeptById" resultMap="deptRM">
select dept_id,dept_name from dept
</select>
bug2、
提示、
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: Statement returned more than one row, where no more than one was expected.
### The error may exist in mybatis/mappers/empMapper.xml
### The error may involve com.jt.springboot_demo3_mybatis.mapper.EmpMapper.selectDeptById
### The error occurred while handling results
### SQL: select dept_id,dept_name from dept
### Cause: org.apache.ibatis.executor.ExecutorException: Statement returned more than one row, where no more than one was expected.
原因、
在 xml 映射文件的 sql 语句编写中 ,条件查询没有 where 条件
<select id="selectDeptById" resultMap="deptRM">
select dept_id,dept_name from dept
</select>
解决方法、
添加 where 子句
<select id="selectDeptById" resultMap="deptRM">
select dept_id,dept_name from dept where dept_id = #{dept_id}
</select>
bug3、
提示、
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in mybatis/mappers/deptMapper.xml
### The error occurred while processing mapper_resultMap[deptRM]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'mybatis/mappers/deptMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Ambiguous collection type for property 'lists'. You must specify 'javaType' or 'resultMap'.
原因、
尝试使用左连接直接查询,使用失败,理解不够
解决方法、
使用子查询
bug4、
提示、
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'lists' of 'class com.jt.springboot_demo3_mybatis.pojo.Dept' with value '[Emp(empId=1, empName=妲己, empAge=18, dept=null), Emp(empId=5, empName=小乔, empAge=18, dept=null), Emp(empId=6, empName=大乔, empAge=19, dept=null)]' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'lists' in 'class com.jt.springboot_demo3_mybatis.pojo.Dept'
### The error may exist in mybatis/mappers/deptMapper.xml
### The error may involve com.jt.springboot_demo3_mybatis.mapper.DeptMapper.findEmp
### The error occurred while handling results
### SQL: select emp_id,emp_name,emp_age from emp where dept_id = ?
### Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'lists' of 'class com.jt.springboot_demo3_mybatis.pojo.Dept' with value '[Emp(empId=1, empName=妲己, empAge=18, dept=null), Emp(empId=5, empName=小乔, empAge=18, dept=null), Emp(empId=6, empName=大乔, empAge=19, dept=null)]' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'lists' in 'class com.jt.springboot_demo3_mybatis.pojo.Dept'
原因、
在 resultMap 里 collection 标签的 property 错误写为 “lists”
<collection property="lists" ofType="Emp" column="dept_id" select="findEmp"/>
解决方法、
将 property = ”lists“ 改为 property = “emps“
<collection property="emps" ofType="Emp" column="dept_id" select="findEmp"/>
bug5、
提示、
在xml文件使用子查询时没有dept 信息
[Emp(empId=1, empName=妲己, empAge=18, dept=null), Emp(empId=2, empName=貂蝉, empAge=19, dept=null)]
原因、
在主查询中没有查询到关联字段 dept_id 以至于子查询的 where dept_id = #{dept_id} 无用
<select id="finAllEmpAndDept" resultMap="allEmpRM">
select emp_id,emp_name,emp_age from emp
</select>
解决方法、
在主查询中 查询到 表间关联字段
<select id="finAllEmpAndDept" resultMap="allEmpRM">
select emp_id,emp_name,emp_age,dept_id from emp
</select>
SpringBoot -- BUG 合集
bug1、
提示、
Field userMapper in com.jt.service.UserServiceImpl required a bean of type 'com.jt.mapper.UserMapper' that could not be found.
原因、
UserMapper 接口没有交给 Spring 容器管理
解决方法、
在UserMapper 接口上加 @Mapper 注解
或 在启动类上加 @MapperScan("mapper类全路径") 注解
bug2、
提示、
2021-09-28 15:29:24.110 ERROR 9332 --- [nio-8090-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.jt.service.UserService.findAll] with root cause
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.jt.service.UserService.findAll
原因、
在启动类上的的 @MapperScan 注解没写完整
解决方法、
@SpringBootApplication
//根据包扫描路径 扫描全部的mapper接口文件
@MapperScan("com.jt.mapper")
public class SpringbootSsmApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSsmApplication.class, args);
}
}