org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may

跟着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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值