zoukankan      html  css  js  c++  java
  • MyBatis ResultMap Assocation 返回属性为null的问题

    Model:

    public class Employee {
        private Integer id;
        private String lastName;
        private String email;
        private String gender;
        private Department dept;
    }
    
    public class Department {
        private Integer id;
        private String departmentName;
    }
    

      

    不使用Assocation的方式,可以正常返回数据,没有关联的属性email和gender也可以绑定上:

        <resultMap id="myEmpPlus" type="com.roy.simple.model.Employee">
            <id column="id" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="d_id" property="dept.id"/>
            <result column="dept_name" property="dept.departmentName"/>
        </resultMap>
    
        <select id="getEmpAndDeptById" resultMap="myEmpPlus">
            select e.*, d.id d_id, d.dept_name from tbl_employee e
            inner join tbl_dept d on e.dept_id = d.id
            where e.id = #{id}
        </select>

    输出:

    Employee{id=3, lastName='roy', email='adfa@asdf', gender='1', dept=Department{id=1, departmentName='开发部'}}

    使用Assocation,如果不显示指定列的关联,gender和emai为null

        <resultMap id="myEmpPlus2" type="com.roy.simple.model.Employee">
            <id column="id" property="id"/>
            <result column="last_name" property="lastName"/>
            <!--<result column="gender" property="gender"/>-->
            <!--<result column="email" property="email"/>-->
            <!--可以指定联合的javabean的对象
                property:指定哪个属性是联合的对象
                javaType:指定对象的类型【不能省略】
            -->
            <association property="dept"javaType="com.roy.simple.model.Department">
                <id column="d_id" property="id"/>
                <result column="dept_name" property="departmentName"/>
            </association>
        </resultMap>

    输出:

    Employee{id=3, lastName='roy', email='null', gender='null', dept=Department{id=1, departmentName='开发部'}}

     在学习过程中碰到了这个问题,暂时还没有找到是什么原因引起的。望各位高手帮忙看看

  • 相关阅读:
    牛客挑战赛48C铬合金之声【Prufer序列】
    Java 基础 反射
    MDX Query mdx的基本语法和概念
    Maven 深入理解maven构建生命周期和各种plugin插件
    Java基础 String,StringBuilder,StringBuffer三者的区别
    Java基础 Java 抽象类 抽象方法
    Java 基础 final vs static
    JMX JMX(Java Management Extensions)定义
    Java 多线程 生产者消费者问题
    Java 基础 如何重写equals()
  • 原文地址:https://www.cnblogs.com/icebutterfly/p/9732436.html
Copyright © 2011-2022 走看看