Java --- Mybatis的动态sql标签

一、if标签

<select id="queryEmpByCondition" resultType="User">
        select * from t_user where 1=1
        <if test="username != null and username != ''">
            and username = #{username}
        </if>
    </select>

if:根据标签中的test属性内容条件决定是否拼接到sql中

@Test
    public void test3(){
        SqlSession sqlSession = SqlSessionUtils.sqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        User admin = new User(null, "admin", null, null, null, null);
        List<User> users = mapper.queryEmpByCondition(admin);
        System.out.println(users);

    }

 二、where标签

条件内容前的and或or

select id="queryEmpByCondition" resultType="User">
        select * from t_user
        <where>
        <if test="username != null and username != ''">
            and username = #{username}
        </if>
       </where>
    </select>

 条件内容后的and或or

<select id="queryEmpByCondition" resultType="User">
        select * from t_user
        <where>
        <if test="username != null and username != ''">
             username = #{username} and
        </if>
       </where>
    </select>

where:当where标签中有内容会生成where关键字,没有内容将不会产生任何效果,并且会将条件内容前的and或or去掉,但条件内容后的依然会生效 

三、trim标签

<select id="queryEmpByCondition" resultType="User">
        select * from t_user
        <trim prefix="where"  suffixOverrides="and|or" >
            <if test="username != null and username != ''">
                username = #{username} and
            </if>
        </trim>
    </select>

trim:

标签中有内容

prefix="" | suffix="" :将trim标签中的内容前面或后面添加指定内容

prefixOverrides="" | suffixOverrides="" :将trim标签中内容前面或后面去掉指定内容

标签中没有内容则trim标签不生效 

四、choose、when、otherwise标签 

<select id="queryEmpByCondition" resultType="User">
        select * from t_user
        <trim prefix="where"  suffixOverrides="and|or" >
            <choose>
                <when test="username != null and username != ''">
                    username = #{username}
                </when>
                <when test="password != null and password != ''">
                    password = #{password}
                </when>
                <otherwise>
                    id = 1
                </otherwise>
            </choose>
        </trim>
    </select>

 相当于if...else if...else

五、foreach标签 

批量删除:

形式一

<mapper namespace="com.cjc.ssm.mapper.DynamicSQLMapper">
    <delete id="removeByUserArray">
        delete from t_user where id in
        <foreach collection="arr" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

 形式二

<delete id="removeByUserArray">
        delete from t_user where
        <foreach collection="arr" item="id" separator="or">
            id = #{id}
        </foreach>
    </delete>

批量添加

 <insert id="addUserList">
        insert into t_user values
        <foreach collection="list" item="user" separator=",">
         (null,#{user.username},#{user.password},#{user.age},#{user.sex},#{user.email})
        </foreach>
    </insert>

 foreach:

collection:设置需要循环的数组或集合

item:数组或集合中的每一个数据

separator:循环体之间的分割符

open:所有循环内容的开始符

close:所有循环内容的结束符

六、sql标签 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鸭鸭老板

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

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

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

打赏作者

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

抵扣说明:

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

余额充值