MyBatis的动态语句使用

本文介绍了MyBatis中动态SQL的应用技巧,包括动态查询、批量操作、参数传递及SQL语句复用等,帮助开发者灵活高效地处理复杂查询场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载来自:http://blog.youkuaiyun.com/u013360850/article/details/53121967

1. 动态查询语句

  • 可以根据传入的bean来判断,动态的拼接SQL语句

  • SQL语句

    <select id="findUserInfo" parameterType="UserInfo" resultType="UserInfo">
        select * from userinfo
        <where>
            <if test="id!=0">
                and id = #{id}
            </if>
            <if test="name!=null and name!=''">
                and name like #{name}
            </if>
            <if test="address!=null and address!=''">
                and address like #{address}
            </if>
        </where>
    </select>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • Java语句
        SqlSession session = MyBatisUtil.openSession();
        UserInfoDao dao = session.getMapper(UserInfoDao.class);

        UserInfo u = new UserInfo();
        u.setId(4);
        u.setName("%a%");
        u.setAddress("%c%");

        session.commit();
        session.close();
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

此处的Like语句如果用#,需要在传入的参数中设置%,如果用的是$,则需要在SQL语句中写'%#{param}%'

  • 使用#
    SQL:and name like #{name}
    Java:u.setName("%a%");
 
  • 1
  • 2
  • 1
  • 2
  • 使用$
    SQL:name like '%${name}%'
    Java:u.setName("a");
 
  • 1
  • 2
  • 1
  • 2

2. 传入多个参数

  • 传入多个参数时需要用集合或数组进行传值

  • SQL

    <select id="findUserInfoByIdArray" resultType="UserInfo">
        select * from userinfo where id in
        <!-- item名称没有限制,和取值的属性一致即可 -->
        <!--  collection为传入的参数类型,可以为list或array,open为需要拼接的开始符号,separator为分割符号,close为结束符号 -->
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • Java
        SqlSession session = MyBatisUtil.openSession();
        UserInfoDao dao = session.getMapper(UserInfoDao.class);

        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        ids.add(3);
        List<UserInfo> list = dao.findUserInfoByIds(ids);

        session.close();
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3. SQL语句复用

  • 被复用的SQL语句
<!-- SQL语句复用 -->
    <sql id="ids">
        where id in
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </sql>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 使用
    <select id="findUserInfoByIds" resultType="UserInfo">
        select * from userinfo
        <!--refid为被复用的sql语句ID-->
        <include refid="ids" />
    </select>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

4. 批量操作

  • 使用foreach循环实现批量操作
  • SQL
    <insert id="insertBatch" parameterType="java.util.List">
        insert into userinfo(id,name,address)
        values
        <foreach collection="list" item="data" separator=",">
            (#{data.id},#{data.name},#{data.address})
            </foreach>
    </insert>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • Java
        SqlSession session = MyBatisUtil.openSession();
        UserInfoDao dao = session.getMapper(UserInfoDao.class);

        List<UserInfo> list = new ArrayList<UserInfo>();

        UserInfo u1 = new UserInfo(5,"c5","dalian");
        UserInfo u2 = new UserInfo(6,"c6","dalian");
        UserInfo u3 = new UserInfo(7,"c7","dalian");
        UserInfo u4 = new UserInfo(8,"c8","dalian");
        UserInfo u5 = new UserInfo(9,"c9","dalian");

        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);
        list.add(u5);

        dao.insertBath(list);
        session.commit();
        session.close();
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

5. 动态的拼接更新语句

  • 通过判断条件来动态的拼接更新语句
  • SQL
    <update id="updateUserInfo" parameterType="UserInfo">
        update userinfo
        <set>
            <if test="name!=null and name!=''">
                name=#{name},
            </if>
            <if test="address!=null and name!=''">
                address=#{address},
            </if>
        </set>
        where id=#{id}
    </update>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • Java
        SqlSession session = MyBatisUtil.openSession();
        UserInfoDao dao = session.getMapper(UserInfoDao.class);

        //此处不能所有值都为空,否则SQL语句没有更新条件会有异常
        UserInfo u = new UserInfo(4, "啦啦啦", null);
        dao.updateUserInfo(u);

        session.commit();
        session.close();


以下内容   转自Clement-Xu的csdn博客:http://blog.youkuaiyun.com/clementad/article/details/55099432

有时候需要简单地把一个Map中所有的key和value获取出来,拼到sql语句中。MyBatis提供的一种方法是遍历Map中的entrySet,然后把key扔进index里面,value扔进item中。具体的一个使用的例子如下:

[html]  view plain  copy
  1. <insert id="operationName" parameterType="map">  
  2.     INSERT INTO table_name(hot_word, cnt)  
  3.     VALUES  
  4.     <foreach item="value" index="key" collection="mapData.entrySet()" open="(" separator="),(" close=")">  
  5.         #{key}, #{value}  
  6.     </foreach>  
  7.     ON DUPLICATE KEY UPDATE  
  8.     cnt=VALUES(cnt)  
  9. </insert>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值