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>