ibatis之我将如何传递参数并在 mybatis 动态查询创建中检查它
lexus
阅读:22
2024-10-01 17:34:08
评论:0
我在我的 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 中方法是
int getCatCount(int catTypeId);
如何在 if 条件下检查 catTypeId。我知道上面的陈述是不正确的,但我想像这样放置条件,所以我检查 catTypeId 是否不为零,然后只添加 AND 条件。还是我需要传递类别类的整个对象?
请您参考如下方法:
您不需要通过整个类别类。按照你的描述去做:
int getCatCount(int catTypeId);
您的 mapper.xml 应如下所示:
<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
,您需要指定一个映射。
假设现在,您要传递两个参数:
int getCatCount(int catTypeId, int mySecondParam);
在映射器中,您应该使用param1
和param2
看,您需要如何使用“paramX”命名法。但是,假设您不想使用该命名法,而是希望使用自定义参数名称作为“catTypeId”。为此,在您的 Java 代码中,您需要执行以下操作:
int getCatCount( @Param("cat_type_id ") String cat_type_id);
XML 映射器将是我提出的那个,但使用 cat_type_id
而不是 param1
。
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。