Mybatis05_自定义映射ResultMap
-
一对一,字段名和属性名不一致
-
将查询结果的字段名设置别名对应上属性名
<select id="getEmpById" resultType="Emp"> select emp_id empId,emp_name empName,age,gender from emp where emp_id = #{emp_id} </select>
-
在mybatis核心配置文件中的setting设置
但是要保证字段名符合mysql的规则使用下划线,属性名符合java规则使用驼峰
<settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> select * from emp where emp_id = #{emp_id}
-
使用自定义映射
resulType改为设置resultMap的id
- id对应字段主键,result对应普通字段
- column对应字段名,property对应属性名
<resultMap id="EmpResultMap" type="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> </resultMap> <!-- Emp getEmpById(@Param("emp_id") int emp_id);--> <select id="getEmpById" resultMap="EmpResultMap"> select * from emp where emp_id = #{emp_id} </select>
-
-
一对多关系映射
-
使用级联方式
<select id="getEmpAndDeptById" resultMap="empAndDeptResultMap"> select * from emp left join dept on emp.dept_id = dept.dept_id where emp_id = #{empId} </select> <resultMap id="empAndDeptResultMap" type="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> <result column="dept_id" property="dept.deptId"></result> <result column="dept_name" property="dept.deptName"></result> </resultMap>
-
使用association
<select id="getEmpAndDeptById" resultMap="empAndDeptResultMapAsso"> select * from emp left join dept on emp.dept_id = dept.dept_id where emp_id = #{empId} </select> <resultMap id="empAndDeptResultMapAsso" type="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> <!-- association:处理多对一映射,即实体类属性 property:处理的实体类属性,javaType:属性的类型 column字段名property实体类属性的成员属性名--> <association property="dept" javaType="Dept"> <id column="dept_id" property="deptId"></id> <result column="dept_name" property="deptName"></result> </association> </resultMap>
-
使用分步查询
<select id="getEmpandDeptByStepOne" resultMap="empAndDeptByStepResultMap"> select * from emp where emp_id = #{empId} </select> <resultMap id="empAndDeptByStepResultMap" type="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> <!-- property映射的实体类属性 select下一步查询的sql唯一标识 column下一步查询的sql的查询条件--> <association property="dept" select="com.canyan7n.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="dept_id"> </association> </resultMap> <select id="getEmpAndDeptByStepTwo" resultType="Dept"> select * from dept where dept_id = #{deptId} </select>
-
-
延迟加载
-
延迟加载,默认值为false
<setting name="lazyLoadingEnabled" value="true"/>
-
按需加载,默认值为false
<setting name="aggressiveLazyLoading" value="true"/>
-
-
一对多映射
-
collection
<!-- Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);--> <select id="getDeptAndEmpByDeptId" resultMap="DeptAndEmpResultMap"> select * from dept left join emp on dept.dept_id = emp.dept_id where dept.dept_id = #{deptId} </select> <!-- collection--> <resultMap id="DeptAndEmpResultMap" type="Dept"> <id column="dept_id" property="deptId"></id> <result column="dept_name" property="deptName"></result> <collection property="emps" ofType="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="genger" property="gender"></result> </collection> </resultMap>
-
分步
<!-- Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);--> <select id="getDeptAndEmpByStepOne" resultMap="DeptAndEmpByStepResultMap"> select * from dept where dept_id = #{deptId} </select> <!-- 分步--> <resultMap id="DeptAndEmpByStepResultMap" type="Dept"> <id column="dept_id" property="deptId"></id> <result column="dept_name" property="deptName"></result> <association property="emps" select="com.canyan7n.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo" column="dept_id" fetchType="eager"> </association> </resultMap>
-