Mybatis整合mybatis查询遇到的坑

7 篇文章 0 订阅
5 篇文章 0 订阅
"这篇博客介绍了在MyBatis中如何使用Map进行查询,特别是当Map的值类型不一致时,如何在动态SQL中指定参数类型。示例展示了如何构建Map查询对象,并在SQL查询中根据条件选择不同的查询路径,同时注意在Map参数判空时避免使用#{...}
摘要由CSDN通过智能技术生成

mybatis整合map查询过程中如果map的value的参数类型不一致时,new map集合的过程中不需要指定map的类型。在动态sql语句中指定具体查询的参数类型即可

 public Result findScenicCommentLevel(@RequestParam(value = "province_ids",required = false) Integer province_ids,
                                         @RequestParam(value = "city_ids",required = false) Integer city_ids,
                                         @RequestParam(value = "ranks",required = false) String ranks,
                                         @RequestParam(value = "scenic_ids",required = false) Integer scenic_ids) {
        try {
            Map querymap = new HashMap<>();
            querymap.put("scenicId",scenic_ids);
            querymap.put("province_ids",province_ids);
            querymap.put("city_ids",city_ids);
            querymap.put("ranks",ranks);
<select id="findNegativeCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(a.`ranges`) FROM yq_comment a
        <choose>
            <when test="scenicId != null and  scenicId != '' ">
                INNER JOIN yq_scenic b ON a.scenic_id = b.`scenic_id`
                where a.scenic_id = #{scenicId,jdbcType=INTEGER}
            </when>
            <otherwise>
                where a.scenic_id in
                (SELECT b.scenic_id FROM yq_scenic b where 1=1
                <if test="province_ids != null and province_ids != ''">
                    and b.province_id = #{province_ids,jdbcType=INTEGER}
                </if>
                <if test="city_ids != null">
                    and b.city_id = #{city_ids,jdbcType=INTEGER}
                </if>
                <if test="rank !=null">
                    and b.rank = #{rank,jdbcType=VARCHAR}
                </if>
                )
            </otherwise>
        </choose>
        GROUP BY a.`ranges` HAVING a.`ranges` = 3
    </select>

 

 在mybatis中map参数判空时注意不需要加上#{},否则会报错

 java.util.LinkedHashMap cannot be cast to java.lang.String

Myabatis中注释sql语句时需要注意,在操作语句中注释容易出错,需要,将注释哪出类似select这样的语句外

Cause: java.sql.SQLException: Parameter index out of range (2 > number of parameters,which is 1). 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值