学习mybatis第四天

多对一处理

按照查询嵌套处理

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="host.qianlong.dao.EmpMapper">
<!--
    思路:
        1.查询所有学生的信息
        2.更具查询出来的学生的dept_id,寻找对应的老师!   子查询
-->
    <select id="getEmployee" resultMap="EmpDept">
    select * from emp;
  </select>
    <resultMap id="EmpDept" type="Emp">
        <result property="eid" column="eid"></result>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
<!--        复杂的属性我们需要单独处理-->
        <association property="dept" column="dept_id" javaType="Dept" select="getDept"/>
    </resultMap>
    <select id="getDept" resultType="Dept" >
        select * from dept where did = #{did};
    </select>
</mapper>

按照结果嵌套查询处理

<!--    按照结果嵌套处理-->
    <select id="getEmployee2" resultMap="EmpDept2">
        select * from emp,dept where emp.dept_id = dept.did;
    </select>
    <resultMap id="EmpDept2" type="Emp">
        <result property="eid" column="eid"></result>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <association property="dept" javaType="Dept">
            <result property="did" column="did"></result>
            <result property="name" column="name"></result>
        </association>
    </resultMap>

10、一对多处理

实体类

//Dept
private int did;
   private String name;

   //一个部门拥有多个员工
    private List<Emp> emp;

Emp

//Emp
public class Emp {
    private int eid;
    private String name;
    private int age;
    private int dept_id;

接口

public interface DeptMapper {
//    List<Dept> getDept();

    //获取部门对应的员工
    Dept getDept(@Param("did")int did);


按照查询嵌套处理

<select id="getDept2" resultMap="DeptEmp2">
        select * from dept;
    </select>
    <resultMap id="DeptEmp2" type="Dept">
        <result property="did" column="did"></result>
        <result property="name" column="name"></result>
        <collection property="emp" javaType="ArrayList" ofType="Emp" select="getEmp" column="did"></collection>
    </resultMap>
    <select id="getEmp" resultType="Emp">
        select * from emp where dept_id = #{did}
    </select>

按照结果嵌套处理

<!--    按照结果嵌套处理-->
    <select id="getDept" resultMap="DeptEmp">
    select d.name dn,e.name en,e.age from emp e,dept d
    where e.dept_id = d.did and d.did = #{did};
  </select>
    <resultMap id="DeptEmp" type="Dept">
        <result property="did" column="did"></result>
        <result property="name" column="dn"></result>
<!--        复杂的属性,我们需要单独处理 对象: association 集合:collection
            集合中的泛型信息,我们使用ofType获取
-->
        <collection property="emp" ofType="Emp">
            <result property="eid" column="eid"></result>
            <result property="name" column="en"></result>
            <result property="age" column="age"></result>
            <result property="dept_id" column="dept_id"></result>
        </collection>
    </resultMap>

小结

  • 关联-association【多对一】

  • 集合-collection【一对多】

  • JavaType & ofType

    • JavType用来只当实体类中属性的类型
    • ofType用来指定映射到List或者集合中的pojo类型,泛型中的约束类型!
  • 注意点:

  • 保证SQL的可读性,尽量保证通俗易懂

  • 注意一对多和多对一中,属性名和字段的问题

  • 如果问题不好排除可以使用日志

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值