MyBatis 字段映射的解決方案
引言
在使用 MyBatis 進(jìn)行數(shù)據(jù)庫(kù)操作時(shí),數(shù)據(jù)庫(kù)字段名(如下劃線命名 delete_flag)與 Java 實(shí)體類屬性名(駝峰命名 deleteFlag)之間的映射是一個(gè)常見(jiàn)問(wèn)題。本文將介紹三種解決方案:手動(dòng) @Results 映射、可復(fù)用的 @Results(id) 映射以及全局駝峰命名配置。
首先,我們定義一個(gè)實(shí)體類UserInfo,后續(xù)所有示例都基于這個(gè)類:
public class UserInfo {
private Integer id;
private String username;
private String password;
private Integer age;
private String gender;
private String phone;
private Integer deleteFlag; // 數(shù)據(jù)庫(kù)字段 delete_flag
private Date createTime; // 數(shù)據(jù)庫(kù)字段 create_time
private Date updateTime; // 數(shù)據(jù)庫(kù)字段 update_time
// getter / setter 省略
}手動(dòng) @Results 映射
最直接的方式是在每個(gè)查詢方法上使用 @Results 注解,手動(dòng)指定字段映射關(guān)系。
@Mapper
public interface UserInfoMapper {
@Results({
@Result(column = "delete_flag", property = "deleteFlag"),
@Result(column = "create_time", property = "createTime"),
@Result(column = "update_time", property = "updateTime")
})
@Select("select * from user_info")
List<UserInfo> selectAll();
// 另一個(gè)查詢方法,需要重復(fù)寫(xiě)一遍 @Results
@Results({
@Result(column = "delete_flag", property = "deleteFlag"),
@Result(column = "create_time", property = "createTime"),
@Result(column = "update_time", property = "updateTime")
})
@Select("select * from user_info where id = #{id}")
UserInfo selectById(Integer id);
}可復(fù)用的 @Results(id) 映射
為了解決重復(fù)代碼問(wèn)題,MyBatis 允許為@Results 注解指定一個(gè) id,其他方法可以通過(guò) @ResultMap 引用該映射。
@Mapper
public interface UserInfoMapper {
@Results(id = "BaseMap", value = {
@Result(column = "delete_flag", property = "deleteFlag"),
@Result(column = "create_time", property = "createTime"),
@Result(column = "update_time", property = "updateTime")
})
@Select("select * from user_info")
List<UserInfo> selectAll();
// 直接引用上面定義的 BaseMap,無(wú)需重復(fù)寫(xiě) @Results
@ResultMap(value = "BaseMap")
@Select("select * from user_info where id = #{id}")
UserInfo selectById(Integer id);
// 再多一個(gè)方法也能復(fù)用
@ResultMap(value = "BaseMap")
@Select("select * from user_info where username = #{username}")
UserInfo selectByUsername(String username);
}全局駝峰命名自動(dòng)轉(zhuǎn)換
如果數(shù)據(jù)庫(kù)字段命名規(guī)范統(tǒng)一(如下劃線命名),最優(yōu)雅的方式是開(kāi)啟 MyBatis 的全局駝峰命名自動(dòng)轉(zhuǎn)換功能。
# application.yml
mybatis:
configuration:
map-underscore-to-camel-case: true # 配置駝峰自動(dòng)轉(zhuǎn)換開(kāi)啟后,Mapper 接口變得極其簡(jiǎn)潔,無(wú)需任何 @Results 注解:
@Mapper
public interface UserInfoMapper {
@Select("select * from user_info")
List<UserInfo> selectAll();
@Select("select * from user_info where id = #{id}")
UserInfo selectById(Integer id);
@Select("select * from user_info where username = #{username}")
UserInfo selectByUsername(String username);
}MyBatis 會(huì)自動(dòng)將 delete_flag 映射為 deleteFlag,create_time 映射為 createTime,
建議在項(xiàng)目初期就統(tǒng)一數(shù)據(jù)庫(kù)命名規(guī)范,并開(kāi)啟 map-underscore-to-camel-case 配置,這樣可以最大程度減少冗余的映射代碼。
到此這篇關(guān)于MyBatis 字段映射的解決方案的文章就介紹到這了,更多相關(guān)mybatis 字段映射內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis-Plus實(shí)現(xiàn)優(yōu)雅處理JSON字段映射
- MyBatis自定義TypeHandler如何解決字段映射問(wèn)題
- Mybatis中如何映射mysql中的JSON字段
- springMvc和mybatis-plus中枚舉值和字段的映射
- mybatis-plus之自動(dòng)映射字段(typeHandler)的注意點(diǎn)及說(shuō)明
- mybatis plus實(shí)體類中字段映射mysql中的json格式方式
- mybatis-plus實(shí)體類中出現(xiàn)非數(shù)據(jù)庫(kù)映射字段解決辦法
- mybatis某些字段無(wú)法映射成功的解決
- Java的MyBatis框架中關(guān)鍵的XML字段映射的配置參數(shù)詳解
相關(guān)文章
SpringBoot 2.0 整合sharding-jdbc中間件實(shí)現(xiàn)數(shù)據(jù)分庫(kù)分表
這篇文章主要介紹了SpringBoot 2.0 整合sharding-jdbc中間件,實(shí)現(xiàn)數(shù)據(jù)分庫(kù)分表,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-06-06
Spring Boot中的YML配置列表及應(yīng)用小結(jié)
在Spring Boot中使用YAML進(jìn)行列表的配置不僅簡(jiǎn)潔明了,還能提高代碼的可讀性和可維護(hù)性,這篇文章主要介紹了Spring Boot中的YML配置列表的詳細(xì)解析,需要的朋友可以參考下2025-05-05
利用Kotlin + Spring Boot實(shí)現(xiàn)后端開(kāi)發(fā)
這篇文章主要給大家介紹了關(guān)于利用Kotlin + Spring Boot實(shí)現(xiàn)后端開(kāi)發(fā)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Java+JFrame實(shí)現(xiàn)貪吃蛇小游戲
這篇文章主要為大家詳細(xì)介紹了Java+JFrame實(shí)現(xiàn)貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
淺談Java中的集合存儲(chǔ)數(shù)據(jù)后,輸出數(shù)據(jù)的有序和無(wú)序問(wèn)題
這篇文章主要介紹了淺談Java中的集合存儲(chǔ)數(shù)據(jù)后,輸出數(shù)據(jù)的有序和無(wú)序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
詳解Spring系列之@ComponentScan批量注冊(cè)bean
本文介紹各種@ComponentScan批量掃描注冊(cè)bean的基本使用以及進(jìn)階用法和@Componet及其衍生注解使用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-02-02
Kafka日志清理實(shí)現(xiàn)詳細(xì)過(guò)程講解
這篇文章主要為大家介紹了Kafka日志清理實(shí)現(xiàn)詳細(xì)過(guò)程講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

