美文网首页
字段名和属性名不一致的情况,处理映射关系

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

作者: 何以解君愁 | 来源:发表于2022-08-09 15:04 被阅读0次

字段名和属性名不一致的情况,如何处理映射关系
使用resultMap自定义映射

<!--    使用resultMap完成实体的属性名称和表字段名称的映射-->
<!--    id唯一标识,type处理映射关系的实体类的类型-->
    <resultMap id="brandResultMap" type="brand">
<!--    id设置主键与属性的映射关系,result设置普通字段与属性的映射关系
column:设置映射关系中的字段名,必须是sql查询出的某个字段,property:设置映射关系中的属性的属性名,必须是处理的实体类类型中的属性名 association:处理多对一映射关系
fetchType:在开启了延迟加载的环境中,通过该属性设置当前的分步查询是否使用延迟加载fetchType="eager(立即加载)/Lazy(延迟加载)“
-->
        <result property="brandName" column="brand_name" />
        <result property="companyName" column="company_name" />
    </resultMap>
<!--association:处理多对一的映射关系(处理实体类类型的属性)property:设置需要处理映射关系的属性的属性名
javaType:设置要处理的属性的类型-->

<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>
<!--Emp{empId=1,empName= '张三', age=20,gender='男', dept=Dept{deptId=1,deptName- 'A'}}
-->


<!--分布查询-->
<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.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptBystepTwo"
                 column="dept_id"></association>
< /resultMap>
public interface DeptMapper {
<!--
通过分步查询查询员工以及所对应的部i门信息的第二步-->
        Dept getEmpArFDeptBystepTwo(@Param( "deptId") Integer deptId);
}
xml:
<select id="getEmpAndDeptBystepTwo" resultType="Dept">
select * from t_dept where dept_id = #{ deptId}</select>

当字段符合MysQL的要求使用,而属性符合java的要求使用驼峰,此时可以在Mybatis核心配置文件中设置一个全局配置,可以自动将下划线映射为驼峰

<settings>
<!--将下划线映射为驼峰-->
    <setting name="mapunderscoreTocame1case" va1ue="true"/>
</ settings>

相关文章

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

    字段名和属性名不一致的情况,如何处理映射关系使用resultMap自定义映射 当字段符合MysQL的要求使用,而属...

  • MyBatis-ResultMap,日志工厂及分页

    当数据库中的字段名和实体类中的属性名不一致时,可以使用ResultMap结果集来映射。 1.数据库中的字段名 id...

  • Mybatis 相关

    一、#{}和${}的区别是什么? 理解: 二、怎么处理实体类中的属性名和表中的字段名不一致? 三、 模糊查询lik...

  • 关于resultType和resultMap的使用

    使用resultType进行输出映射,只有查询出来的字段名和pojo中的属性名一致,该列才可以映射成功。 如果查询...

  • 5、ResultMap

    要解决的问题:属性名和字段名不一致 查看之前的数据库的字段名 Java中的实体类设计public class Us...

  • 解决MyBatis属性名和字段名不一致的问题

    当属性名于字段名不一致时,查询出的属性会为null。 解决方式一:取别名 解决方式二:resultMap【重点】 ...

  • 懒汉处理dapper字段名与属性名的映射方式

    你还以为走路是世上最简单的事情呢?只不过是把一只脚放到另一只脚前面。但我一直很惊讶这些原本是本能的事情实际上做起来...

  • 单表的 CURD 操作(第二讲)

    属性名与查询字段名不行同 resultType 可以将查询结果直接映射为实体 Bean 对象的条件是, SQL 查...

  • Django ORM

    ORM 映射关系表名 <-------> 类名 字段 <-------> 属性 表记录 <------->类实...

  • 98、【JavaEE】【Mybatis】复杂映射

    1、概述 在映射配置文件中,查询语句标签 中,有resultType这一属性,如果实体的属性名与表中字段名一致,会...

网友评论

      本文标题:字段名和属性名不一致的情况,处理映射关系

      本文链接:https://www.haomeiwen.com/subject/ykvrwrtx.html