0
点赞
收藏
分享

微信扫一扫

MyBatis处理表字段和实体类属性名不一致的情况及多对一映射关系的处理

德州spark 2022-09-26 阅读 163

目录

一、处理表字段和实体类属性名不一致的情况

方式一:给字段名取别名

方式二:在核心配置文件中配置驼峰映射

方式三:在映射文件中使用标签自定义映射

二、多对一映射关系的处理

方式一:使用级联

方式二:使用标签

方式三:使用分步查询


一、处理表字段和实体类属性名不一致的情况

方式一:给字段名取别名

方式二:在核心配置文件中配置驼峰映射

● 使用前提:表字段符合Mysql命名规范(使用下划线_分割单词),而实体类属性符合驼峰命
名规范

● 使用方式:

● 范例:

<settings>
    <setting name="mapUnderscoreToCamelCase" value="true" />
</settings>

● 在核心配置文件使用了如上配置后,在SQL语句中可以使用表的字段名而不用考虑表字段名和
实体类属性名不一致的情况

方式三:在映射文件中使用<resultMap>标签自定义映射

● <resultMap>标签含有id属性和type属性,其中id属性是设置当前自定义映射的标识,type属
性是映射的实体类

● <resultMap>标签下含有的子标签以及功能

1、<id>标签:设置主键字段的映射关系,使用column属性设置映射关系中表的字段名,
使用property属性设置映射关系中实体类的属性名

2、<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>

<select id="getEmpByEmpId" resultType="Emp"
resultMap="empResultMap">
    select * from t_emp where emp_id = #{empId}
</select>

注意:SQL语句所在标签中的resultMap属性值必须是自定义映射的id

二、多对一映射关系的处理

方式一:使用级联

<resultMap>配置:

<resultMap id="getEmpAndDeptByEmpIdResultMap" 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>

<select id="getEmpAndDeptByEmpId"
resultMap="getEmpAndDeptByEmpIdResultMap">
    select emp_id,emp_name,age,gender,t_dept.dept_id,dept_name
    from t_emp left join t_dept
    on t_emp.dept_id = t_dept.dept_id where emp_id = #{empId}
</select>

方式二:使用<association>标签

<resultMap>配置:

<resultMap id="getEmpAndDeptByEmpIdResultMap" 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="dept" javaType="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
</association>
</resultMap>
<select id="getEmpAndDeptByEmpId"
resultMap="getEmpAndDeptByEmpIdResultMap">
        select emp_id,emp_name,age,gender,t_dept.dept_id,dept_name from t_emp left join t_dept
        on t_emp.dept_id = t_dept.dept_id where emp_id = #{empId}
</select>

方式三:使用分步查询

<resultMap>配置:

查询员工信息:

<resultMap id="getEmpAndDeptByEmpIdResultMap" 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="dept"
select="com.liaoxiangqian.mapper.DeptMapper.getDeptByDeptId"
            column="dept_id">
    </association>
</resultMap>
<select id="getEmpAndDeptByEmpId"
resultMap="getEmpAndDeptByEmpIdResultMap">
    select * from t_emp where emp_id = #{empId}
</select>

根据员工的部门id查询部门信息:

<resultMap id="getDeptByDeptIdResultMap" type="Dept">
    <id column="dept_id" property="deptId"></id>
    <result column="dept_name" property="deptName"></result>
</resultMap>

<select id="getDeptByDeptId" resultMap="getDeptByDeptIdResultMap">
    select * from t_dept where dept_id = #{deptId}
</select>
举报

相关推荐

0 条评论