日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

MyBatis查詢時數據表字段名與實體類屬性名不一致

作者:LiZhen798 更新時間: 2022-07-16 編程語言

解決使用MyBatis查詢時數據表字段名與實體類屬性名不一致的問題
使用數據表:

?創(chuàng)建對應實體類(此時員工類中屬性名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 + '\'' +
                '}';
    }
}

當查詢字段名與對應實體類屬性名不一致怎么辦?

首先,我們來看看,當查詢字段名與對應實體類屬性名不一致時會發(fā)生什么。

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));
    }

運行結果:

?我們發(fā)現程序并沒有報錯,而是查詢字段值為空,因為此時屬性名與字段名不一致導致映射時無法找到對應字段,程序就認為該值為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標簽,將_自動映射為駝峰
注意:該方法只適用于命名高度規(guī)范的情況,即數據表字段名使用( _ )下劃線連接

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

原文鏈接:https://blog.csdn.net/LiZhen314/article/details/125810868

欄目分類
最近更新