跟着mybatis官方文档再次学习mybatis,果然,还是不记得一些配置了…
同时,官方文档也是坑小白哦
官方文档中文 http://www.mybatis.org/mybatis-3/zh/getting-started.html
在使用xml配置文件进行sqlsession获取的时候出现以下报错
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/wy/bean/StudentMapper.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 'Student'.
Cause: java.lang.ClassNotFoundException: Cannot find class: Student
看报错提示很明显知道是Student这个类找不到,同时提示了可能发生错误的地方是
The error may exist in com/wy/bean/StudentMapper.xml
以下是我的mapper文件
<?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.wy.bean.StudentMapper">
<!-- 查找所有学生 -->
<select id="selectAllStudent" resultType="Student" >
select * from student
</select>
</mapper>
官方的mapper配置文件
<?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="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
仔细看resultType那块!直接是一个类名,然而在mybatis-config.xml中也没有配置类别名的相关配置,SO,被文档坑了…
解决方案
在mybatis-config.xml没有配置类别名的时候需要写上类的全路径
<?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.wy.bean.StudentMapper">
<!-- 查找所有学生 -->
<!-- 注意:在mybatis-config.xml没有配置类别名的时候需要写上类的全路径 -->
<select id="selectAllStudent" resultType="com.wy.bean.Student" >
select * from student
</select>
</mapper>