MyBatis扩展之动态sql

佞言者,谄而于忠;谀言者,博而于智


前言

mybatis内容扩展的真多…


一、动态sql

可以定义代码片段,可以进行逻辑判断,可以进行循环处理(批量处理)
使条件判断更为简单

1.1sql标签:

用来定义代码片段,可以将所有的列名,或复杂的条件定义为代码片段,供使用时调用

    <!--    定义代码片段-->
    <sql id="allColumns">
        id,username,birthday,sex,address
    </sql>

1.2 include标签:

用来引用sql标签定义的代码片段

    <select id="getById" parameterType="int" resultType="users">
        select <include refid="allColumns"></include>
        from users
        where id=#{id}
    </select>

1.3. if标签:进行条件判断

1.4. where标签:进行多条件拼接,在查询,删除,更新中使用

<select id="getByCondition" parameterType="users" resultType="users">
        select <include refid="allColumns"></include>
        from users
        <where>
            <if test="userName != null and userName != ''">
                and username like concat('%',#{userName},'%')
            </if>
            <if test="birthday != null">
                and birthday = #{birthday}
            </if>
            <if test="sex != null and sex != ''">
                and sex = #{sex}
            </if>
            <if test="address != null and address != ''">
                and address like concat('%',#{address},'%')
            </if>
        </where>
    </select>

用where if标签可以动态的进行模糊查询,如下:

    @Test
    public void testGetByCondition() throws ParseException {
        Users u = new Users();
        u.setSex("1");
        u.setUserName("小");
        u.setAddress("市");
        //u.setBirthday(sf.parse("2001-01-01"));
        List<Users> list = usersMapper.getByCondition(u);
        list.forEach(users -> System.out.println(users));
    }

1.5. set标签:

有选择的进行更新处理,至少更新一列
没有给值的情况,保持不变,但是至少更新一列
实例:

<update id="updateBySet" parameterType="users">
        update users
        <set>
            <if test="userName != null and userName!=''">
                username = #{userName},
            </if>
            <if test="birthday !=null">
                birthday = #{birthday},
            </if>
            <if test="sex != null and sex != ''">
                sex = #{sex},
            </if>
            <if test="address != null and address != ''">
                address = #{address},
            </if>
        </set>
        where id = #{id}
    </update>

1.6. foreach标签:

用来进行循环遍历,完成循环条件查询,批量删除,批量增加,批量更新
例如:

    <!--
        //查询指定多个id的用户信息
        List<Users> getByIds(Integer []arr);
        <foreach>中的collection是类型item是名称separator属性是分隔符
    -->
    <select id="getByIds" resultType="users">
        select <include refid="allColumns"></include>
        from users
        where id in
            <foreach collection="array" item="id" separator="," open="(" close=")">
                #{id}
            </foreach>

    </select>

参数:
collection:用来指定入参的类型,如果是List集合,则为list,如果是Map集合,则为map,如果是数组,则为array(最常见的三类型).
item:每次循环遍历出来的值或对象
separator:多个对象或对象或语句之间的分隔符
open:整个循环外的前括号
close:整个循环外的后括号

二、指定参数下标位置查询

如果入参是多个,可以通过指定参数位置进行传参,是实体类包含不住的条件。实体类只能封装住成员变量的条件。如果某个成员变量要有区间范围内的判断,或者有两个值进行处理,则实体类包不住。

例如:查询指定日期范围内的用户信息(也可以用@Param)

    <!--
        //查询指定日期范围内的用户
        List<Users> getByBirthday(Date begin,Date end);
    -->
    <select id="getByBirthday" resultType="users">
        select <include refid="allColumns"></include>
        from users
        where birthday between #{arg0} and #{arg1}
    </select>

arg0是第一个参数,arg1是第二个参数
此法中arg0和1是系统设定的参数名
而在@Param中相当于是自定义参数名

三、入参是Map

如果入参超过一个以上,使用map封装查询条件(KV)
更有语义,查询条件更明确

如下:

    <!--
        //入参是map(实体类无法封装所有查询条件)
        List<Users> getByMap(Map map);
        #{birthdayBegin}:就是map中的key
    -->
    <select id="getByMap" resultType="users">
        select <include refid="allColumns"></include>
        from users
        where birthday between #{birthdayBegin} and #{birthdayEnd}
    </select>

测试类:

    @Test
    public void testGetByMap() throws ParseException {
        Date begin = sf.parse("1999-01-01");
        Date end = sf.parse("1999-12-31");
        Map map = new HashMap<>();
        map.put("birthdayBegin",begin);
        map.put("birthdayEnd",end);
        List<Users> list = usersMapper.getByMap(map);
        list.forEach(users -> System.out.println(users));
    }

四、返回值是map

如果返回的数据实体类无法包含,可以使用map返回多张表中的若干数据
返回这些数据之间没有任何关系。就是Object类型,返回的map的key就是列名或别名
如下:

    <!--
        //返回值是map(一行)
        Map getReturnMap(Integer id);
    -->
    <select id="getReturnMap" parameterType="int" resultType="map">
        select username name,address a
        from users
        where id = #{id}
    </select>

    <!--
        //返回多行的map
        List<Map> getMulMap();
    -->
    <select id="getMulMap" resultType="map">
        select username,address
        from users
    </select>

测试如下:

    @Test
    public void testReturnMap(){
        Map map = usersMapper.getReturnMap(3);
        System.out.println(map);
        System.out.println(map.get("username"));
    }

    @Test
    public void testReturnMapMul(){
        List<Map> list = usersMapper.getMulMap();
        list.forEach(map -> System.out.println(map));
    }

总结

mybatis的配置标签动态用法
看看代码就行
都有注解

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值