MyBatis中的动态SQL

一.MyBatis中的动态SQL

1.if标签

根据条件,构建查询条件

<if test="属性判断">
    and 属性=#{属性}
</if>
    

举例:

    <select id="select" resultType="studentInfo">
        select * from studentinfo where 1=1//where1=1是为了保证sql完整性
        <if test="id!=null">
            and id=#{id}
        </if>
        <if test="age!=null">
            and age=#{age}
        </if>
        <if test="name!=null">
            and name=#{name}
        </if>
        <if test="score!=null">
            and score=#{score}
        </if>
    </select>

2.where标签

上述例子中 有一句where 1=1,也可以不用去拼接,但此时需要用<where></where><if></if>包裹起来。

3.foreach标签

   <delete id="deleteId" parameterType="list">
        delete from studentinfo
        <where>
            <foreach collection="array" item="id" open="id in (" close=")" separator=",">
                #{id}
            </foreach>
        </where>
    </delete>

<foreach>标签 parameterType不可以是Array/array
collection:遍历的对象,数组->array,集合->list,map->对应的key
item:给遍历的对象取个名字,通过它来取值
open:语句的开始
close:语句的结束

4.trim标签

去除多余的AND,逗号,或者拼接SQL

  <select id="selectMany" parameterType="studentInfo" resultType="studentInfo">
      
        select * from studentinfo
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="id!=null">
                and id=#{id}
            </if>
            <if test="age!=null">
                and age=#{age}
            </if>
            <if test="name!=null">
                and name=#{name}
            </if>
            <if test="score!=null">
                and score=#{score}
            </if>
        </trim>
    </select>

prefix:给SQL语句(给子标签中的语句)的前面拼接

prefixOverrides:去除SQL语句(给子标签中的语句)前面的指定字符

5.set标签

如果子标签有内容,自动添加set,并删除最后一个逗号

<update id="updateByUser" parameterType="UserInfo">
        update userinfo
<set>
       <if test="username != null">
              username = #{username},
         </if>
         <if test="name != null">
            name = #{name},
         </if>
          where id = #{id}
   </set>
 </update>

6.choose标签

没有语句和when匹配时,执行 <otherwise> </otherwise>标签里面的,满足一个条件即可

 <select id="select1" resultType="studentInfo">
        select * from studentinfo
        <where>
            <choose>
                <when test="id != null" >
                    and id=#{id}
                </when>
                <when test="age != null" >
                    and age=#{age}
                </when>
        <otherwise>

        </otherwise>
            </choose>
        </where>
    </select>

二.简化SQL

1.定义代码片段

<sql id="default">
    id,name,password
    </sql>//抽取重复的语句代码片段

2.引用代码片段

<include refid="与id的值对应"></include>

三.分页插件

PageHelper:第三方分页助手

开发步骤:(1).导入PageHelper依赖

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.2</version>
</dependency>

(2)在mybatis配置文件中配置pagehelper


<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 指定方言 -->
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
使用

在要用的地方:PageHelper.startpage(第几页,每页条数)

@Test
public void selectAll() {
PageHelper.startPage(1, 2);
List<UserInfo> userInfos = mapper.selectAll();
userInfos.forEach(u -> System.out.println(u));
}
其他信息
PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfos);
System.out.println("是否是第一页" + pageInfo.isIsFirstPage());
System.out.println("是否有下一页: " + pageInfo.isHasNextPage());
System.out.println("是否是最后一页:" + pageInfo.isIsLastPage());
System.out.println("是否有上一页:" + pageInfo.isHasPreviousPage());
System.out.println("当前是第几页:" + pageInfo.getPageNum());
System.out.println("页大小:" + pageInfo.getPageSize());
System.out.println("共多少条数据:" + pageInfo.getTotal());
System.out.println("总共多少页: " + pageInfo.getPages());
System.out.println("下一页: " + pageInfo.getNextPage());
System.out.println("上一页: " + pageInfo.getPrePage());	
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值