iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Mybatis中association和collection怎么用
  • 125
分享到

Mybatis中association和collection怎么用

2023-06-29 02:06:43 125人浏览 八月长安
摘要

这篇文章将为大家详细讲解有关mybatis中association和collection怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。association和collection用法1.单个关联查询

这篇文章将为大家详细讲解有关mybatis中association和collection怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

association和collection用法

1.单个关联查询association

1.1实体之间的关联表示

package com.worldly.config.entity;import java.io.Serializable;public class Employee implements Serializable {    private Integer id;    private String name;    private String email;    private String tel;    //关联的部门实体,查询某个人的时候可以把所在部门信息查询出来    private Department dep;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    public String getTel() {        return tel;    }    public void setTel(String tel) {        this.tel = tel;    }    public Department getDep() {        return dep;    }    public void setDep(Department dep) {        this.dep = dep;    }    @Override    public String toString() {        return "{\"Employee\":{"                + "\"id\":\"" + id + "\""                + ", \"name\":\"" + name + "\""                + ", \"email\":\"" + email + "\""                + ", \"tel\":\"" + tel + "\""                + ", \"dep\":" + dep                + "}}";    }}

1.2 两种关联查询方式

//第一中方式:直接进行关联查询把关联实体的属性在xml中配置//然后关联查出来<resultMap id="emp2ResultMap" type="com.worldly.config.entity.Employee">        <id column="emp_id" property="id"></id>        <result column="emp_name" property="name"/>        <result column="emp_email" property="email"/>        <result column="emp_tel" property="tel"/>        <association property="dep" column="emp_dep" javaType="com.worldly.config.entity.Department">            <id column="dep_id" property="id"/>            <result column="dep_name" property="name"/>            <result column="dep_addr" property="addr"/>        </association>    </resultMap>    <select id="selectEmployAll" resultMap="emp2ResultMap">        SELECT            *        FROM            t_emp e        INNER JOIN t_dep d ON e.emp_dep = d.dep_id    </select>
//第二中查询方式,采用 association中的select来查询<resultMap id="empResultMap" type="com.worldly.config.entity.Employee">        <id column="emp_id" property="id"></id>        <result column="emp_name" property="name"/>        <result column="emp_email" property="email"/>        <result column="emp_tel" property="tel"/>        <association column="emp_dep" property="dep" javaType="com.worldly.config.entity.Department" select="selectDepByCondition"></association>    </resultMap>    <select id="selectEmployeeList" resultMap="empResultMap" databaseId="mysql">        select * from t_emp    </select>    <resultMap id="depResultMap" type="com.worldly.config.entity.Department">        <id column="dep_id" property="id"></id>        <result column="dep_name" property="name"/>        <result column="dep_addr" property="addr"/>    </resultMap>    <select id="selectDepByCondition" resultMap="depResultMap">        SELECT        *        FROM        t_dep d        WHERE        d.dep_id = #{emp_dep}    </select>

1.3 两种方式的优劣

a.查询条件相同,所用的时间:从测试结果显示,关联查询要比嵌套查询快一点(结果不一定准确,可能关联表多的时候,结果会有所变化)

a.查询条件相同,所用的时间:从测试结果显示,关联查询要比嵌套查询快一点(结果不一定准确,可能关联表多的时候,结果会有所变化)

Mybatis中association和collection怎么用

Mybatis中association和collection怎么用

b.适用的情况

2.多个关联查询 collection

2.1实体之间的关联表示

package com.worldly.config.entity;import java.util.List;public class Department {    private int id;    private String name;    private String addr;    List<Employee> employeeList;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getNamel() {        return name;    }    public void setNamel(String name) {        this.name = name;    }    public String getAddr() {        return addr;    }    public void setAddr(String addr) {        this.addr = addr;    }    public List<Employee> getEmployeeList() {        return employeeList;    }    public void setEmployeeList(List<Employee> employeeList) {        this.employeeList = employeeList;    }    @Override    public String toString() {        return "{\"Department\":{"                + "\"id\":\"" + id + "\""                + ", \"name\":\"" + name + "\""                + ", \"addr\":\"" + addr + "\""                + ", \"employeeList\":" + employeeList                + "}}";    }}

2.2 两种关联查询方式

//第一种方式嵌套查询<resultMap id="depResultMap2" type="com.worldly.config.entity.Department">        <id column="dep_id" property="id"></id>        <result column="dep_name" property="name"/>        <result column="dep_addr" property="addr"/>        <collection column="dep_id" property="employeeList" javaType="java.util.List" ofType="com.worldly.config.entity.Employee"           select="selectEmpBydepId"/>    </resultMap>    <select id="selectDepByCondition" resultMap="depResultMap2">        SELECT            *        FROM            t_dep d        WHERE            d.dep_id = #{param}    </select>    <resultMap id="empResultMap" type="com.worldly.config.entity.Employee">        <id column="emp_id" property="id"></id>        <result column="emp_name" property="name"/>        <result column="emp_email" property="email"/>        <result column="emp_tel" property="tel"/>    </resultMap>    <select id="selectEmpBydepId" resultMap="empResultMap">        SELECT            *        FROM            t_emp e        WHERE            e.emp_dep = #{dep_id}    </select>
//第二中方式关联查询<resultMap id="dep2ResultMap" type="com.worldly.config.entity.Department">        <id column="dep_id" property="id"></id>        <result column="dep_name" property="name"/>        <result column="dep_addr" property="addr"/>        <collection property="employeeList" ofType="com.worldly.config.entity.Employee">            <id column="emp_id" property="id"></id>            <result column="emp_name" property="name"/>            <result column="emp_email" property="email"/>            <result column="emp_tel" property="tel"/>        </collection>    </resultMap>    <select id="selectDepWithEmp" resultMap="dep2ResultMap">        SELECT            *        FROM            t_dep d        INNER JOIN t_emp e ON d.dep_id = e.emp_dep        WHERE            d.dep_id = #{param}    </select>

2.3 多条件查询

<resultMap id="depResultMap2" type="com.worldly.config.entity.Department">        <id column="dep_id" property="id"></id>        <result column="dep_name" property="name"/>        <result column="dep_addr" property="addr"/>        <result column="dep_status" property="status"/>        <collection column="{depId=dep_id,status=dep_status}" property="employeeList" javaType="java.util.List" ofType="com.worldly.config.entity.Employee"           select="selectEmpBydepId"/>    </resultMap>    <select id="selectDepByCondition" resultMap="depResultMap2">        SELECT            *        FROM            t_dep d        WHERE            d.dep_id = #{param}    </select>    <resultMap id="empResultMap" type="com.worldly.config.entity.Employee">        <id column="emp_id" property="id"></id>        <result column="emp_name" property="name"/>        <result column="emp_email" property="email"/>        <result column="emp_tel" property="tel"/>    </resultMap>    <select id="selectEmpBydepId" resultMap="empResultMap">        SELECT            *        FROM            t_emp e        WHERE            e.emp_dep = #{depId} AND e.emp_status=#{status}    </select>

多条件查询,用{}来包装方法

Mybatis中association和collection怎么用

3.鉴别器discriminator

3.1 鉴别器适用的场景

3.2 鉴别器的实现 

association和collection关联查询用法

这里只做最简单的用法,其它方法请自行查询;

一对多 collection

 <collection property="要查询的实体集合" javaType="java.util.List"                    ofType="要查询的实体所在包路径"                    select="要查询的mapper方法"                    column="关联的实体中的字段=关联的数据库中的字段"/>

举例

 <collection property="stsManageStudentList" javaType="java.util.List"                    ofType="com.crm.project.domain.StsManageStudent"                    select="com.crm.project.mapper.StsManageStudentMapper.selectStsManageStudentList"                    column="manageId=manage_id"/>

一对一 & 多对一

<association property="要查询的实体" column="数据库中的关联字段"                     javaType="要查询的实体所在包路径"                     select="要查询的mapper方法"/>

举例

<association property="stsStudent" column="student_id"                     javaType="com.crm.project.domain.StsStudent"                     select="com.crm.project.mapper.StsStudentMapper.selectStsStudentById"/>

关于“Mybatis中association和collection怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

--结束END--

本文标题: Mybatis中association和collection怎么用

本文链接: https://www.lsjlt.com/news/321977.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Mybatis中association和collection怎么用
    这篇文章将为大家详细讲解有关Mybatis中association和collection怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。association和collection用法1.单个关联查询...
    99+
    2023-06-29
  • Mybatis之association和collection用法
    目录association和collection用法1.单个关联查询association2.多个关联查询 collection3.鉴别器discriminatorassociati...
    99+
    2022-11-13
  • mybatis collection和association的区别解析
    目录1.collection标签1.1 相关代码和运行结果1.2 collection部分源码解析1.3 <collection>和<association>...
    99+
    2022-11-13
  • MyBatis中的ResultMap的association和collection标签详解
    目录一、前言二、ResultMap 的属性列表三、resultMap标签介绍四、id & result标签参数详解五、association标签常用参数详解六、collect...
    99+
    2022-11-13
    MyBatis association和collection标签 MyBatis  ResultMap
  • foreach与collection怎么在mybatis 中使用
    foreach与collection怎么在mybatis 中使用?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。foreach的主要用在构建in条件中,它可以在SQL语句中进...
    99+
    2023-05-31
    mybatis foreach collection
  • MyBatis中association的基本使用方法
    目录通过association对两表进行联表查询按照查询嵌套处理按照结果嵌套处理总结通过association对两表进行联表查询 student表属性如下 teacher表属性如下...
    99+
    2022-11-13
  • MyBatis中的collection如何使用
    今天小编给大家分享一下MyBatis中的collection如何使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。colle...
    99+
    2023-07-02
  • Mybatis中association标签多层嵌套问题怎么解决
    这篇“Mybatis中association标签多层嵌套问题怎么解决”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Mybat...
    99+
    2023-06-29
  • mybatis中一对一关系association标签的使用
    目录一对一关系association标签使用1、嵌套的resultMap2、嵌套的select语句association标签三种用法association的用法一assoc...
    99+
    2022-11-13
  • Mybatis怎么解决collection一对多问题
    这篇文章主要介绍了Mybatis怎么解决collection一对多问题的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Mybatis怎么解决collection一对多问题文章都会有所收获,下面我们一起来看看吧。先...
    99+
    2023-07-05
  • Mybatis的collection三层嵌套查询怎么写
    这篇“Mybatis的collection三层嵌套查询怎么写”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Mybatis的c...
    99+
    2023-07-05
  • resultMap中的collection标签怎么用
    这篇文章主要介绍“resultMap中的collection标签怎么用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“resultMap中的collection标签怎么用”文章能帮助大家解决问题。re...
    99+
    2023-06-29
  • MyBatis中的collection两种使用方法及效率比较
    目录第一种方式,采用select第二种方式,执行一次sql比较collection主要是应对表关系是一对多的情况 查询的时候,用到联表去查询 接下来的小案例包括:市,学校,...
    99+
    2022-11-13
  • UI5使用Association和Aggregation方法是什么
    这篇文章主要介绍“UI5使用Association和Aggregation方法是什么”,在日常操作中,相信很多人在UI5使用Association和Aggregation方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法...
    99+
    2023-06-04
  • Java Collection集合的ArrayList和HashSet怎么用
    今天小编给大家分享一下Java Collection集合的ArrayList和HashSet怎么用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我...
    99+
    2023-06-29
  • MyBatis中int和Integer怎么使用
    这篇文章主要介绍“MyBatis中int和Integer怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“MyBatis中int和Integer怎么使用”文章能帮助大家解决问题。有关int和In...
    99+
    2023-07-05
  • Mybatis中resultMap的Colum和property怎么用
    本文小编为大家详细介绍“Mybatis中resultMap的Colum和property怎么用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Mybatis中resultMap的Colum和property怎么用”文章能帮助大家解决疑惑,下...
    99+
    2023-06-26
  • 怎么用ABAP代码读取CDS view association的数据
    这篇文章主要介绍了怎么用ABAP代码读取CDS view association的数据,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。 我有...
    99+
    2022-10-19
  • Mybatis中resultMap怎么用
    小编给大家分享一下Mybatis中resultMap怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!resultMap是Mybatis最强大的元素,它可以将查...
    99+
    2023-06-20
  • MyBatis中@MapKey怎么用
    这篇文章将为大家详细讲解有关MyBatis中@MapKey怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。MyBatis @MapKey的妙用背景在实际开发中,有一些场景需要我们返回主键或者唯一键为K...
    99+
    2023-06-29
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作