MyBatis ResultMap Assocation 返回属性为null的问题
2018-09-30 16:39
429 查看
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='开发部'}}
在学习过程中碰到了这个问题,暂时还没有找到是什么原因引起的。望各位高手帮忙看看
相关文章推荐
- 注解开发mybatis的mapper属性和字段不对应问题@Results—resultMap
- mybatis返回对象为null或者0的问题
- 解决mybatis使用char类型字段查询oracle数据库时结果返回null问题
- [小技巧]关于spring boot 整合mybatis-plus: value为null时不返回的问题。
- mybatis sum函数返回为null的解决方式,格式化小数千分位的问题
- 解决mybatis执行SQL语句部分参数返回NULL问题
- 关于onActivtyResult方法的data形参返回null或未被调用的问题
- mybatis-plus timestamp返回为null问题排除
- mybatis查询,返回类型为resultType,某些属性值为null
- 关于相机拍照获取图片onActivityResult返回data 为null的问题
- mybatis查询,如果返回类型为resultType时,查询不到某些属性值问题
- Mybatis查不到数据查询返回Null问题
- MyBatis解决字段名和实体类属性名不相同冲突问题时返回查询结果为空
- 解决 mybatis 使用sum 返回null 的 问题
- mybatis遇见的奇葩问题(返回null)
- mybatis处理查询map列表属性为null的问题,而导致查询map无该key对象
- Android 相机拍照后 ,Intent 返回 null问题 的 l对应方法
- Json-simple解析Json文件返回null的问题解决
- mybatis返回类型resulttype之string和java.lang.string的不同点
- findViewById返回null的问题解决