網站首頁 編程語言 正文
解決使用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
相關推薦
- 2023-03-22 教你利用Golang可選參數實現可選模式_Golang
- 2022-04-11 git項目初次push提示error: failed to push some refs to ht
- 2022-09-12 Shell編程之/bin/bash和/bin/sh的區(qū)別淺析_linux shell
- 2022-09-15 利用Anaconda創(chuàng)建虛擬環(huán)境的全過程_python
- 2022-05-11 如何在 CSS 中設置組件在瀏覽器屏幕水平垂直居中
- 2022-07-28 ?python中的元類metaclass詳情_python
- 2022-05-19 C++線程之thread詳解_C 語言
- 2022-09-04 React18中請求數據的官方姿勢適用其他框架_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支