mybatis向動態表名,動態屬性表中插入數據,更新數據


1.因為在向動態表中插入數據后,要返回主鍵id,所以我建了實體類。(若是不需要返回主鍵,則不需要建實體類)

xml代碼

 <insert id="insertInfo" parameterType="com.entity.InsertMap" useGeneratedKeys="true" keyProperty="id">  
     insert ignore into ${tableName}   
      <foreach collection="params.keys" item="key" open="(" close=")" separator="," >  
         ${key} 
      </foreach>  
      values   
      <foreach collection="params.keys"  item="key" open="(" close=")" separator=",">  
         #{params.${key}}  
      </foreach>  
</insert>

①若是不需要返回插入數據的主鍵id,則 parameterType="java.util.Map"即可,無需建實體類

②需要返回主鍵id的,實體類如下

2.向動態表名,動態屬性表中更新數據

<update id="updateInfoByID" parameterType="java.util.Map">
  UPDATE ${tableName} set
      <foreach item="value" index="key" collection="params" separator=",">
         <if test="key != 'id'">
              ${key} = #{value}
         </if>
      </foreach>
       WHERE
     <foreach item="value" index="key" collection="params" separator=",">
           <if test="key == 'id'">
               ID = #{value}
           </if>
     </foreach> 
</update>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM