Mybatis(五 1)动态SQL

根据业务不同,编写的sql语句不会是简单的select * from xxx。很多时候是多条件的查询,同时以前在编写jdbc代码时要进行输入参数的判断,防止输入参数为空,导致sql语句执行失败,等等之类的情况,第五节就将描述如何在mybatis中是实现这些判空,拼接等操作。

 

select直接写法:

select * from flower where name = #{param1} and production = #{param2}

问题:如果输入的参数为空,如果进行判空

一、输入条件的判断,<if test = “”></if>

传入参数name, production  
List<Flower>  selectMore(String  name,String  production);

mapper.xml文件中:

<mapper namespace="com.bjsxt.mapper.FlowerMapper">

    <!--if标签
       if(test){..}
       if(test){..}
    -->
    <select id="selectMore" resultType="flower">
        SELECT  *  from   flower  where
        <!--OGNL表达式-->
<!--只有当第一个参数param1满足非空且不等于空字符串 if标签里面的sql语句才会被拼接进去-->
        <if test="param1!=null and param1!=''">
            name=#{param1}
        </if>
        <if test="param2!=null and param2!=''">
            and  production=#{param2}
        </if>
    </select>
 </mapper>

按照上述写法,假设输入两个参数均为空,那么sql就变成了
select * from flower where 
语句错误,提出改进如下,写为where 1=1


<mapper namespace="com.bjsxt.mapper.FlowerMapper">

    <!--if标签
       if(test){..}
       if(test){..}
    -->
    <select id="selectMore" resultType="flower">
        SELECT  *  from   flower  where  1=1
        <!--OGNL表达式-->
<!--只有当第一个参数param1满足非空且不等于空字符串 if标签里面的sql语句才会被拼接进去-->
        <if test="param1!=null and param1!=''">
            and  name=#{param1}
        </if>
        <if test="param2!=null and param2!=''">
            and  production=#{param2}
        </if>
    </select>
 </mapper>

上面的例子引出了sql动态拼接,其实就是使用一些标签加以规范mapper里面的sql写法完成sql拼接

对于上面例子中的where的解决方案,并不是mybatis所提倡的,mybatis提供了where标签来改进这个问题:

<!--Where标签的作用:会自动的增加where关键字,并且会把多余的第一个and去掉-->
    <select id="selectMore2" resultType="flower">

        SELECT  *  from   flower
        <!--OGNL表达式-->
            <where>
                    <if test="param1!=null and param1!=''">
                        <!--第一个条件前的and可加可不加-->
                        name=#{param1}
                    </if>

                    <if test="param2!=null and param2!=''">
                        and  production=#{param2}
                    </if>
            </where>
    </select>

下面介绍在where标签中使用的另外一组标签choose  when

<!--
        if(){..}
        else if(){..}
        else if(){..}
        else {}
    -->

<select id="selectMore4" resultType="flower">

        SELECT  *  from  flower

         <where>
                <choose>

                     <when test="param1!=null and param1!=''">
                           name=#{param1}
                     </when>

                     <when test="param2!=null and param2!=''">
                          and  production=#{param2}
                     </when>
                     <otherwise>
                         1=1
                     </otherwise>

                </choose>

         </where>

    </select>

那么这种写法跟上面的ififif的区别就是,这种写法更类似于if else ,只找到满足条件的一个语句拼接上去

SqlSession sqlSession = factory.openSession(true);
//[4]执行方法
FlowerMapper mapper = sqlSession.getMapper(FlowerMapper.class);

情况一:
List<Flower> list = mapper.selectMore4("玫瑰花","");
情况二:
List<Flower> list = mapper.selectMore4("玫瑰花","中国");

情况一和情况二的日志输出相同:

 这个结果就表明,当找到了一个满足情况的sql后,后续的sql均不会被拼接上去

那么如果所有条件都不满足条件呢?就要额外添加一个otherwise标签

作用:where 1 = 1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
mybatis批量更新动态sql可以使用foreach标签来实现。在动态sql中,可以使用foreach标签来遍历一个集合,并对集合中的每个元素执行相同的sql语句。具体操作步骤如下: 1. 定义一个集合对象,用于存储要批量更新的数据。 2. 在mapper.xml文件中,使用<foreach>标签包裹要执行的sql语句。例如,对于Oracle 12C环境下的批量入库方式,可以按照以下格式编写sql语句: <insert id="insertSysUserLoanGroups" useGeneratedKeys="false"> insert into SYS_USER_LOAN_GROUP (USER_ID,GROUP_ID) ( <foreach item="item" index="index" collection="list" separator="UNION ALL"> select #{item.userId}, #{item.groupId} from dual </foreach> ) </insert> 在上述示例中,<foreach>标签中的collection属性指定了要遍历的集合对象,item属性指定了集合中的每个元素的别名,index属性指定了循环的索引变量名,separator属性指定了每个循环体之间的分隔符。 这样,通过foreach标签,可以将集合中的每个元素转换为一条insert语句的一部分,从而实现批量插入的功能。 3. 在Java代码中,使用mybatisSqlSession对象执行上述定义的批量更新操作。 通过调用SqlSession对象的insert方法,并指定要执行的sql语句的id(即insertSysUserLoanGroups),以及要传递给sql语句的参数,即定义的集合对象。 例如,可以按照以下方式执行批量更新操作: ``` List<UserLoanGroup> dataList = new ArrayList<>(); // 向dataList中添加要批量插入的数据 sqlSession.insert("insertSysUserLoanGroups", dataList); ``` 在上述示例中,insertSysUserLoanGroups是mapper.xml文件中定义的要执行的sql语句的id,dataList是要传递给sql语句的参数。 这样,就可以通过mybatis的foreach标签和SqlSession对象来实现mybatis批量更新动态sql的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Meikesibondwell

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值