怎样判断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中写的是:


<if test="loopNum == '1'"> WHERE  RN &lt;=100000  and RN &gt;=1 </if>

<if test="loopNum == '2'"> WHERE  RN &lt;=200000  and RN &gt;=100001 </if>

结果报错:

There is no getter for property named 'loopNum' in 'class java.lang.String'。

然后按照 Mybatis中传参报错 链接里的改成_parameter,执行之后报misfired triggers,一直在查询数据,感觉是在查整个库的数据,但是一直执行不出来。

所以想判断传入MyBatis参数的值,应该使用哪种方法,可不可以用when标签?


暮色呼如
浏览 740回答 1
1回答

慕尼黑8549860

问题已解决,在MyBatis的xml中替换成了when标签,并且在判断时,将判断的值加上toString(),参数名为_parameter,不需要指定成传入的参数名,参考代码如下:<select id="getLoop" resultMap="AAA" parameterType="String">&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; SELECT *, ROWNUM RN from tablename&nbsp; &nbsp; &nbsp; &nbsp; <where>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <choose>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <when test="_parameter != null and _parameter == '1'.toString()">&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RN &lt;=100000&nbsp; and RN &gt;=1&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </when>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <when test="_parameter != null and _parameter == '2'.toString()">&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RN &lt;=200000&nbsp; and RN &gt;=100001&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </when>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <when test="_parameter != null and _parameter == '3'.toString()">&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RN &lt;=300000&nbsp; and RN &gt;=200001&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </when>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <otherwise>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RN &lt;=1&nbsp; and RN &gt;=1&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </otherwise>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </choose>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </where>&nbsp; &nbsp;</select>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java