Mybatis四种多表查询方式

131 篇文章 1 订阅
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.InvitationMapper">
    <resultMap id="baseResultMap" type="Invitation">
          <id property="id" column="ID"></id>
          <result property="title" column="title"></result>
          <result property="summary" column="summary"></result>
        <result property="author" column="author"></result>
        <result property="createdate" column="createdate"></result>

        <collection property="replay_detailList" ofType="Replay_detail" >
            <id property="id" column="rid"></id>
            <result property="content" column="content"></result>
            <result property="author" column="rauthor"></result>
            <result property="createdate" column="rcreatedate"></result>
        </collection>
    </resultMap>

    <resultMap id="lazeQueryResultMap" type="Invitation">
        <id property="id" column="ID"></id>
        <result property="title" column="title"></result>
        <result property="summary" column="summary"></result>
        <result property="author" column="author"></result>
        <result property="createdate" column="createdate"></result>
        <collection property="replay_detailList" ofType="Replay_detail" select="mapper.Replay_detailMapper.selectByInvId" column="id">
        </collection>

    </resultMap>

    <!--id:该条语句的唯一标识
    parameterType:表示入参类型
    resultType:返回结果将要封装成的类型-->

    <!--基本类型及String入参时,与#{}中的字符没有关系-->
    <!--1.通过传统多条select方式实现-->
    <select id="selectById" parameterType="java.lang.Integer" resultType="Invitation">
        select * from Invitation  where id = #{a}
    </select>
    <!--3.join关联查询,通过查询帖子列表,将其中的回复查询出来-->
    <select id="selectByIdAndJoin" resultMap="baseResultMap">
        SELECT invitation.*,replay_detail.id as rid,replay_detail.content,replay_detail.author as rauthor,replay_detail.createdate as rcreatedate from invitation,replay_detail where invitation.id=replay_detail.invid and invitation.id=#{id}
    </select>
    <!--<select id="selectAll"  resultType="Invitation">-->
        <!--select * from invitation-->
    <!--</select>-->
    <!--4.通过mybatis配置多条select方式实现(验证lazy加载)-->
      <select id="lazeQueryById" resultMap="lazeQueryResultMap">
          select * from Invitation  where id = #{a}
      </select>






    <!--引用类型入参,是POJO中的属性对应的get方法的后缀,首字母小写-->
    <!--<insert id="insert">-->
        <!--insert into invitation (title,summary,author,createdate) values (#{title},#{summary},#{author},#{createdate})-->
    <!--</insert>-->

    <!--<delete id="delete" parameterType="java.lang.Integer">-->
        <!--delete  from invitation where id = #{id}-->
    <!--</delete>-->

    <!--<update id="update">-->
        <!--update invitation set title=#{title},summary=#{summary} where id=#{id}-->
    <!--</update>-->

</mapper>
package mapper;

import model.Invitation;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

public interface InvitationMapper {
    Invitation selectById(int id);
//    List<Invitation> selectAll();
//    Integer insert(@Param("title") String title,@Param("summary") String summary,@Param("author") String author,@Param("createdate") Date createdate);
//    Integer delete(int id);
//    Integer update(@Param("title")String title,@Param("summary")String summary,@Param("id")int id);
    Invitation selectByIdAndJoin(int id);
    Invitation lazeQueryById(int id);

}
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.Replay_detailMapper">

    <!--id:该条语句的唯一标识
    parameterType:表示入参类型
    resultType:返回结果将要封装成的类型-->

    <!--基本类型及String入参时,与#{}中的字符没有关系-->
    <select id="selectById" parameterType="java.lang.Integer" resultType="model.Replay_detail">
        select * from replay_detail  where id = #{a}
    </select>

    <select id="selectByInvId" parameterType="java.lang.Integer" resultType="model.Replay_detail">
        select * from replay_detail  where invid = #{a}
    </select>

    <select id="selectByIdAndSubQuery" resultType="model.Replay_detail">
                select * from replay_detail  where invid = (select id from invitation where id = #{a})
    </select>

    <!--<select id="selectAll"  resultType="model.Replay_detail">-->
        <!--select * from replay_detail-->
    <!--</select>-->

    <!--&lt;!&ndash;引用类型入参,是POJO中的属性对应的get方法的后缀,首字母小写&ndash;&gt;-->
    <insert id="insert" keyColumn="ID" keyProperty="id" useGeneratedKeys="true">
        insert into replay_detail (invid,content,author,createdate) values (#{invid},#{content},#{author},#{createdate})
    </insert>

    <!--<delete id="delete" parameterType="java.lang.Integer">-->
        <!--delete  from replay_detail where invid = #{id}-->
    <!--</delete>-->

    <!--<update id="update">-->
        <!--update replay_detail set content=#{content},author=#{author} where id=#{id}-->
    <!--</update>-->

</mapper>
package mapper;


import model.Replay_detail;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

public interface Replay_detailMapper {
    Replay_detail selectById(int id);
//    List<Replay_detail> selectAll();
 //   Integer insert(@Param("invid") int invid, @Param("content") String content, @Param("author") String author, @Param("createdate") Date createdate);
//    Integer delete(int id);
//    Integer update(@Param("content")String content,@Param("author")String author,@Param("id")int id);
    List<Replay_detail> selectByInvId(int invid);
    List<Replay_detail> selectByIdAndSubQuery(int invid);
    Integer insert(Replay_detail replay_detail);


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值