首页 > 其他分享 >字段名和属性名不一致的情况,如何处理映射关系?

字段名和属性名不一致的情况,如何处理映射关系?

时间:2022-10-03 22:56:25浏览次数:44  
标签:empId 映射 did dept emp id 字段名 属性

注意:使用1和2方式的前提是字段名和实体类中的属性名不一致,但是字段名符合数据库的规则(使用_),实体类中的属性 名符合Java的规则(使用驼峰)。

1、可以通过为字段起别名的方式,保证和实体类中的属性名保持一致 。

/**
     * 根据id查询员工信息
     * @param empId
     * @return
     */
    Emp getEmpByEmpId(@Param("empId") Integer empId);
<!--    Emp getEmpByEmpId(@Param("empId") Integer empId)-->
    <select id="getEmpByEmpIdOld" resultType="Emp">
      select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId}
    </select>

2、可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,可以在查询表中数据时,自动将_类型的字段名转换为驼峰 。

<!--    settings设置Mybatis所有的全局配置-->
    <settings>
        <!--将下划线映射为驼峰-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

3、使用resultMap自定义映射

resultMap处理字段和属性的映射关系,若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射。

  • 属性:
    •    id:表示自定义映射的唯一标识
    •    type:查询的数据要映射的实体类的类型
  • 子标签:
    •    id:设置主键的映射关系
    •    result:设置普通字段的映射关系
<!--    使用ResultMap设置自定义映射处理-->
    <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" resultMap="empResultMap">
         select * from t_emp where emp_id = #{empId}
    </select>

3.1、多对一映射处理

场景:有员工表和部门表,查询员工信息以及员工所对应的部门信息
/**
     * 获取员工以及所对应的部门信息
     * @param empId
     * @return
     */
    Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);
<!--    Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId)-->
    <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
        select *
        from t_emp
        left join t_dept
        on t_emp.dept_id = t_dept.dept_id
        where t_emp.emp_id = #{empId}
    </select>

3.1.1、级联方式

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

3.1.2、使用association

   association:设置多对一的映射关系    属性:    property:设置映射关系中实体类中的属性名    column:设置映射关系中表中的字段名
<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>
        <association property="dept" javaType="Dept">
            <id column="dept_id" property="deptId"></id>
            <result column="dept_name" property="deptName"></result>
        </association>
    </resultMap>

 3.1.3、分布查询

/**
 * 第一步:查询查询员工信息
 * @param eid
 * @return 
 */

Emp getEmpByStep(@Param("eid") int eid);

/**
 * 第二步: 根据员工所对应的did查询部门信息
 * @param did
 * @return 
 */

Dept getEmpDeptByStep(@Param("did") int did);
<resultMap id="empDeptStepMap" type="Emp">
    <id column="eid" property="eid"></id>
    <result column="ename" property="ename"></result>
    <result column="age" property="age"></result>
    <result column="sex" property="sex"></result>
    <!--
        select:设置分步查询,查询某个属性的值的sql的标识(namespace.sqlId) 
        column:将sql以及查询结果中的某个字段设置为分步查询的条件
    -->
    <association property="dept" 
select="com.ssm.mybatis.mapper.DeptMapper.getEmpDeptByStep" column="did"> 
</association>
</resultMap>

<!--Emp getEmpByStep(@Param("eid") int eid);-->
<select id="getEmpByStep" resultMap="empDeptStepMap">
 select * from t_emp where eid = #{eid}
</select>

<!--Dept getEmpDeptByStep(@Param("did") int did);-->
<select id="getEmpDeptByStep" resultType="Dept">
 select * from t_dept where did = #{did}
</select>

 3.2、一对多映射处理

3.2.1、collection

/**
 * 根据部门id查新部门以及部门中的员工信息
 * @param did
 * @return 
 */

Dept getDeptEmpByDid(@Param("did") int did);
<resultMap id="deptEmpMap" type="Dept">
    <id property="did" column="did"></id>
    <result property="dname" column="dname"></result>
    <!--ofType:设置collection标签所处理的集合属性中存储数据的类型-->
    <collection property="emps" ofType="Emp">
        <id property="eid" column="eid"></id>
        <result property="ename" column="ename"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
    </collection>
</resultMap>

<!--Dept getDeptEmpByDid(@Param("did") int did);-->
<select id="getDeptEmpByDid" resultMap="deptEmpMap">
   select dept.*,emp.* from t_dept dept left join t_emp emp on dept.did = emp.did where dept.did = #{did}
</select>

3.2.2、分步查询

/**
 * 第一步:查询部门信息
 * @param did
 * @return 
 */

Dept getDeptByStep(@Param("did") int did);

/**
 * 第二步:根据部门id查询员工信息
 * @param did
 * @return
 */

List<Emp> getEmpListByDid(@Param("did") int did);
<resultMap id="deptEmpStep" type="Dept">
 <id property="did" column="did"></id>
 <result property="dname" column="dname"></result>
 <collection property="emps" fetchType="eager"
select="com.ssm.mybatis.mapper.EmpMapper.getEmpListByDid" column="did">
    </collection>
</resultMap>

<!--Dept getDeptByStep(@Param("did") int did);-->
<select id="getDeptByStep" resultMap="deptEmpStep">
 select * from t_dept where did = #{did}
</select>

<!--List<Emp> getEmpListByDid(@Param("did") int did);-->
<select id="getEmpListByDid" resultType="Emp">
 select * from t_emp where did = #{did}
</select>

 3.2.3、分布查询的优点

可以实现延迟加载 但是必须在核心配置文件中设置全局配置信息: lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载 aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属 性会按需加载 此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。 此时可通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载, fetchType="lazy(延迟加载) | eager(立即加载)"。

标签:empId,映射,did,dept,emp,id,字段名,属性
From: https://www.cnblogs.com/didadida-wang/p/16751505.html

相关文章

  • 008.beanScope的属性
                   ......
  • 第一季:8spring支持的常用数据库事务传播属性和事务隔离级别【Java面试题】
    第一季:8spring支持的常用数据库事务传播属性和事务隔离级别【Java面试题】​​前言​​​​推荐​​​​第一季:8spring支持的常用数据库事务传播属性和事务隔离级别​​​​......
  • Vue2 自定义属性
    概述vue中不不仅仅有官方提供的指令,用户还可以根据自己的需要进行自定义指令。比如当我们需要一个常用的操作将文字改为蓝色,如果我们需要修改大量标签时,就可以使用自定......
  • Vue2 计算属性
    概述计算属性指的是通过一系列运算之后,最终得到一个属性值。间的的理解,当我们拥有一些数据时,我们需要将这些数据整合到一起,这时候计算属性就会完成这个操作,整合到一起的......
  • CSS 中可继承的属性有哪些
    继承就是指子节点默认使用父节点的样式属性。可以继承的属性很少,只有颜色,文字,字体间距行高对齐方式,和列表的样式可以继承。所有元素可继承:visibility和cursor。内联元......
  • SQLMap入门——获取表中的字段名
    查询表名之后,查询表中的字段名pythonsqlmap.py-uhttp://localhost/sqli-labs-master/Less-1/?id=1-Dxssplatform-Toc_user--columns 后续注入--columns缩写成-......
  • vertical-align 属性应用
    vertical-align属性应用CSS的vertical-align属性使用场景:经常用于设置图片或表单(行内块元素)和文字垂直对齐。官方解释:用于设置一个元素的垂直对齐方式,但是它只针对行......
  • 【Linux】学习-03-文件属性查看及修改
       以上命令中,最左边的一列,第一个字母【d】表示文件类型为【目录文件】,之后的字母,三个字母为一组,其意义如下图所示:第一个字母如果是【l】表示该文件类型为【连接文......
  • 弹性布局(display:flex;)属性详解
    https://www.cnblogs.com/hellocd/p/10443237.html Flexbox 是flexiblebox的简称(注:意思是“灵活的盒子容器”),是 CSS3 引入的新的布局模式。它决定了元素如何在......
  • background属性 css背景设置
    CSS中的背景样式我们都会设置,但是总会遇到一些比较陌生的属性,或不知道背景样式到底有多少个属性,下面就让我们一探究竟吧!!一、背景属性大全属性描述background在......