mysql中sql语句的常用语句

本文介绍了MyBatis中动态SQL的应用技巧,包括提取公共SQL语句、动态添加及修改SQL语句的方法,并展示了如何避免重复插入数据、获取受影响行数、自动获取主键等实用操作。此外还提供了批量新增数据、去重插入以及使用CONCAT和bind进行like查询的示例。

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

 

1:提取公共的sql语句:

 

2:动态添加----sql语句:

代码:

  <insert id="test1" parameterType="com.floor.shop.model.Product">
        INSERT INTO product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productName!=null and productName!=''">
                product_name,
            </if>
            <if test="stockNum!=null and stockNum!=''">
                stock_num,
            </if>
            <if test="salePrice!=null and salePrice!=''">
                sale_price,
            </if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="productName!=null and productName!=''">
                #{productName},
            </if>
            <if test="stockNum!=null and stockNum!=''">
                #{stockNum},
            </if>
            <if test="salePrice!=null and salePrice!=''">
                #{salePrice},
            </if>

        </trim>
    </insert>
View Code

3:动态修改----sql语句:

 

4:增加前避免重复:

(当userName在数据库中不存在的情况下,增加到数据库)

5:修改或者新增获取受影响的行数:

6:新增自动获取主键(新增id):

7:dao多参数处理,不封装的情况下传递参数:

mapper中的sql语句不用添加参数类型(paramater=" ")

8:传入多个值的数组处理(foreach):

 

 接口:

映射文件:

测试:

 

批量新增,数据采用list传入:

<insert id="saveCustomerOpenBankInfo" parameterType="List">
    INSERT INTO resource_customer_openbank(id,open_name,account_number,open_bank,customer_id)
    VALUES
     <foreach item="item" collection="list" index="idx" open="" separator="," close="">
    (#{item.id}, #{item.openName}, #{item.accountNumber},#{item.openBank},#{item.customerId})
     </foreach>
</insert>

 

批量插入去重数据:

<!--插入用户角色信息  -->

    <insert id="insertUserRole" parameterType="ArrayList" >
    insert into usi_user_role (user_id,role_id)
    <foreach collection="list" item="item" index="index" separator="union all">
    select #{item.userid,jdbcType=VARCHAR},
    #{item.roleid,jdbcType=VARCHAR} from dual  where not exists(select * from usi_user_role
     where user_id = #{item.userid,jdbcType=VARCHAR}
       and role_id = #{item.roleid,jdbcType=VARCHAR})
    </foreach>

  </insert> 

 

 

 

mybatis like 拼接、动态sql拼接:

 

1、CONCAT()拼接%:

 SELECT * FROM t_usr WHERE name like CONCAT('%',#{name},'%') 

 

2:bind:

 

 SELECT  *  
  FROM t_usr 
  WHERE
      <if test="name !=null || name !=''">
          <bind name="name" value="'%' + name + '%'"/>
          name like #{name}
      </if>

 

 怎样用查询出的一张表数据更新另一张表的字段??

用表B的数据(B1列,B2)更新表A的A1,A2列
update A, B  set A.A1 = B.B1,A.A2=B.B2 where A.ID1 = B.ID1 and A.ID2 = B.ID2;

--或

update A INNER JOIN B ON A.ID1 = B.ID1 AND A.ID2= B.ID2 SET A.A1 = B.B1,A.A2=B.B2;

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/dw3306/p/9289891.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值