Mybatis使用实例

本文介绍使用MyBatis进行数据库批量增删改查的具体实现方式,包括插入、更新、删除及查询等操作,并提供了相应的XML配置示例。

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

 <insert id="addStudent" parameterType="java.util.Map">
 insert into student
    (id,name,age)
    values
    (
      #{item.id},
      #{item.name},
      #{item.age}
    )
 </insert>

批量

 <insert id="addStudent" parameterType="java.util.Map">
 insert into student
    (id,name,age,start_time)
    values
     <foreach collection="list" item="item" index="index" separator=","> 
    (
      #{item.id},
      #{item.name},
      #{item.age},
      now()
    )
    </foreach>
  </insert>

这里写代码片

批量


 <update id="updateStudent" parameterType="java.util.Map">
   update student
  <set> 
      <if test="userid != null and userid != '' ">
        userid=#{userid},
      </if>
  </set>
  where 1=1
    <if test="id!= null and id!= '' ">
        and id=#{id}
    </if>
  </update>

批量

<update id="updateStudent" parameterType="java.util.Map">
      update student set state=2  where 1=1
       and id in 
       <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
              #{item}
       </foreach>

   </update>
<update id="editStudent"  parameterType="java.util.List">  
    <foreach collection="list" item="item" index="index" separator=";"> 
     update ware_service
    <set> 
        <if test="item.endTime!=null and item.endTime!=''">
           end_time=#{item.endTime},
        </if>
        <if test="item.redeemcode!=null and item.redeemcode!=''">
           redeemcode=#{item.redeemcode}
        </if>         
    </set>
    where id=#{item.id}
      </foreach>  
</update>  

批量修改需设置db.properties
新增 &allowMultiQueries=true


  <select id="getAllStudent" resultType=com.mofangge.entity.student" parameterType="java.util.Map">
    select 
    id,
    name,
    age,
    start_time as startTime
    from student where 1=1
      <if test="userid != null and userid != '' ">
        and userid=#{userid}
      </if>
    and end_time > now()
  </select>

批量

<select id="getAllStudents" resultType="com.mofangge.entity.student" parameterType="java.util.Map">
    select
    id,
    name,
    age,
    start_time as startTime
    from student where 1=1 and age in
      <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
        #{item}
      </foreach>
         and end_time > now()
    <if test="userid!=null and userid!=''">
        and userid=#{userid}
    </if>      
  </select>
<select id="getAllStudent" resultType="com.mofangge.entity.Student" parameterType="java.util.Map">
    select
    id,
    name,
    age,
    from student where 1=1 and code in
      <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
      </foreach>
         and end_time > now()
    <if test="id!=null and id!=''">
        and id=#{id}
    </if>      
  </select>

看不懂的留评论~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值