美文网首页
mybatis 实例

mybatis 实例

作者: 高山之水 | 来源:发表于2018-04-16 18:02 被阅读0次
    <!-- 批量插入 遇到重复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>
    

    看不懂的留评论~~

    相关文章

      网友评论

          本文标题:mybatis 实例

          本文链接:https://www.haomeiwen.com/subject/czczhftx.html