mybatis 实例

<!-- 批量插入 遇到重复key 改为更新 需求设定-->
    <insert id="addBatchDepartment" parameterType="java.util.List" >
        INSERT INTO department (
        branchId,
        branchName,
        common,
        spellId)
        VALUES
        <foreach collection="list" item="item" index="index" separator="," >
            (
            #{item.branchId},
            #{item.branchName},
            #{item.common},
            #{item.spellId}
            )
        </foreach>
        ON DUPLICATE KEY UPDATE
        branchId = values(branchId)
    </insert>

 <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>

看不懂的留评论~~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 官方文档链接 一、添加依赖 maven project 注:Mac 上开发,使用SqlServer,所以需要jtd...
    天涯笑笑生阅读 475评论 0 1
  • 写下去 5天没有写文了。 假期开始就对自己说,好好玩,不准写东西,不准思考,不准用脑。 大概是因为自己的姿势不正确...
    熊宴阅读 333评论 11 4
  • 从少年到成年,从莽撞到成熟,从浑身刺角到温顺乖巧,从血气方刚到安于现状,时间改变了好多东西,那个曾经你哭...
    阿森者阅读 301评论 0 0
  • text-align: center的作用是什么,作用在什么元素上?能让什么元素水平居中 text-align: ...
    知更鸟_b4d4阅读 360评论 0 0
  • 最近遇到了这样的三件事情。 和A最初打交道的时候,觉得和善,平易近人,礼貌周到。最初的合作没有任何的问题,我得到的...
    云心月阅读 958评论 4 4