本文章写于本人看完《Java EE企业级应用开发教程 Spring+SpringMVC+Mybatis》第二章"MyBatis的核心配置"
错误代码
最开始的错误
org.apache.ibatis.exceptions.PersistenceException:
###Error building SqlSession.
###The error may exist in com/itheima/mapper/StudentMapper.xml
###The error occurred while processing mapper_resultMap[studentMap]
###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 ‘com/itheima/mapper/StudentMapper.xml’. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias ‘com.itheima.domain.Student’. Cause: java.lang.ClassNotFoundException: Cannot find class: com.itheima.domain.Student
解决方法
一个个开始解决
第一步
经查找发现是StudentMapper.xml中的pojo写成domain了
< resultMap type=“com.itheima.pojo.Student” id=“studentMap”>
改正后报错变为
org.apache.ibatis.exceptions.PersistenceException:
###Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.itheima.mapper.StudentMapper.findAllStudent
###Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.itheima.mapper.StudentMapper.findAllStudent
第二步
检查IDEA有没有成功连接上mysql
连接上后报错依旧没有改变
Error querying database. Cause: java.sql.SQLException: Access denied for user ‘root’@‘localhost’ (using password: YES)
这次报错
Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.itheima.mapper.StudentMapper.findAllStudent
然后检查
查了很久,发现findAllStudent写错了多加了个s,改正过后,报错变为
org.apache.ibatis.exceptions.PersistenceException:
###Error querying database. Cause: java.sql.SQLSyntaxErrorException: Table ‘mybatis.t_student’ doesn’t exist
###The error may exist in com/itheima/mapper/StudentMapper.xml
###The error may involve defaultParameterMap
###The error occurred while setting parameters
###SQL: select * from t_student
###Cause: java.sql.SQLSyntaxErrorException: Table ‘mybatis.t_student’ doesn’t exist
第三步
检查后发现db.properties的数据库名没有改正,将如图的mybatis改为自己数据库的名字
至此,报错全部修改完毕
总结
我发现这次的mapper包没有放到src/main/resource下,所以mapper包不是固定位置的(应该是吧),只要在@test中指明路径就行
以下是两次目录结构的对比