mybatis传入map任意表增删改查,分页过滤字段

本文介绍了一个使用MyBatis进行动态SQL查询、插入、更新和分页的示例。通过参数化的map集合,实现了根据实体参数动态构建SQL语句的功能。包括了根据条件查询数据、统计查询结果数量、插入新记录、更新现有记录和实现分页功能的具体实现。
<!--根据实体参数查询 -->
    <select id="selectBaseList" resultType="java.util.HashMap">
        select
        *
        from ${map.tableName}
        where 1=1
        <foreach collection="map" index="key" item="value">
            <if
                test="key != 'tableName' and key != 'pageSize' and key != 'pageNo' ">
                and `${key}` = #{value}
            </if>
        </foreach>
        limit #{baseModel.fromRec},#{baseModel.pageSize}
    </select>

    <!--根据实体参数查询个数 -->
    <select id="selectBaseCount" resultType="java.lang.Integer">
        select
        count(1)
        from ${map.tableName}
        where 1=1
        <foreach collection="map" index="key" item="value">
            <if
                test="key != 'tableName' and key != 'pageSize' and key != 'pageNo' ">
                and  `${key}` = #{value}
            </if>
        </foreach>
    </select>


    <insert id="insertBase" parameterType="java.util.HashMap">
        insert into ${map.tableName}
        (
        <foreach collection="map" index="key" item="value"
            separator=",">
            <if test="key != 'tableName' ">
                `${key}`
            </if>
        </foreach>
        )
        values (
        <foreach collection="map" index="key" item="value"
            separator=",">
            <if test="key != 'tableName' ">
                #{value}
            </if>
        </foreach>
        )
    </insert>
    <update id="updateBaseByPrimaryKey">
        update ${map.tableName}
        <set>
            <foreach collection="map" index="key" item="value"
                separator=",">
                <if test="key != 'tableName' ">
                    `${key}`= #{value}
                </if>
            </foreach>
        </set>
        where id = #{map.id}
    </update>

java代码部分,baseModel中放入分页参数:


import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;


public interface BaseDao {
    int insertBase(@Param("map") Map map);

    Map selectBaseByPrimaryKey(Long id);

    int updateBaseByPrimaryKey(@Param("map") Map map );

    List<Map> selectBaseList(@Param("map") Map map, @Param("baseModel") BaseModel baseModel);

    int selectBaseCount(@Param("map") Map map);

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lxr1908

用钱砸我

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值