mybatis对数据库一对多操作报错

org.apache.ibatis.exceptions.PersistenceException:

Error building SqlSession.

The error may exist in OneToManyMapper.xml

The error occurred while processing mapper_resultMap[oneToMany]

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 'OneToManyMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'classes'. Cause: java.lang.ClassNotFoundException: Cannot find class: classes

代码如图

img

img

type="classes"
换成全限定类名。

sql哪有你这样写的呀?很明显sql写错了,你应该用表链接呀,因为一对多或者多对一都不是一个表的查询,而是两个表之间存在关联关系,所以你应该使用两表链接查询,推荐左连接查询
分析一下:一对多,一个查询多个,所以一为主表,即左表,多为右表,也就是查询班级的顺便同时将学生查询出来

select c.id cid,c.name cname,s.id sid,s.name sname,s.age sage from classes c left join student s on c.id=s.cid

报错信息中就有的:Could not resolve type alias 'classes'. Cause: java.lang.ClassNotFoundException: Cannot find class: classes;
mapper.xml中的resultMap写的 type: classes这个有问题;程序不能解析这个classes;
解决方式有两种:
1.type这个中写的是classes这个类的全限定路径:包名.类名;比如这个类是在com.xx.xx.classes;
2.mybatis中可以配置alias--别名;具体使用方法可以查看mybaits的官网文档