mybatis中解决属性名和字段名不一致的方法

 1、在sql查询语句中,给字段起别名保持和实体类中的属性名一致

<select id="getAllEmpOld" resultType="Emp">
        select eid,emp_name empName,age,sex,email from t_emp
</select>
2、设置全局配置,将_下划线自动映射为驼峰(在mybatis-config.xml中进行配置)
设置mybatis中的全局配置
    <settings>
        <!--mapUnderscoreToCamelCase:将_自动映射为驼峰:emp_name -> empName-->
        <!--这样就可以解决字段名和实体类属性名字不一致的问题-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

3、使用resultMap:设置自定义映射关系

<resultMap id="resultEmpMap" type="Emp">
        <!--
            resultMap:设置自定义映射关系
            type:设置映射关系中的实体类类型
            id 设置主键的映射关系  result普通字段的映射关系
            property:type设置中的实体类中属性的名字
            column:sql查询出来的字段名字
        -->
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
</resultMap>

    <!--List<Emp> getAllEmp();-->
    <select id="getAllEmp" resultMap="resultEmpMap">
        select * from t_emp
    </select>

补充:解决多对一映射关系的三种方式

1、通过级联赋值的方式

<resultMap id="empAndDeptResultMapOne" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <result property="dept.did" column="did"></result>
        <result property="dept.deptName" column="did_name"></result>
</resultMap>
<select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">
        select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid}
    </select>

2、通过association

<resultMap id="empAndDeptResultMapTwo" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <association property="dept" javaType="Dept">
            <!--先把查询出来的did dept_name和 javaType中设置的Dept类型进行映射
                然后将映射得到的Dept赋值给Emp中Dept属dept
            -->
            <id property="did" column="did"></id>
            <result property="deptName" column="dept_name"></result>
        </association>
    </resultMap>
    <select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">
        select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid}
    </select>

3、通过分步查询(常用方式);

优点:可以实现延迟加载,但是必须在核心配置文件中开启lazyLoadingEnabled(延迟加载的全局开关,默认关闭false)设置全局配置信息,同时需要将aggressiveLazyLoading关闭(默认为false关闭,版本<=3.4.1默认为true开启);

EmpMapper

/**
     * 通过分布查询员工以及员工所对应的部门信息
     */
    /**
     * 分布查询第一步查询员工信息
     * @return
     */
    Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);

EmpMapper.xml

<resultMap id="empAndDeptByStepResultMap" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <!--设置分步查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名
            column:设置分步查询的条件
            (第二步骤是根据第一步得到的员工信息中的did进行查询,所以这里的分布查询条件就是did
             然后第二步骤就根据第一步骤得到的did根据select中设置的唯一标识,找到对应的select语句
             然后进行相应的查询,得到部门信息)
        -->
        <association property="dept"
                     select="whut.zyf.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did">
        </association>
    </resultMap>

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

DeptMapper

/**
     * 通过分步查询查询员工以及员工所对应的部门信息
     * 分布查询第二步:通过did查询员工所对应的部门信息
     */
    Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);

DeptMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="whut.zyf.mybatis.mapper.DeptMapper">
    <!--Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);-->
    <!--因为在mybatis-config配置文件中已经设置了全局配置讲下划线转化为驼峰
        所以可以根据查询到的did 和 dept_name赋值给resultType中设置的Dept类型中对应的值
    -->
    <select id="getEmpAndDeptByStepTwo" resultType="Dept">
        select * from t_dept where did = #{did}
    </select>
</mapper>

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小馨java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值