是JF啊 2020年10月16日
Mybatis问题萌新求助大佬们
这是报的错误
Exception in thread "main" java.lang.ClassCastException: 
class com.entity.User cannot be cast to class com.entity.Car
(com.entity.User and com.entity.Car are in unnamed module of loader 'app')
at UserMapperTest.main(UserMapperTest.java:30)


这是接口
public interface CarMapper {
List<Car> getCarUsers();
}


这是接口的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.dao.CarMapper">
<select id="getCarUsers" resultMap="UserCar">
SELECT u.name,u.sex,u.email,u.info,c.uid ,c.carmodel,c.address
FROM user u,`car` c
WHERE u.id=c.uid
</select>

<resultMap id="UserCar" type="com.entity.User">
<result property="id" column="id"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<result property="info" column="info"/>
<collection property="cars" ofType="com.entity.Car">
<result property="id" column="carid"/>
<result property="carmodel" column="carmodel"/>
<result property="address" column="address"/>
</collection>
</resultMap>

</mapper>


这是测试类

public class UserMapperTest {
private Logger logger = Logger.getLogger(UserMapperTest.class);

public static void main(String[] args) {
String resource="mybatis-config.xml";
SqlSession sqlSession = MybatisUtils.getSqlSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
List<Car> carList = mapper.getCarUsers();
for (Car car : carList) {
System.out.println(car);
}
}
}


求助一下各位大佬,这是什么错,原理是什么,该如何修改,谢谢各位大佬


...全文
1028 点赞 收藏 8
写回复
8 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Java SE
创建于2007-09-28

3.4w+

社区成员

30.7w+

社区内容

Java 2 Standard Edition
社区公告
暂无公告