Mybatis Sql 动态 插入

发布时间 2024-01-04 16:41:30作者: 乔治叔叔
public interface CustomizeMapper {

    void dynamicInsert(@Param("tableName") String tableName, @Param("list") Collection<JSONObject> list);

    int dynamicUpdate(@Param("cId") Long cId, @Param("tableName") String tableName, @Param("identField") String identField, @Param("identValue") Long identValue, @Param("update") LinkedHashMap<String, String> update);

    void batchDynamicUpdate(@Param("cId") Long cId, @Param("tableName") String tableName, @Param("identField") String identField, @Param("updateList") List<JSONObject> updateList);

    void batchDynamicUpdate2(@Param("cId") Long cId, @Param("tableName") String tableName, @Param("identField") String identField, @Param("ids") Collection<Long> ids, @Param("update") JSONObject update);

    List<JSONObject> dynamicSelect(@Param("tableName") String tableName,@Param("cId") Long cId,@Param("select") LinkedHashMap<String, String> select);

}
<mapper namespace="com.fumasoft.bill.server.bigCustomer.mapper.CustomizeMapper">
    <insert id="dynamicInsert">
        <foreach collection="insert" index="index" item="item" separator=";">
            insert into ${tableName} (<foreach collection="item.entrySet()" index="key" item="value" separator=",">
            ${key}</foreach>)
            values (<foreach collection="item.entrySet()" index="key" item="value" separator=",">#{value}</foreach>)
        </foreach>
    </insert>
    <update id="dynamicUpdate">
        update ${tableName} set <foreach collection="update.entrySet()" index="key" item="value" separator=",">
        ${key}=#{value}
    </foreach> where c_id = #{cId} and ${identField} = #{identValue}
    </update>
    <update id="batchDynamicUpdate">
        <foreach collection="updateList" item="item" index="index" separator=";">
            update ${tableName} set
            <foreach collection="item.entrySet()" index="key" item="value" separator=",">
                ${key}=#{value}
            </foreach>
            where c_id = #{cId} and ${identField} = #{item.[${identField}]}
        </foreach>
    </update>
    <update id="batchDynamicUpdate2">
        update ${tableName} set <foreach collection="update.entrySet()" index="key" item="value" separator=",">
        ${key}=#{value}
    </foreach> where c_id = #{cId} and ${identField} in
        <foreach collection="ids" index="index" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    <select id="dynamicSelect" resultType="com.alibaba.fastjson.JSONObject">
        select * from ${tableName} where c_id = #{cId} and
        <foreach collection="select.entrySet()" index="key" item="value" separator="and">
            ${key}=#{value}
        </foreach>
    </select>
</mapper>