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

简介: 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>
目录
相关文章
|
1月前
|
XML Oracle Java
mybatis反向生成实体类、dao层以及映射文件
mybatis反向生成实体类、dao层以及映射文件
14 1
|
1月前
|
SQL Java 数据库连接
|
2月前
|
Java 数据库连接 Maven
使用mybatis插件generator生成实体类,dao层和mapper映射
使用mybatis插件generator生成实体类,dao层和mapper映射
52 0
|
19天前
|
SQL XML Java
【mybatis】第二篇:@Select注解中加入字段判断
【mybatis】第二篇:@Select注解中加入字段判断
|
1月前
|
XML Java 数据库连接
【MyBatis】1、MyBatis 核心配置文件、多表查询、实体映射文件 ......
【MyBatis】1、MyBatis 核心配置文件、多表查询、实体映射文件 ......
52 0
|
1天前
|
SQL Java 数据库连接
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
|
30天前
|
Java 数据库连接 mybatis
mybatis plus字段为null或空字符串把原来的数据也更新了,只需要注解
mybatis plus字段为null或空字符串把原来的数据也更新了,只需要注解
22 0
|
1月前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
15 1
|
1月前
ssm(Spring+Spring mvc+mybatis)实体类——Dept
ssm(Spring+Spring mvc+mybatis)实体类——Dept
13 0
|
1月前
|
XML Java 数据库连接
【Mybatis】XML映射文件
【Mybatis】XML映射文件
27 0