1.dao层代码
public int getCount(@Param("sex")String sex, @Param("name")String name);
2..xml代码 ,错误的写法,看似正确的等于号select count(*)
from PRO_PROJECT t
where 1=1
<if test="sex == '1'">
and name=#{name}
</if>正确的写法有两种
第一种 因为参数类型是字符串,必须这样写,否则等于号判断无效!
select count(*)
from PRO_PROJECT t
where 1=1
<if test="sex == '1'.toString()">
and name=#{name}
</if>第二种 这种方法与参数类型为整形时一样
select count(*)
from PRO_PROJECT t
where 1=1
<if test="sex == 1">
and name=#{name}
</if>
本文介绍了在使用MyBatis框架进行数据库操作时,如何正确地实现条件查询,特别是针对字符串类型的条件处理方法。通过对比错误与正确的XML映射文件编写方式,帮助开发者避免常见错误。
2万+

被折叠的 条评论
为什么被折叠?



