MyBatis查询时数据表字段名与实体类属性名不一致

解决使用MyBatis查询时数据表字段名与实体类属性名不一致的问题
使用数据表:

 创建对应实体类(此时员工类中属性名empName与员工表中字段名emp_name不一致):

package com.zyf.pojo;

// 员工类
public class Emp {

    private Integer eid;
    private String empName;
    private Integer age;
    private String sex;
    private String email;
    
    public Emp(){}
    public Emp(Integer eid, String empName, Integer age, String sex, String email) {
        this.eid = eid;
        this.empName = empName;
        this.age = age;
        this.sex = sex;
        this.email = email;
    }
    
    public Integer getEid() {
        return eid;
    }
    public void setEid(Integer eid) {
        this.eid = eid;
    }
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    
    @Override
    public String toString() {
        return "Emp{" +
                "eid=" + eid +
                ", empName='" + empName + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

当查询字段名与对应实体类属性名不一致怎么办?

首先,我们来看看,当查询字段名与对应实体类属性名不一致时会发生什么。

package com.zyf.mapper;

public interface EmpMapper {

    /**
     * 查询所有的员工信息
     */
    List<Emp> getAllEmp();
}
<?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="com.zyf.mapper.EmpMapper">

    <select id="getAllEmp" resultType="Emp">
        select * from t_emp;
    </select>
</mapper>

测试代码:

    @Test
    public void testGetAllEmp(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> empList = empMapper.getAllEmp();
        empList.forEach(emp -> System.out.println(emp));
    }

运行结果:

 我们发现程序并没有报错,而是查询字段值为空,因为此时属性名与字段名不一致导致映射时无法找到对应字段,程序就认为该值为null。

针对此类问题我们有三种解决方式:SQL语句设置别名、添加全局配置、使用resultMap自定义映射

(1)SQL语句设置别名
这种方法是给SQL语句中所查询的字段名设置一个别名,使它与实体类中的属性名保持一致(不常用)

<?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="com.zyf.mapper.EmpMapper">

    <select id="getAllEmp" resultType="Emp">
        select eid,emp_name as empName,age,sex,email from t_emp;
    </select>
</mapper>

(2)添加全局配置
在MyBatis核心配置文件中使用setting标签,将_自动映射为驼峰
注意:该方法只适用于命名高度规范的情况,即数据表字段名使用( _ )下划线连接

<settings>
        <!-- 将_自动映射为驼峰,emp_name:empName -->
        <setting name="mapUnderscoreToCamelCase" value="true"></setting>
</settings>

(3)使用resultMap自定义映射
使用resultMap为字段名与属性名自定义映射关系

<!--
	resultMap标签:设置自定义映射
	属性:
		id属性:表示自定义映射的唯一标识
		type属性:查询的数据要映射的实体类的类型
	子标签:
		id标签:设置主键的映射关系
		result标签:设置普通字段的映射关系
		association标签:设置多对一的映射关系
		collection标签:设置一对多的映射关系
		属性:
			property属性:设置映射关系中实体类中的属性名
			column属性:设置映射关系中表中的字段名
-->
<resultMap id="empResultMap" 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>
</resultMap>
<!-- List<Emp> getAllEmp(); -->
<select id="getAllEmp" resultMap="empResultMap">
    select * from t_emp;
</select>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值