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

1.由於在向動態表中插入數據後,要返回主鍵id,因此我建了實體類。(如果不須要返回主鍵,則不須要建實體類)java

xml代碼spa

 <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"便可,無需建實體類code

②須要返回主鍵id的,實體類以下xml

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

<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>
相關文章
相關標籤/搜索