动态SQL

XML文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--=================================================================================================================-->
<!--动态SQL-->
<mapper namespace="com.itheima.mapper.EmpMapper">


<!--    更新一个或几个字段-->
    <update id="update2" >
        update emp
<!--            <if>用于判断条件是否成立,如果条件为true,则拼接SQL语句(<if test="name != null ">...</if>)</if>-->
<!--        <set>自动除去多余的逗号,</set>-->
        <set>
            <if test="username != null">username=#{username},</if>
            <if test="name != null">name=#{name},</if>
            <if test="gender != null"> gender=#{gender},</if>
           <if test="image != null"> image=#{image},</if>
           <if test="job"> job=#{job},</if>
           <if test="entrydate">  entrydate=#{entrydate},</if>
          <if test="deptId"> dept_id=#{deptId},</if>
           <if test="updateTime">update_time=#{updateTime}</if>
        </set>
        where id = #{id}
    </update>


<!--  批量删除  -->
    <delete id="deleteByIds">
        delete from emp where id in
<!--<foreach collection="遍历的集合" item="遍历出来的元素" separator="分隔符" open="遍历开始前拼接的SQL片段" close="遍历拼接的SQL字段"></foreach>-->
                             <foreach collection="ids" item="id" separator="," open="(" close=")">#{id}</foreach>
    </delete>


<!--    SQL动态查询,只用一个或多个条件查询-->
    <select id="list" resultType="com.itheima.pojo.Emp">
    select * from emp
<!--<where>自动去除条件中多余的and和or</where>-->
             <where>
    <if test="name != null">
        name like concat('%',#{name},'%')
    </if>
    <if test="gender != null">
      and  gender=#{gender}
    </if>
    <if test="begin != null and end != null">
     and   entrydate between #{begin} and #{end}
    </if>
             </where>
    order by update_time desc
</select>


    <!--  sql和include的使用-->

<!--    <sql id="定义可重用的SQL片段"></sql>-->
    <sql id="commonSelect">
        select id,username,password, name, gender, image, job, entrydate, dept_id, create_time, update_time
        from emp
    </sql>

<!--include通过属性refid引用上面<sql></sql>中包含的sql片段-->
    <select id="list1" resultType="com.itheima.pojo.Emp">
      <include refid="commonSelect"></include>
        <where>
            <if test="name != null">
                name like concat('%',#{name},'%')
            </if>
            <if test="gender != null">
                and  gender=#{gender}
            </if>
            <if test="begin != null and end != null">
                and   entrydate between #{begin} and #{end}
            </if>
        </where>
        order by update_time desc
    </select>


</mapper>

=========================================================================

Mapper类文件:

//SQL动态查询,只用一个或多个条件查询
    public List<Emp> list(String name, Short gender, LocalDate begin,LocalDate end);

//sql和include的使用
    public List<Emp> list1(String name, Short gender, LocalDate begin,LocalDate end);

    //更新一个或几个字段
    public  void update2(Emp emp);

//批量删除
 public void deleteByIds(List<Integer> ids);

=========================================================================

测试类SpringbootMybatisCrudApplicationTests:

    //SQL动态查询,只用一个或多个条件查询
    @Test
    public void testList(){
//     List<Emp> empList= empMapper.list("张",(short)1,LocalDate.of(2010,1,1),LocalDate.of(2020,1,1));
//    List<Emp> empList= empMapper.list("张",null,null,null);
//    List<Emp> empList= empMapper.list("张",(short)1,null,null);
//    List<Emp> empList= empMapper.list(null,(short)1,null,null);
    List<Emp> empList= empMapper.list(null,null,null,null);
    System.out.println(empList);
    }

    //更新一个或几个字段
    @Test
    public void testUpdate2(){
        Emp emp=new Emp();
        emp.setId(19);
        emp.setUsername("Ailisi1");
        emp.setName("爱丽丝1");
        emp.setGender((short)2);
        emp.setUpdateTime(LocalDateTime.now());

        empMapper.update2(emp);

    }

    //批量删除
@Test
    public void testDeleteByIds(){
        List<Integer> ids= Arrays.asList(18,19);
        empMapper.deleteByIds(ids);
}

    //sql和include的使用
    @Test
    public void testList1(){
        List<Emp> empList= empMapper.list1("张",null,null,null);
        System.out.println(empList);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值