1.新增
dao接口
/**
* 对指定表进行数据新增
*
* @param tableName 表名
* @param keys 表字段集合
* @param values 表插入的数据集合
*/
void insert(@Param("tableName") String tableName, @Param("keys") List<Object> keys, @Param("values") List<Object> values)
mapper.xml
<insert id="insert">
insert into ${tableName}
<foreach collection="keys" item="key" index="index" open="(" separator="," close=")">
${key}
</foreach>
values
<foreach collection="values" item="value" index="index" open="(" separator="," close=")">
#{value}
</foreach>
</insert>
2.修改
dao接口
/**
* 对指定表进行数据修改
*
* @param tableName
* @param map 字段作为key,值作为value的map
* @param primaryKey 主键
* @param id id值
*/
void update(@Param("tableName") String tableName, @Param("map")Map<String, String> map, @Param("primaryKey") String primaryKey, @Param("id") String id);
mapper.xml
<update id="update" parameterType="map">
update ${tableName}
set
<foreach collection="map.entrySet()" index="key" item="value" separator=",">
${key} = #{value}
</foreach>
where
${primaryKey} = #{id}
</update>
注意:${}和#{}的使用,字段名如果使用#{}会导致字段名加引号,所以要字段名都要使用${}