mybatis的查询sql,参数为map

本文探讨了在MyBatis中如何使用Map作为参数进行查询SQL的方法,包括直接封装到Map中以及不封装的情况。详细讲解了在service层、mapper层以及xml配置中的实现方式,并提到了在MySQL中concat和group_concat()函数的应用。
1.将参数封装到map集合中(用param.

service层

String tableName = "userData";
Map<String,Object> params = new HashMap<>();
params.put("userId", userId);
params.put("name", name);
List<UserData> list = userDataMapper.listSelective(tableName, params);

mapper层

List<UserData> listSelective(
            @Param("tableName") String tableName,
            @Param("params") Map<String, Object> params);

xml中

 <select id="listSelective" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from ${tableName} 
        <trim prefix="where" prefixOverrides="and|or">
            <if test="params.userId != null">
                user_id = #{params.userId,jdbcType=BIGINT}
            </if>
            <if test="params.name !=null and params.name !='' ">
                and name like concat("%",#{params.name, jdbcType=VARCHAR},"%")
            </if>
        </trim>
    </select>

1.传表名使用$
2.concat拼接字符串,mysql中concat 和 group_concat()的用法

2.将参数封装到map集合中(不用param.)

mapper层

List<CloanUserModel> listModel(Map<String, Object> params);

xml

<!-- 基本用户信息的sql查询条件公共引用 -->
    <sql id="searchBaseBy">
        <trim prefix="where" prefixOverrides="and|or">
            <if test="loginName !='' and loginName !=null">
                and u.login_name like concat("%",#{loginName,jdbcType=VARCHAR},"%")  
               </if>
            <if test="registTime !=null">
                and DATE_FORMAT(u.regist_time,'%Y-%m-%d') = #{registTime,jdbcType=TIMESTAMP}
            </if>
            <if test="startTime != null">
                and DATE_FORMAT(u.regist_time,'%Y-%m-%d') &gt;= #{startTime,jdbcType=TIMESTAMP}
            </if>
            <if test="endTime  != null">
                and DATE_FORMAT(u.regist_time,'%Y-%m-%d') &lt;= #{endTime,jdbcType=TIMESTAMP}
            </if>
        </trim>
    </sql>

  <select id="listModel" resultMap="BaseInfoModelMap" parameterType="java.util.HashMap">
        select u.id,u.login_name,a.real_name,
        (SELECT c.used FROM arc_credit c where c.consumer_no = u.id)  as used
        from cl_user u left join cl_user_base_info a on u.id = a.user_id
        LEFT JOIN cl_user_auth b ON a.user_id = b.user_id
        <include refid="searchBaseBy"/>
        order by u.id desc
    </select>

先暂时写这两种,至于foreach使用,日后补充,可以参考基于mysql对mybatis中的foreach进行深入研究

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值