Mybatis出入Map参数查询

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Mybatis出入Map参数查询

走路的猫头鹰   2022-09-08 我要评论

通过出入Map参数作为条件进行查询

映射文件中查询语句部分:

<!--通过map进行条件查询-->
<select id="selectByMap" resultType="com.heiketu.testpackage.pojo.Product">
    select * from Products where prod_price = #{prodPrice} and prod_desc = #{prodDesc}
</select>

接口文件中对应的查询方法:

//输入参数为Map的条件查询
Product selectByMap(Map<String,Object> map);

测试代码:

//...前面有创建sqlSessionFactory对象和SQLSession对象的代码
Map<String,Object> map = new HashMap<>();
map.put("prodPrice","11.99");
map.put("prodDesc","18 inch teddy bear, comes with cap and jacket");
ProductsMapper mapper = sqlSession.getMapper(ProductsMapper.class);
Product product = mapper.selectByMap(map);
System.out.println(product);

map中的key值与映射文件中的select语句#{}占位符中的值需要一一对应

在mybatis中,任何传入的参数都会被对应封装成Map集合,然后会以map的key=value形式取值。

对传入的参数是List集合,mybatis会对list集合进行特殊处理,其取值方式通过list[下标]或collection[下标]的方式入参取值。

对于数组,Mybatis同样会做特殊处理,它会对数组采用array[下标]的方式入参取值。

<!--如果是List集合(或set集合)就如下入参取值-->
<select id="selectByMap" resultType="com.heiketu.testpackage.pojo.Product">
    select * from Products where prod_price = #{list[0]} and prod_desc = #{collection[1]}
</select>
<!--如果是数组就如下入参取值-->
<select id="selectByMap" resultType="com.heiketu.testpackage.pojo.Product">
    select * from Products where prod_price = #{array[0]} and prod_desc = #{array[1]}
</select>

Mybatis查询传递Map参数

使用场景

mybaits传递Map查询数据,choose里面的判断根据自己的map参数类型自行使用,这里传的value是Object类型,这里用不到

<select id="getObjectByMap" parameterType="map" resultMap="BaseResultMap">
        select
            <include refid="Base_Column_List" />
        from
            s_app
        <where>
            <foreach collection="map" item="v" index="k" separator="and">
                <if test="v != null and v != ''">
                    ${k} = #{v}
                </if>
                <!--<choose>
                    <when test="v instanceof integer">
                        <if test="v != null">
                            ${k} = #{v}
                        </if>
                    </when>
                    <when test="v instanceof list">
                        <if test="v != null and v.size() > 0">
                            ${k} in
                            <foreach collection="list" item="v" separator="," open="(" close=")">
                                #{v}
                            </foreach>
                        </if>
                    </when>
                    <when test="v instanceof array">
                        <if test="v != null and v.length > 0">
                            ${k} in
                            <foreach collection="array" item="v" separator="," open="(" close=")">
                                #{v}
                            </foreach>
                        </if>
                    </when>
                    <otherwise>
                        <if test="v != null and v != ''">
                            ${k} = #{v}
                        </if>
                    </otherwise>
                </choose>-->
            </foreach>
        </where>
    </select>

这里需要注意的一点是${K},首先map的key一定要跟数据表字段保持一致。如果这里的${K}写成了#{K}则生成的sql语句如下所示:

程序不会报错,条件无效。${}和#{}的区别相信大家都了解,简单概括就是一个是替换占位符,一个是当做参数传入

传参

    @PostMapping({"demo"})
    public AjaxResult demo(String appNo, String name) {
        Map map = Map.of("app_no", appNo, "cust_name", name);
        SApp objectByMap = service.getObjectByMap(map);
        return success(objectByMap);
    }

持久层

public interface SAppMapper extends BaseMapper<SApp> {
    SApp getObjectByMap(@Param("map") Map<String, Object> map);
}

生成sql

查询结果

总结:这样的好处是参数的个数不固定、参数的类型不固定,适用的场景较多。缺点也很明显map传参可读性差,参数难以控制等。以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们