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

目录

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

方式一:给字段名取别名

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

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

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

方式一:使用级联

方式二:使用标签

方式三:使用分步查询


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

方式一:给字段名取别名

● 如果表中字段名和实体类属性名不一致,可以在SQL语句中给字段名取别名
给字段取得别名必须和实体类属性名一致

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

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

● 使用方式:

1、在核心配置文件中使用<settings>标签,在该标签下使用<setting>子标签来配置

2、给子标签<setting>设置name属性值为mapUnderscoreToCamelCase,value属性值

为true

● 范例:

<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

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

这里多对一是指实体类中某个属性是以表中多个字段为属性构成的实体类,如员工类的部门属性,部门属性的类型是部门类,这个部门类有部门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>

注意:association标签中property属性是指映射实体类中属性的名称,javaType是它的类型,而association标签下的id标签和result标签中的property属性是指javaType指定的类中的属性名称,column属性指表中的字段名

方式三:使用分步查询

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

猜你喜欢

转载自blog.csdn.net/m0_61961937/article/details/127043137