MyBatis的关联映射

devtools/2025/3/5 5:02:14/

前言

    在实际开发中,对数据库的操作通常会涉及多张表,MyBatis提供了关联映射,这些关联映射可以很好地处理表与表,对象与对象之间的的关联关系。

一对一查询

步骤:

  1. 先确定表的一对一关系
  2. 确定好实体类,添加关联对象
  3. 使用resultMap定义好输出参数
  4. 编写sql语句
  5. 测试

    在MyBatis中,通过<association>元素来处理一对一的关联关系。<association>元素中的属性如下:

属性说明
property用于指定映射到的实体类对象的属性,与表字段一一对应
column用于指定表中对应的字段
javaType用于指定映射到实体对象的属性的类型
jdbcType用于指定数据库中对应字段的类型
fetchType用于指定关联查询时是否延迟加载。fetchType有lazy和eager两个属性值,默认为lazy延迟加载
select用于引入嵌套查询的SQL语句,该属性用于关联映射的嵌套查询
autoMapping用于指定是否自动映射
typeHandler用于指定一个类型处理器
    <resultMap id="oneByOne" type="com.cc.User"><id column="id" property="id"/><result column="name" property="name"/><result column="gender" property="gender"/><result column="age" property="age"/><result column="address" property="address"/><result column="email" property="email"/><result column="qq" property="qq"/><association property="login" javaType="com.cc.Login"><id column="id" property="id"/><result column="username" property="username"/><result column="password" property="password"/></association></resultMap><select id="findPassword" resultMap="oneByOne">select u.*,l.* from tb_userinfo u,tb_login l where u.name=l.username;</select>

   使用resultMap定义结果映射集,其中id标签用于映射数据库表中的主键字段,column为数据库的列名,property为java类的属性名。result用于映射普通字段。

    association标签用于映射关联对象的信息。property为关联对象的属性名,javaType为关联对象的Java类路径

一对多查询 

    在MyBatis中,通过<collection>元素来处理一对多关联关系。<collection>大多与<association>元素相同,还包含一个特殊属性ofType与javaType属性相对应,它用于指定实体类对象中集合类属性所包含的元素的类型。


示例:

    通过tb_userinfo表中的name值与tb_login中的username关联查询出两张表中的数据:

    将userinfo表与login两张表设计成两个实体类,并将login作为属性加入到userinfo中(也可以反过来):

public class User {private String id;private String name;private String gender;private int age;private String address;private String email;private String qq;private Login login;public String getId() {return id;}public void setId(String id) {this.id = id;}public Login getLogin() {return login;}public void setLogin(Login login) {this.login = login;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getQq() {return qq;}public void setQq(String qq) {this.qq = qq;}public User(String name, String gender, int age, String address, String email, String qq) {this.name = name;this.gender = gender;this.age = age;this.address = address;this.email = email;this.qq = qq;}public User() {}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", gender='" + gender + '\'' +", age=" + age +", address='" + address + '\'' +", email='" + email + '\'' +", qq='" + qq + '\'' +", login=" + login +'}';}
}public class Login {private Integer id;private String username;private String password;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Login(){}public Login(String password, String username) {this.password = password;this.username = username;}@Overridepublic String toString() {return "Login{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +'}';}
}

    <resultMap id="order" type="com.cc.User"><id column="id" property="id"/><result column="name" property="name"/><result column="gender" property="gender"/><result column="age" property="age"/><result column="address" property="address"/><result column="email" property="email"/><result column="qq" property="qq"/><collection property="login" ofType="com.cc.Login"><id column="id" property="id"/><result column="username" property="username"/><result column="password" property="password"/></collection></resultMap><select id="findPassword" resultMap="order">select u.*,l.* from tb_userinfo u,tb_login l where u.name=l.username;</select>

测试: 

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);List<User> u = userMapper.findPassword();for (User user:u){System.out.println(user);}

    在使用MyBatis嵌套查询进行MyBatis关联映射查询时,使用MyBatis的延迟加载在一定程度上可以降低运行消耗并提高查询效率。MyBatis默认没有开启延迟加载,需要在核心配置文件mybatis.xml中的setting元素内进行配置。

        <setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/>

多对多查询

    在数据库中,多对多的关联关系通常要使用一个中间表来维护。

    例如,要查询每个员工的职位,根据employee的id值在emp_posi找到对应的pid,再根据pid查找position职位的名称,如下图所示:

映射思路:

将position信息映射到employee中

在position添加属性List<EP> eps

mapper配置如下: 

    <resultMap id="map" type="com.cc.entity.Employee"><id property="id" column="id"/><result property="name" column="name"/><result property="age" column="age"/><result property="position" column="position"/><collection property="positions" ofType="com.cc.entity.Position"><id property="id" column="id"/><result property="name" column="name"/><collection property="eps" ofType="com.cc.entity.EP"><id property="id" column="id"/><result property="eId" column="emp_id"/><result property="pId" column="posi_id"/></collection></collection></resultMap><select id="selectPosi" resultMap="map">select e.*,p.*,ep.*from employee e,position p,emp_posi epwhere e.id=ep.emp_id and ep.posi_id=p.id;</select>

 查询结果如下:

发现查询出的职位错查成employee中的name值。出现该错误的原因是employee和position表使用了相同的列名name,解决方法就是给这些字段起别名

 


http://www.ppmy.cn/devtools/164651.html

相关文章

vue3+nuxt中监听sessionStorage.setItem,数据发生变化动态获取响应

在写项目时&#xff0c;遇见一个未读消息的功能&#xff0c;当消息已读时&#xff0c;减少一条未读消息提示&#xff0c;需要实现当sessionStorage.setItem存储的数据发生改变时&#xff0c;响应式的获取并显示&#xff0c;但是由于sessionStorage 的 setItem 操作本身并不会触…

深度学习-137-LangGraph之应用实例(六)构建RAG问答系统带条件边分支

文章目录 1 大语言模型2 带分支的RAG系统2.1 处理文本构建Document2.2 向量存储2.3 自定义工具2.4 创建图2.4.1 预构建的MessagesState2.4.2 编排图2.4.3 可视化图2.5 测试调用3 参考附录使用langgraph框架构建一个带有条件分支的智能问答系统。创建一个能够从网页文档中提取信…

mac 安装node提示 nvm install v14.21.3 failed可能存在问题

如果你在 macOS 上使用 nvm&#xff08;Node Version Manager&#xff09;安装 Node.js 版本 v14.21.3 时遇到安装失败的问题&#xff0c;可以按照以下步骤进行排查和解决&#xff1a; 1. 确认 nvm 安装是否正确 首先&#xff0c;确认你的 nvm 是否正确安装&#xff0c;并且能…

【FL0093】基于SSM和微信小程序的微信点餐系统小程序

&#x1f9d1;‍&#x1f4bb;博主介绍&#x1f9d1;‍&#x1f4bb; 全网粉丝10W,CSDN全栈领域优质创作者&#xff0c;博客之星、掘金/知乎/b站/华为云/阿里云等平台优质作者、专注于Java、小程序/APP、python、大数据等技术领域和毕业项目实战&#xff0c;以及程序定制化开发…

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_list_init

ngx_list_init 定义在 src\core\ngx_list.h static ngx_inline ngx_int_t ngx_list_init(ngx_list_t *list, ngx_pool_t *pool, ngx_uint_t n, size_t size) {list->part.elts ngx_palloc(pool, n * size);if (list->part.elts NULL) {return NGX_ERROR;}list->par…

如何将一台服务器的pip环境迁移到另一个机器?

在没有网络的情况下,将一台服务器的 pip 环境迁移到另一台机器,可按以下步骤进行操作: 步骤一:在源服务器上导出已安装的包列表 在有网络且已安装所需 Python 包的源服务器上,使用以下命令导出已安装的 Python 包列表: pip freeze > requirements.txt该命令会将当前…

中间件专栏之Redis篇——Redis的三大持久化方式及其优劣势对比

Redis是内存数据库&#xff0c;它的数据一般存放在内存中&#xff0c;一旦断电或者宕机&#xff0c;存在内存中的数据就会丢失。当然&#xff0c;它也具备数据持久化的能力&#xff0c;本文就将介绍Redis的三种持久化方式及其优劣势对比。 一、RDB&#xff08;Redis Database&…

VUE3+Vite使用TailwindCSS【若依前后端分离框架】

参考&#xff1a;https://tailwind.nodejs.cn/docs/guides/vite#vue 和 https://blog.csdn.net/hjl_and_djj/article/details/144694485依次运行命令&#xff1a; cnpm install -D tailwindcss3.4.17 postcss autoprefixernpx tailwindcss init -p修改配置文件tailwind.config.…