MyBatis动态SQL,分页查询,List/Array/Map查询.


  • 动态sql

if

<select id="selectAll" parameterType="com.yoona.dao.doctor"
            resultType"BaseResultMap">
        select
        *
        from doctor
        where 
            <if test="age != null">
                age = #{age,jdbcType=VARCHAR}
            </if>
            <if test="id != null">
                AND id = #{id,jdbcType=VARCHAR}
            </if>
            <if test="name != null">
                AND name = #{name,jdbcType=CHAR}
            </if>
        </where>
</select>

上述写法是一种最简单的动态sql.<if>语句.但是设想这样一种情况,当条件一条也没匹配上时,语句就变为了

select * from doctor where

 

很明显,这是条错误的语句.但是仅仅匹配到了第二个或第二个以后的语句呢?sql语句就变为

select * from doctor
where
  and id = "***"

 

也会查询失败.只需要将查询条件放在<where>标签内,最好的写法为

​<select id="selectAll" parameterType="com.yoona.dao.doctor"
            resultMap="BaseResultMap">
        select
        *
        from doctor
        <where>
            <if test="age != null">
                age = #{age,jdbcType=VARCHAR}
            </if>
            <if test="id != null">
                AND id = #{id,jdbcType=VARCHAR}
            </if>
            <if test="name != null">
                AND name = #{name,jdbcType=CHAR}
            </if>
        </where>
</select>
​

where 元素知道只有在一个以上的if条件有值的情况下才去插入"where"子句.即使第一个条件 age 为空,也不会报错.

choose, when, otherwise

<select id="select" paramType="com.yoona.dao.doctor" resultType="BaseResultMap">
  select * from daoctor
<where>
  <choose>
    <when test="id != null">
       id=#{id}
    </when>
    <when test="name != null">
      and name=#{name}
    </when>
    <otherwise>
      AND age = 30
    </otherwise>
  </choose>
</where>
</select>

 

这样,提供了"id"就按"id"查找,提供了"name"就按"name"查找,若两者都没有提供,就返回所有符合 age=30 条件的结果.


  •  分页查询

方法有很多,可以看https://blog.csdn.net/chenbaige/article/details/70846902的步骤,已经写的很详细.我与他的方法不同:

首先实体类继承一个基类.

​实体类中创建基类BaseEntity
public BaseEntity() {
        HttpServletRequest request = ((ServletRequestAttributes) 
                           RequestContextHolder.currentRequestAttributes()).getRequest();
        String pageSizeStr = request.getParameter("pageSize");
        String pageNoStr = request.getParameter("pageNo");

        if (StringUtils.isNotBlank(pageSizeStr)) {
            int size = Integer.parseInt(pageSizeStr);
            this.pageSize = size > 0 ? size : PAGE_SIZE;

        } else {
            this.pageSize = PAGE_SIZE;
        }

        if (StringUtils.isNotBlank(pageNoStr)) {
            int number = Integer.parseInt(pageNoStr);
            this.pageNo = number > 0 ? number - 1 : PAGE_NO;
        } else {
            this.pageNo = PAGE_NO;
        }

        this.beginNo = pageNo * pageSize;
    }

}

需要分页查询的使其实体类继承BaseEntity,实体类就会含有pageNo(页码),pageSize(每页条数),beginNo(查询起始条数)属性.

           controller层传入的参数需要含有pageNo,pageSize属性.

xml中代码

<select id="selectLimit"  parameterType="com.***.***.entity.Doctor" 
    resultMap="BaseResultMap">
    select *
    from doctor
        <where>
            **************
        </where>
        limit #{beginNo},#{pageSize}
</select>

需要注意的是 limit #{beginNo},#{pageSize}中的值名称要与基类中属性名一致.

  • List/Array/Map查询 

主要就是对 <foreatch> 标签的使用.

​xml中代码

<select id="selectList" parameterType="com.yoona.dao.doctor" 
    resultMap="BaseResultMap">
    select * from doctor
    <where>
        id in
            <foreach collection="Ids" item="id" index="index" open="(" close=")" 
                     separator=",">
                #{id}
            </foreach>
    </where>
</select>

实体类中含有属性Ids属性(数据库中很可能无此字段,可以创建新实体类DoctorNew继承Doctor实体类,这样 parameterType="com.***.***.***.DoctorNew").也可以直接在反向生成的实体类直接加上此字段使用.
需要注意的是,当传递的List(或Array,Map)为空时,SQL语句报错,所以传递前最好判断容器内容.

foreach元素的属性主要有(item,index,collection,open,separator,close).
item表示集合中每一个元素进行迭代时的别名
index指定一个名字,用于表示在迭代过程中,每次迭代到的位置
open表示该语句以什么开始
separator表示在每次进行迭代之间以什么符号作为分隔符
close表示以什么结束
collection表示传入参数属性.该属性是必须指定的, 在不同情况下,该属性的值是不一样的,主要有一下3种情况: 
    1.如果传入的参数是单参数且参数类型是一个List的时候,collection属性值为list .
    2.如果传入的参数是单参数且参数类型是一个Array数组的时候,collection的属性值为array .
    3.如果传入的参数是多个的时候,可以封装成一个Map,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
​

​

当传入参数集合已知,并不推荐使用 <foreach> 标签,比较消耗性能,查询数据量过大,耗费时间会过长.

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值