Mybatis复杂查询(一对多)

通过ResultMap标签,在标签里定义Collection标签

1、按查询嵌套处理

实际两条执行sql

  <select id="getTeacher2" resultMap="teacherStu2">
        select * from teacher where id = #{id}
    </select>
    <resultMap id="teacherStu2" type="com.me.domain.Teacher2" >
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <collection property="students" javaType="ArrayList" ofType="com.me.domain.Student2" select="getStudentByTid" column="id"/>
    </resultMap>

    <select id="getStudentByTid" resultType="com.me.domain.Student2">
        select * from student where tid = #{id}
    </select>

1、集合--collection【一对多】

2、关联--association【多对一】

3、javaType:用来指定实体类中属性的类型

4、ofType:用来指定映射到List或集合中的实体类型,泛型中的约束类型。

2、按结果嵌套处理(联表查询)

<select id="getTeacher" resultMap="teacherStu">
        select s.id sid ,s.name sname ,t.id tid ,t.name tname
        from student s, teacher t
        where s.tid = t.id and t.id =#{id}
    </select>
    <resultMap id="teacherStu" type="com.me.domain.Teacher2">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="com.me.domain.Student2">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

实际一个查询sql,会有分页问题出现

多条件查询的写法:

column="{id=id,name=name}" 

mybatis的内部类用 $

com.shh.multiple.linkage.domain.ControlConfig$ControlConfigOperate

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值