如何检查 myBatis 映射器 XML 中的参数是否存在?
我将我的问题简化为以下代码。
Mapper 接口如下所示:
@Mapper
public interface TestMapper {
public Integer countTable(@Param("tValue1") Integer tValue1);
public Integer maxTable(@Param("tValue2") Integer tValue2);
}
Mapper Xml 如下所示:
<sql id="sql_where">
<where>
<if test="tValue1 != null">tValue1 = #{tValue1}</if>
<if test="tValue2 != null">AND tValue2 = #{tValue2}</if>
<where>
</sql>
<select id="countTable" resultType="Integer">
SELECT COUNT(*) FROM TestTable
<include refId="sql_where"/>
</select>
<select id="maxTable" resultType="Integer">
SELECT MAX(*) FROM TestTable
<include refId="sql_where"/>
</select>
当调用 countTable 或 maxTable 方法时,发生 BindingException。 由于未找到参数之一。
如何检查 XML 中是否存在该参数? 我可以将所有参数插入到方法中,但我不希望程序员感到困惑(它们不会被使用)
I simplifed my problem as following code.
Mapper Interface looks like:
@Mapper
public interface TestMapper {
public Integer countTable(@Param("tValue1") Integer tValue1);
public Integer maxTable(@Param("tValue2") Integer tValue2);
}
Mapper Xml looks like:
<sql id="sql_where">
<where>
<if test="tValue1 != null">tValue1 = #{tValue1}</if>
<if test="tValue2 != null">AND tValue2 = #{tValue2}</if>
<where>
</sql>
<select id="countTable" resultType="Integer">
SELECT COUNT(*) FROM TestTable
<include refId="sql_where"/>
</select>
<select id="maxTable" resultType="Integer">
SELECT MAX(*) FROM TestTable
<include refId="sql_where"/>
</select>
when countTable or maxTable method is called, BindingException occurs.
Due to one of parameter is not found.
How can I check the parameter exists in XML?
I could've insert all params to method, but I don't want programmers to be confused (they're not going to be used)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论