Mybatis别名 动态sql语句 分页查询

给Mybatis的实体类起别名

给Mybatis的xml文件注册mapper映射文件

动态sql语句

        1 if

        2 choose

         3 where

        4 foreach

一)if

        查询指定名称商品信息

        语法:

        SELECT * FROM goods     

        where  1=1

                <if test ="gName!=null">

                and g.g_name like concat('%',#{gName},'%')

                </if>

         test中添加判断条件,结果为true内容拼接到where语句中。

         test中形参不用加#{}

         注意没有if else,但可以使用多个if并列,但每个if都要判断拼接

二)choose:从多个分支中选择一个执行 等同if else

        语法:

       SELECT * FROM goods

        where 1=1

         <choose>

        <when test = "gName!=null">

                and g.gname like concat('%',#{gName},'%')

        </when>

        <when test = "gPrice!=null">

                and  g.gPrice<=#{gPrice}

        </when>

        </choose>

        choose语句只会选择唯一一个执行,谁先满足条件先执行谁

        没有满足条件的可以添加<otherwise></otherwise>标签执行拼接内部sql语句。

三)where标签 --->trim(自适应where标签,可以自定义删除关键词)

        语法

        where元素只会在子元素返回任何内容情况下才插入<where>标签

        只有在子元素(如 <if> 标签)返回了具体的内容或结果时,才会在生成的 SQL 中插入 <where> 标签及其内容

        拼接的sql语句开头有and  or  ,where标签也会正确的去除

       SELECT * FROM user

<where>

        <if test="id != null">

                AND id = #{id}

        </if>

        <if test="name != null">

                AND name = #{name}

        </if>

</where>

四)foreach

<delete id="deleteByList">
    delete from goods
    where g_id in
          <foreach item="item" collection="list"
              open = "(" separator="," close=")">
          #{item}
          </foreach>
</delete>

list为传入的形参,

所以在对应mapper接口方法形参为List类型的list

或者写成@Param("list") List list

<delete id="deleteByArray">
    delete from goods
    where g_id in
          <foreach item="item" index="index" collection="array"
              open = "(" separator="," close=")">
          #{item}
          </foreach>
</delete>

Array为传入的形参,

所以在对应mapper接口方法形参为Integer [] 类型的array

或者写成@Param("array") Integer [] array

 分页查询

        1在sql语句中添加limit关键词

                主要数据:pageIndex:当前页码,pageCount:每页的数量

select *
from goods
limit #{index},#{count}

         使用pagehelper的jar工具包进行分页查询

 

            GoodsMapper mapper = SqlSessionUtil.getMapper(GoodsMapper.class);
            PageHelper.startPage(index, count,true,true,true);
            List<Goods> goodsList = mapper.getAll();
            PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsList);
            SqlSessionUtil.commitSession();
            PageInfo<Goods> goodsByPage = goodsService.getGoodsByPage(1, 3);

        int pages = goodsByPage.getPages();
        long total = goodsByPage.getTotal();
        int pageNum = goodsByPage.getPageNum();
        int size = goodsByPage.getSize();

        System.out.println("总页数"+pages);
        System.out.println("总记录数据条数"+total);
        System.out.println("第几页"+pageNum);
        System.out.println("当前页记录数据数"+size);
           

 pagehelper的几种重载方法

  startPage(int pageNum, int pageSize)
  startPage(int pageNum, int pageSize, boolean count)
  startPage(int pageNum, int pageSize, boolean count, Boolean reasonable, Boolean pageSizeZero) 
  startPage(int pageNum, int pageSize, String orderBy)

  1. startPage(int pageNum, int pageSize): 这个方法指定了要查询的页数和每页的数据量。它用于简单的分页查询。

  2. startPage(int pageNum, int pageSize, boolean count): 这个方法在第二个参数的基础上增加了一个 boolean 类型的参数,用于指示是否进行 count 查询。count 查询是为了获取总记录数,以便于计算总页数。如果传入 true,则会进行 count 查询;如果传入 false,则不进行 count 查询。

  3. startPage(int pageNum, int pageSize, boolean count, Boolean reasonable, Boolean pageSizeZero): 这个方法除了包含前两个参数外,还可以指定是否启用 "reasonable" 和 "pageSizeZero" 的设置。reasonable 设置表示是否启用合理化参数,默认为 false,表示不启用;pageSizeZero 表示当 pageSize 为 0 时是否返回全部结果,默认为 false,表示不返回全部结果。

  4. startPage(int pageNum, int pageSize, String orderBy): 这个方法还包含一个 orderBy 参数,用于指定结果集的排序方式。orderBy 参数是一个字符串,表示按照哪个字段进行排序,可以包含 ASC(升序)或 DESC(降序)等排序规则。

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值