MyBatis 字段映射的解决方案
引言
在使用 MyBatis 进行数据库操作时,数据库字段名(如下划线命名 delete_flag)与 Java 实体类属性名(驼峰命名 deleteFlag)之间的映射是一个常见问题。本文将介绍三种解决方案:手动 @Results 映射、可复用的 @Results(id) 映射以及全局驼峰命名配置。
首先,我们定义一个实体类UserInfo,后续所有示例都基于这个类:
public class UserInfo {
private Integer id;
private String username;
private String password;
private Integer age;
private String gender;
private String phone;
private Integer deleteFlag; // 数据库字段 delete_flag
private Date createTime; // 数据库字段 create_time
private Date updateTime; // 数据库字段 update_time
// getter / setter 省略
}手动 @Results 映射
最直接的方式是在每个查询方法上使用 @Results 注解,手动指定字段映射关系。
@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();
// 另一个查询方法,需要重复写一遍 @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);
}可复用的 @Results(id) 映射
为了解决重复代码问题,MyBatis 允许为@Results 注解指定一个 id,其他方法可以通过 @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,无需重复写 @Results
@ResultMap(value = "BaseMap")
@Select("select * from user_info where id = #{id}")
UserInfo selectById(Integer id);
// 再多一个方法也能复用
@ResultMap(value = "BaseMap")
@Select("select * from user_info where username = #{username}")
UserInfo selectByUsername(String username);
}全局驼峰命名自动转换
如果数据库字段命名规范统一(如下划线命名),最优雅的方式是开启 MyBatis 的全局驼峰命名自动转换功能。
# application.yml
mybatis:
configuration:
map-underscore-to-camel-case: true # 配置驼峰自动转换开启后,Mapper 接口变得极其简洁,无需任何 @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 会自动将 delete_flag 映射为 deleteFlag,create_time 映射为 createTime,
建议在项目初期就统一数据库命名规范,并开启 map-underscore-to-camel-case 配置,这样可以最大程度减少冗余的映射代码。
到此这篇关于MyBatis 字段映射的解决方案的文章就介绍到这了,更多相关mybatis 字段映射内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
谈谈Java利用原始HttpURLConnection发送POST数据
这篇文章主要给大家介绍java利用原始httpUrlConnection发送post数据,设计到httpUrlConnection类的相关知识,感兴趣的朋友跟着小编一起学习吧2015-10-10
构建springboot自动生成mapper文件和dao接口项目的步骤和配置方法
这篇文章主要介绍了构建springboot自动生成mapper文件和dao接口项目的步骤和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-05-05
详解eclipse将项目打包成jar文件的两种方法及问题解决方法
本文给大家介绍了eclipse中将项目打包成jar文件的两种方法及其遇到问题解决方法,本文图文并茂给大家介绍的非常详细,需要的朋友可以参考下2017-12-12


最新评论