当前位置: 代码迷 >> 综合 >> mybatis向动态表名,动态属性表中插入数据,更新数据
  详细解决方案

mybatis向动态表名,动态属性表中插入数据,更新数据

热度:86   发布时间:2024-02-21 03:29:39.0

1、在向动态表中插入数据后,要返回主键id,需要建实体类。(若是不需要返回主键,则不需要建实体类)

package com.lyf.entitypublic class InsertMao{//主建private int id;//表名private String tableName;//字段和字段值private Map<String,String> params;
}

新增sql: 

 <insert id="insertInfo" parameterType="package com.lyf.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>

更新sql:

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

 

  相关解决方案