五,mybatis的多表关联查询

一,association关联查询

先来介绍association。Association有两种用法,第一种是自己创建要查询的内容。
在这里插入图片描述

<resultMap id="empResultMapByAssociation" type="emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
        <association property="dept" javaType="dept">
            <id column="dept_id" property="deptId"></id>
            <result column="dept_name" property="deptName"></result>
        </association>
    </resultMap>

可以看到,我们在resultMap里面加了一个association,这个property属性是说明这个association关联的实体类的名称是什么,javaType是对应实体类的类名或者是类型别名(比如String,Int等等)。
那么现在就来创建对应的方法

List<Emp> selectEmpByAssociationOne(@Param("empId") Integer empId);  

和sql语句

<select id="selectEmpByAssociationOne" resultMap="empResultMapByAssociation">
        select * from t_emp left join t_dept on t_emp.dept_id=t_dept.dept_id where t_emp.emp_id=#{empId}
    </select>

观察这个sql语句可以看到含义是 t_emp表与t_dept表左连接,以t_emp的dept_id字段和t_dept的dept_id字段相同连接,并且筛选所有t_emp的emp_id的值等同于输入的值的情况。

现在来讲association的第二个用法。就是不自己创建要查询的字段,在association里面锁定要查询的另一个表的mapper的方法查询,将结果拼接在一起。
在这里插入图片描述

<resultMap id="empResultMapStepOne" type="emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
        <association property="dept"
                     select="com.chenchao.mybatis.mapper.DeptMapper.selectDeptStepTwo"
                     column="dept_id">
        </association>
    </resultMap>

这里的property和上面的是一样的,select就是查询语句是我们mapper的DeptMapper里面的selectDeptStepTwo方法,然后我们传递过去的参数是查询出来的每行数据的dept_id字段数据。
在这里插入图片描述
这也意味着使用这个方法必须要到DeptMapper里面创建对应的方法来满足查询。现在我们从EmpMapper开始创建方法。在EmpMapper创建方法

List<Emp> selectEmpByAssociationStepOne(@Param("empId") Integer empId);

再创建对应的sql语句

<select id="selectEmpByAssociationStepOne" resultMap="empResultMapStepOne">
        select * from t_emp where emp_id=#{empId}
    </select>

为了满足查询我们还要到DeptMapper里面创建方法,而且方法里面有参数,是我们传递过去的dept_id。
DeptMapper里面的方法是

List<Dept> selectDeptStepTwo(@Param("deptId") Integer deptId);

对应的sql语句是

<select id="selectDeptStepTwo" resultType="com.chenchao.mybatis.pojo.Dept">
        select * from t_dept where dept_id=#{deptId}
    </select>

这里直接使用resultType的原因是开启了转换命名法的setting所以可以查询到。

二,collection关联查询

使用collection也差不多。这两个基本可以互换。但是默认情况下assiociation应用于一对一和多对一的情况,而collection应用于一对多的情况。
现在简单解释collection的用法。

<resultMap id="resultMapByCollection" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <collection property="emps" ofType="Emp">
            <id column="emp_id" property="empId"></id>
            <result column="emp_name" property="empName"></result>
            <result column="age" property="age"></result>
            <result column="gender" property="gender"></result>
        </collection>
    </resultMap>

可以看到用法和association基本相同,只有ofType有差别,ofType是对象的类型。
那么我们就只需要创建对应的方法和sql语句就可以了。这个用法没有使用别的mapper的方法,所以只是使用左连接方法查询表
先创建方法

List<Emp> selectEmpByAssociationOne(@Param("empId") Integer empId);

再创建sql语句

<select id="selectEmpByAssociationOne" resultMap="empResultMapByAssociation">
        select * from t_emp left join t_dept on t_emp.dept_id=t_dept.dept_id where t_emp.emp_id=#{empId}
    </select>

在启动类创建对应的方法

@Test
    public void testForSelectDeptByCollection() throws IOException {
        SqlSession sqlSession= SqlSessionUtil.getSqlSession();
        DeptMapper deptMapper= sqlSession.getMapper(com.chenchao.mybatis.mapper.DeptMapper.class);
        List<Dept>  result=deptMapper.selectDeptByCollection(1);
        System.out.println(result);
        sqlSession.close();
    }


现在介绍使用别的Mapper方法的collection

<resultMap id="selectDeptStepOne" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <collection property="emps"
                    select="com.chenchao.mybatis.mapper.EmpMapper.selectStepTwoByCollection"
                    column="dept_id"></collection>
    </resultMap>

创建方法

List<Dept> selectStepOneByCollection(@Param("deptId") Integer deptId);

创建sql方法

<select id="selectStepOneByCollection" resultMap="selectDeptStepOne">
        select * from t_dept where dept_id=#{deptId}
    </select>

再到EmpMapper里面创建方法

List<Emp> selectStepTwoByCollection(@Param("deptId") Integer deptId);

创建sql语句

<select id="selectStepTwoByCollection" resultType="emp">
        select * from  t_emp where  dept_id = #{deptId}
    </select>


在启动类创建对应的方法

@Test
    public void testForSelectDeptByCollectionStepOne() throws IOException {
        SqlSession sqlSession= SqlSessionUtil.getSqlSession();
        DeptMapper deptMapper= sqlSession.getMapper(com.chenchao.mybatis.mapper.DeptMapper.class);
        List<Dept>  result=deptMapper.selectStepOneByCollection(1);
        System.out.println(result);
        sqlSession.close();
    }

  • 18
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值