Mybatis之association和collection用法

目录
  • association和collection用法
    • 1.单个关联查询association
    • 2.多个关联查询 collection
    • 3.鉴别器discriminator
  • association和collection关联查询用法
    • 一对多 collection
    • 一对一 & 多对一

association和collection用法

1.单个关联查询association

1.1实体之间的关联表示

package com.worldly.config.entity;
import java.io.Serializable;
/**
 * @Description
 * @Author xiaoqx <worldly_xuan@163.com>
 * @Version V1.0.0
 * @Since 2017/11/26
 */
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.查询条件相同,所用的时间:从测试结果显示,关联查询要比嵌套查询快一点(结果不一定准确,可能关联表多的时候,结果会有所变化)

b.适用的情况

2.多个关联查询 collection

2.1实体之间的关联表示

package com.worldly.config.entity;
import java.util.List;
/**
 * @Description
 * @Author xiaoqx <worldly_xuan@163.com>
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2017/12/16
 */
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>

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

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"/>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Mybatis中collection和association的使用区别详解

    最近一直把collection和association弄混,所以为了增强自己的记忆,就撸一个关系出来算是总结罢了 1. 关联-association 2. 集合-collection 比如同时有User.java和Card.java两个类 User.java如下: public class User{ private Card card_one; private List<Card> card_many; } 在映射card_one属性时用association标签, 映射card_many时

  • 详解mybatis中association和collection的column传入多个参数问题

    项目中在使用association和collection实现一对一和一对多关系时需要对关系中结果集进行筛选,如果使用懒加载模式,即联合使用select标签时,主sql和关系映射里的sql是分开的,查询参数传递成为问题. mybatis文档: property description column 数据库的列名或者列标签别名.与传递给resultSet.getString(columnName)的参数名称相同.注意: 在处理组合键时,您可以使用column="{prop1=col1,prop2=c

  • mybatis利用association或collection传递多参数子查询

    有时候我们在查询数据库时,需要以查询结果为查询条件进行关联查询. 在mybatis 中通过 association 标签(一对一查询,collection 一对多 查询) 实现延迟加载子查询 <resultMap id="xxxMap" type="xxxx.bean.xxx" extends="zzzzMap"> <association property="destName" javaType="

  • Mybatis之association和collection用法

    目录 association和collection用法 1.单个关联查询association 2.多个关联查询 collection 3.鉴别器discriminator association和collection关联查询用法 一对多 collection 一对一 & 多对一 association和collection用法 1.单个关联查询association 1.1实体之间的关联表示 package com.worldly.config.entity; import java.io.S

  • MyBatis持久层框架的用法知识小结

    MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录. MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google c

  • MyBatis动态SQL标签的用法详解

    1.MyBatis动态SQL MyBatis 的强大特性之一便是它的动态 SQL,即拼接SQL字符串.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉列名列表最后的逗号.利用动态 SQL 这一特性可以彻底摆脱这种痛苦. 通常使用动态 SQL 不可能是独立的一部分,MyBatis 当然使用一种强大的动态 SQL 语言来改进这种情形,这种语言可以被用在任意的 SQL 映射语句中. 动态 SQL 元素和

  • mybatis嵌套循环map方式(高级用法)

    目录 mybatis嵌套循环map mybatis入参map的基本语法 mybatis嵌套循环map的高级用法 mybatis中循环map集合操作 mybatis嵌套循环map mybatis有默认的list,array,但是没有默认的map.所以不能直接写collection="map",如果这么写,它会当成是根据map.get(“map”)来取value值,大部分情况下是一个map中是不会有“map”这个key的,于是就是报错. 如果你想用这个“map”标识取参数map,就需要保证

  • MyBatis中association的基本使用方法

    目录 通过association对两表进行联表查询 按照查询嵌套处理 按照结果嵌套处理 总结 通过association对两表进行联表查询 student表属性如下 teacher表属性如下 按照查询嵌套处理 关于需求的SQL稍微有点复杂时,可以打开右侧查询框进行语句的编写执行. 当使用以下时,查询出来存在问题 <select id="getStudentTeacher" resultType="Student" > select s.id,s.name

  • MyBatis XML方式的基本用法之多表查询功能的示例代码

    1. 多表查询 在之前,我们示例的2个查询都是单表查询,但实际的业务场景肯定是需要多表查询的,比如现在有个需求: 查询某个用户拥有的所有角色.这个需求要涉及到sys_user,sys_user_role,sys_role三张表,如何实现呢? 首先,在SysUserMapper接口中定义如下方法. /** * 根据用户id获取角色信息 * * @param userId * @return */ List<SysRole> selectRolesByUserId(Long userId); 然后

  • Mybatis注解方式@Insert的用法

    目录 Mybatis注解方式@Insert 1.不需要返回主键 2.返回自增主键 3.返回非自增主键 Mybatis@Insert注解批量插入数据库 bean:实体类 Mapper Mybatis注解方式@Insert 1.不需要返回主键 @Insert({"insert into user(name, create_time) values(#{name}, #{createTime, jdbcType=TIMESTAMP})"}) int add(User user); 2.返回自

  • mybatis.type-aliases-package的作用及用法说明

    目录 mybatis.type-aliases-package的用法说明 type-aliases-package使用的几个问题 mybatis.type-aliases-package的用法说明 在mapper.xml文件中的resultMap的type或者parameterType会用到自定义的POJO. 例如: <mapper namespace="com.example.demo.mapper.UserMapper">     <select id="

随机推荐