Mybatis中resultMap级联属性赋值解决多对一映射问题

resultMap级联属性赋值解决多对一映射问题

实体类中除了有有参构造器外,还要再加一个无参构造器,要不然就会一直报错。

实体类

① Emp.java(get,set,toString还有构造器略)

//员工表
public class Emp {
   private Integer eid;
   private String empName;
   private Integer age;
   private String sex;
   private String email;
   private Dept dept;

② Dept.java(get,set,toString还有构造器略)

//部门表
public class Dept {
    private Integer did;
    private String deptName;
mapper接口和mapper.xml
/**
 * 查询员工以及员工所对应的部门信息
 */
Emp getEmpAndDept(@Param("eid") Integer eid);

<resultMap id="empAndDeptResultMapOne" type="com.jxxy.mybatis.pojo.Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <result property="dept.did" column="did"></result>
        <result property="dept.deptName" column="dept_name"></result>
    </resultMap>
    
<!--  Emp getEmpAndDept(@Param("eid") Integer eid);  -->
<select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">
    select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid}
</select>
数据库数据

在这里插入图片描述在这里插入图片描述

测试

sql在数据库中查询,可以看到没得问题
在这里插入图片描述
用代码测试:

@Test
public void testGetEmpAndDept(){
    SqlSession sqlSession= SqlSessionUtils.getSqlSession();
    EmpMapper mapper=sqlSession.getMapper(EmpMapper.class);
    Emp emp=mapper.getEmpAndDept(2);
    emp.getDept().getDeptName();
    System.out.println(emp);
}

居然报错了!!!!

### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: Cannot set value of property 'dept.did' because 'dept.did' is null and cannot be instantiated on instance of com.jxxy.mybatis.pojo.Dept. Cause:org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.jxxy.mybatis.pojo.Dept with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.jxxy.mybatis.pojo.Dept.<init>()
### The error may exist in com/jxxy/mybatis/mapper/EmpMapper.xml
### The error may involve com.jxxy.mybatis.mapper.EmpMapper.getEmpAndDept
### The error occurred while handling results
### SQL: select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = ?
### Cause: org.apache.ibatis.reflection.ReflectionException: Cannot set value of property 'dept.did' because 'dept.did' is null and cannot be instantiated on instance of com.jxxy.mybatis.pojo.Dept. Cause:org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.jxxy.mybatis.pojo.Dept with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.jxxy.mybatis.pojo.Dept.<init>()
后边的省略... ...

查资料发现,要在实体类中加无参构造器,说干就干,加无参构造器后,测试通过辣!!!

Emp{eid=2, empName='李四', age=33, sex='女', email='12@12.com', dept=Dept{did=2, deptName='B', empList=null}}
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 17
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

StarKNG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值