异常1.使用映射器 (还没有使用Spring)
异常信息摘要:
org.apache.ibatis.binding.BindingException: Type interface com.jege.mybatis.mapper.UserMapper 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:717)
at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:292)
at com.jege.mybatis.MapperInterfaceTest.setUp(MapperInterfaceTest.java:38)
异常信息说明:UserMapper还没有注册
解决思路:注册一个空的映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jege.mybatis.mapper.UserMapper">
</mapper>
ps:如果使用Spring集成MyBatis后没有此问题
异常2.在使用MyBatis映射器的时候,注解映射配置查询所有数据
异常信息摘要:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.UnsupportedOperationException
### The error may exist in com/jege/framework/mapper/UserMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select id,name,password from t_user
### Cause: java.lang.UnsupportedOperationException
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:107)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:98)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
异常信息说明:嵌套异常
解决思路:注册一个空的映射文件
@Select("select id,name,password from t_user")
List getAll();
原来没有写泛型参数保存,修改为
@Select("select id,name,password from t_user")
List<User> getAll();就可以了,
相当于xml配置了resultType="User"
异常3.模型类是User类,实际在映射文件写成了Dept类
异常信息摘要:
org.apache.ibatis.exceptions.PersistenceException:
Error building SqlSession.
The error may exist in
com/jege/mybatis/mapper/UserMapper.xml
Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Dept'. Cause: java.lang.ClassNotFoundException: Cannot find class: Dept
异常信息说明:写错resultType=”Dept”
解决思路:
修改为resultType="User"
其他关联项目
- MyBatis 菜鸟教程 1 环境配置
http://blog.csdn.net/je_ge/article/details/53998874
如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。您的支持将鼓励我继续创作!谢谢!