【Mybatis09】动态sql

1.动态sql

1.1if语句

接口映射文件中,多条件查询时,有可能因为传递的参数不对,导致异常

比如:如果你传递的username为null怎么办?为" "怎么办?

	<!--
        多条件查询
        如果传递参数个数不对,会出现异常,动态sql可以解决
    -->
    <select id="findByCondition" resultMap="users" parameterType="user">
        select * from user where uname like "%"#{username}"%"  and sex = #{sex}
    </select>

使用动态sql解决,动态sql可以加一些判断。

	<!--
        多条件查询
    -->
  <select id="findByCondition" resultMap="users" parameterType="user">-->
       select * from user where 1=1
        <if test="username != null">
            and uname like "%"#{username}"%"
        </if>
        <if test="sex != null">
            and sex = #{sex}
        </if>
  </select>

1.2where语句

	<!--
        where : 可以自动去掉条件中的第一个and
            多条件 都写上  and关键字
    -->
    <select id="findByCondition" resultMap="users" parameterType="user">
        select * from user
        <where>
            <if test="username != null">
                and uname like "%"#{username}"%"
            </if>
            <if test="sex != null">
                and sex = #{sex}
            </if>
        </where>
    </select>

1.3sql片段
如果很多statement动态sql部分都一样,可以抽取为sql片段,然后在statement中引用即可。

<!--SQL片段
        把重复的sql语句提取出来,需要使用时引用即可

        id="": 唯一标志
        文本:sql语句
    -->
    <sql id="select_user">select * from user</sql>

    <!--
        where : 帮助程序员处理第一个and
            多条件 都写上  and关键字
    -->
    <select id="findByCondition" resultMap="users" parameterType="user">
--       关联使用sql片段
--         include :包含
--         refid: 关联的sql片段的id。如果指定的id不在本映射文件中,需要在前面加namespace
--         ref :references
        <include refid="select_user"></include>
        <where>
            <if test="username != null">
                and uname like "%"#{username}"%"
            </if>
            <if test="sex != null">
                and sex = #{sex}
            </if>
        </where>
    </select>

1.4foreach语句
如果要向sql传递数组或list,则使用foreach语句
可以将查询改为多个id查询,比如:

select*from user where id=1 or id=12 or id=17或
select*from user where id in(1,12,17)

多个id要作为参数传进来,所以可以在自定义一个pojo中增加一个属性:private List ids;
然后修改sql片段:

  <if test="ids!=null and ids!=' ' ">
    	<foreach collection="ids" item=user_id open="AND(" close=")" separator=" OR ">
    		id=#{user_id}
    	</foreach>
    <if>

所以这一部分拼接出来就是AND(id=1 OR id=12 OR id=17)
上面的属性:

  • collection: 参数的类型:如果是集合:list,如果是数组: array
  • open :前缀
  • close:后缀
  • separator: 分隔符
  • item: 循环中的每一个对象
  • index:循环中的索引( 一般不用)
    补充:
    在mybatis中,如果输入的是Integer或int类型的0,上面<if>标签返回的是false,也就是说即使非null非" ",也不会拼接sql
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一个流行的Java持久层框架,它允许开发者在运行时构建动态SQL查询,从而提高了代码的灵活性和适应性。MyBatis通过结合XML映射文件和注解,实现了动态SQL的执行。以下是MyBatis动态SQL的主要实现方式: 1. XML映射文件(Mapper XML):在MyBatis中,`<select>`, `<update>`, `<delete>`等标签可以包含参数占位符,如`#{id}`, `#{name}`,这些占位符会在运行时被实际的值替换,形成动态的SQL语句。 ```xml <select id="getUser" parameterType="int" resultType="User"> SELECT * FROM users WHERE id = #{id} </select> ``` 2. 参数化查询(Parametrized queries):MyBatis支持使用预编译语句,将参数与SQL语句分离,这样可以防止SQL注入攻击。 3. 显式对象(Parameter Objects):如果动态SQL非常复杂,可以创建一个Java对象作为参数传递给查询,其中包含了多个属性,MyBatis会自动将对象的属性转换为SQL中的列名。 ```java Map<String, Object> params = new HashMap<>(); params.put("startDate", startDate); params.put("endDate", endDate); List<User> users = sqlSession.selectList("getUsers", params); ``` 4. 动态SQL标签:MyBatis提供了`<if>`, `<choose>`, `<when>`, `<otherwise>`等标签,用于根据条件动态生成SQL,实现基于条件的分支查询。 ```xml <select id="getUser" parameterType="map" resultType="User"> <if test="id != null"> SELECT * FROM users WHERE id = #{id} </if> <if test="name != null"> AND name LIKE '%' + #{name} + '%' </if> </select> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值