Mybatis的查询方式

分页查询:(数据库数据是从0开始计数)
Limit:page size ,每页多少项
Offset:从0开始,从哪一行开始

两种方法:
第一种:手写SQL进行分页,将offset和limit直接写入SQL中
接口中的方法:

List<Customer> findPage(@Param("offset") int offset, @Param("limit") int limit);
Xml文档中的配置:
<select id="findPage" resultType="Customer">
        SELECT * FROM CUSTOMER ORDER BY ID OFFSET #{offset} LIMIT #{limit}
</select>

第二种:通过mybatis自己的支持,结合jdbc驱动进行分页,不需要在SQL语句中写limit和offset(返回类型相同,参数类型不一样了)
Mybatis提供了Rowbounds类用于实现分页查询,该类提供了两个构造方法,一个为空构造函数,一个需要两个参数,同样是offset和limit,相当于将第一种方法SQL语句中的两个参数封装到了Mybatis中。

接口中的方法:

List<Customer> findPage2(RowBounds rowBounds);

Xml文档中的配置:

<select id="findPage2" resultType="Customer">
        SELECT * FROM CUSTOMER ORDER BY ID 
</select>

Main函数中的实现方法:

System.out.println("findPage2(6,5):" + mapper.findPage2(new RowBounds(6, 5)));

多表查询:(手写映射-resultMap,为了区分各表字段名的不同—Mybatis的强大之处)
写resultMap的时候要采用步进的方式,检查出错的地方
对一时的写法:

<select id="findOne" resultMap="blogResult">
        SELECT b.id blog_id, b.title
        blog_title,
        a.id author_id, a.name author_name
        FROM blog b inner join
        author a ON b.author_id = a.id
        where b.id = #{id}
</select>

<resultMap type="Blog" id="blogResult">
        <id property="id" column="blog_id" />
        <result property="title" column="blog_title" />
        <association property="author" javaType="Author">
            <id property="id" column="author_id" />
            <result property="name" column="author_name" />
        </association>
</resultMap>

对多时的写法:

<select id="findOneWithPosts" resultMap="blogWithPostResult">
        SELECT b.id blog_id, b.title blog_title,
        a.id author_id, a.name author_name,
        p.id post_id,p.subject post_subject
        FROM blog b inner join
        author a ON b.author_id = a.id
        left join post p ON p.blog_id = b.id
        where b.id = #{id}
</select>

    <resultMap type="Blog" id="blogWithPostResult">
        <id property="id" column="blog_id" />
        <result property="title" column="blog_title" />
        <association property="author" javaType="Author">
            <id property="id" column="author_id" />
            <result property="name" column="author_name" />
        </association>
        <collection property="post" ofType="Post">
            <id property="id" column="post_id"/>
            <result property="subject" column="post_subject"/>
        </collection>
</resultMap>

association:对一时使用的关键字
collection:对多时使用的关键字
JavaType:明确指定java类型
ofType:使用collection时使用,指定元素类型,符合英语语法规则

内联接:(等值连接 )inner join (典型的连接运算,使用像=或<>之类的比较运算符)包括相等连接和自然连接

外联接:
1.left join/left outer join 包括左表所有行,若是左表某行在右表没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值
2.right join/right outer join 返回右表所有的行,若是右表的某行在左表中没有匹配行,则将左表返回空值
3.full join/full outer join 完整外部链接返回左表和右表中的所有行,当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值;如果表之间有匹配行,则整个结果集行包含基表的数据值

交叉联接:返回左表中所有的行,左表中的灭一行与右表中的所有行组合,也称作笛卡尔积

+表示可以为空

【代码优化结果映射】
将相同的提取出来,使用resultType关键字,一定要完全相同,例如之前两个查询可以简化

<resultMap ty
        pe="Blog" id="blogResult">
        <id property="id" column="blog_id" />
        <result property="title" column="blog_title" />
        <association property="author" resultMap="authorResult">
        </association>
</resultMap>

对多时的写法:

<resultMap type="Blog" id="blogWithPostResult">
            <id property="id" column="blog_id" />
        <result property="title" column="blog_title" />
        <association property="author" resultMap="authorResult">
        </association>
        <collection property="post" ofType="Post">
            <id property="id" column="post_id" />
            <result property="subject" column="post_subject" />
        </collection>
</resultMap>

都有相同的一个author,可以提取出来:

<resultMap type="Author" id="authorResult">
        <id property="id" column="author_id" />
        <result property="name" column="author_name" />
</resultMap>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值