问题描述
我在mapper.xml
<select id="getCatCount" parameterType="String" resultType="int">
select count(*) from Categories where status is not null
<if test="catTypeId != 0">
AND cat_type_id = #{catTypeId,jdbcType=INTEGER}
</if>
</select>
并且在mapper.java方法中是
and in mapper.java method is
int getCatCount(int catTypeId);
如何在条件中检查catTypeId.我知道上面的语句是不正确的,但是我想放置这样的条件,所以我检查catTypeId是否不为零,然后仅添加AND条件.还是我需要传递Category类的整个对象?
How can I check for catTypeId in if condition. I know the above statement is incorrect but I want to put condition like this so I check if catTypeId not zero then only add AND condition. Or do I need to pass whole object of Category class?
推荐答案
您不需要传递整个Category类.只需按照您的描述进行操作即可:
You do not need to pass the whole Category class. Just do it as you described:
int getCatCount(int catTypeId);
您的mapper.xml应该如下所示:
Your mapper.xml should look like this:
<select id="getCatCount" parameterClass="map" resultType="int">
select count(*) from Categories where status is not null
<if test="param1 != 0">
AND cat_type_id = #{param1,jdbcType=INTEGER}
</if>
</select>
请注意,作为parameterClass
,您需要指定地图.
Notice that as parameterClass
you need to specify a map.
假设现在您要传递两个参数:
Let's say that now, you want to pass two parameters:
int getCatCount(int catTypeId, int mySecondParam);
在映射器中,您应该使用param1
和param2
In the mapper, you should work with param1
and param2
请参阅如何使用"paramX"命名法.但是,可以说,您要使用该命名法,而要使用自定义的参数名称作为"catTypeId".为此,在Java代码中,您需要执行以下操作:
See, how you need to use "paramX" nomenclature. But let's say, that instead using that nomenclature, you would like to use a customized parameter name as "catTypeId". For that, in you Java code you need to do this:
int getCatCount( @Param("cat_type_id ") String cat_type_id);
我将介绍XML映射器,但是使用cat_type_id
代替param1
.
The XML mapper would be the one that I put it about, but using cat_type_id
instead param1
.
这篇关于我如何传递参数并在mybatis动态查询创建中对其进行检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!