开发者社区> 问答> 正文

怎样判断Mybatis传入参数的值

在查询数据时,想通过循环,每次只查询10万条记录。该怎样把循环的参数传入SQL中,并利用MyBatis判断参数,执行相应的SQL
相应的代码:

//循环查询信息
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<AAA> getLoop(String loopNum) {
    return (List)this.getBySqlKey("getLoop",loopNum);
}

XML:

<select id="getLoop" resultMap="AAA" parameterType="String">       
    SELECT *, ROWNUM RN from tablename
        <if test = " _parameter=='1'"> WHERE  RN &lt;=100000  and RN &gt;=1</if>
        <if test = " _parameter=='2'"> WHERE  RN &lt;=200000  and RN &gt;=100001 </if>
</select>

一开始if中写的是:
` WHERE RN <=100000 and RN >=1
WHERE RN <=200000 and RN >=100001 `
结果报错:
There is no getter for property named 'loopNum' in 'class java.lang.String'。
然后按照 Mybatis中传参报错 链接里的改成_parameter,执行之后报misfired triggers,一直在查询数据,感觉是在查整个库的数据,但是一直执行不出来。
所以想判断传入MyBatis参数的值,应该使用哪种方法,可不可以用when标签?

展开
收起
蛮大人123 2016-03-10 16:58:40 5898 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    在MyBatis的xml中替换成了when标签,并且在判断时,将判断的值加上toString(),参数名为_parameter,不需要指定成传入的参数名,参考代码如下:

    <select id="getLoop" resultMap="AAA" parameterType="String">     
        SELECT *, ROWNUM RN from tablename
            <where>    
                <choose>    
                    <when test="_parameter != null and _parameter == '1'.toString()">    
                        RN &lt;=100000  and RN &gt;=1   
                    </when>   
                    <when test="_parameter != null and _parameter == '2'.toString()">    
                        RN &lt;=200000  and RN &gt;=100001   
                    </when>
                    <when test="_parameter != null and _parameter == '3'.toString()">    
                        RN &lt;=300000  and RN &gt;=200001   
                    </when>        
                    <otherwise>    
                        RN &lt;=1  and RN &gt;=1     
                    </otherwise>    
                </choose>    
            </where>   
    </select>
    2019-07-17 18:57:57
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题) 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多