请问:ibatis2,假如传入参数A,B,它们中任意一个isNotEmpty 那么就增加一条sql。 这个该怎么写?

紧急!我要写在xml文件中,不是通过java传个sql语句进去
2025-04-27 02:53:11
推荐回答(2个)
回答1:




sql1



sql2




没经过测试,一般动态ibatis SQL动态语句是用来动态改变一条SQL语句的条件(包括where 条件和 select 的字段条件)

一般不会你说的那么用,因为你这个问题,完全可以在xml写两个ibatis statement,在java里判断A,B是否为空,来决定调一个还是两个statement

回答2:

C#是这么写的,ibatis2不了解,但是看到isNotEmpty
string Condition = " where 1=1 ";
if (!string.IsNullOrEmpty(KeyWord))
{
Condition += " and aa like '%" + KeyWord.Replace("'", "''") + "%' ";
}
if (!string.IsNullOrEmpty(Remark))
{
Condition += " and Remark = '" + Remark.Replace("'", "''") + "' ";
}
string SelectString = "select * from table " + Condition + " order by Date desc";