这是我的实体参数。
public List<HelpTopic> findHelpTopicByCondition(HelpTopic ht){
List<HelpTopic> hts=null;
SqlSession session=getSqlSessionFactory().openSession();
hts=session.selectList("HelpTopicSpace.findHelpTopicByCondition",ht);
close(session);
return hts;
}
xml配置: <select id="findHelpTopicByCondition"
resultType="org.apache.ibatis.zsamples.entities.HelpTopic"
parameterType="org.apache.ibatis.zsamples.entities.HelpTopic"
useCache="true">
select help_topic_id as id, name as name, help_category_id,
description as description, example as example, url as url from
help_topic
<where>
<if test="name!=''">
name=#{name}
</if>
<if test="id!=null">
and id=2
</if>
<if test="name!=''">
and description=#{description}
</if>
<if test="name!=''">
and example=#{example}
</if>
<if test="name!=''">
and url=#{url}
</if>
</where> </select>
ibatis 的打印的SQLSELECT help_topic_id AS id,
name AS name,
help_category_id,
description AS description,
example AS example,
url AS url
FROM help_topic
WHERE name =?
AND id = 2
AND description =?
AND example =?
AND url =? 我的问题是:ht实体的id==null.所以IBATIS 生成的SQL语句应该没有 AND id = 2 这一句
官方文档上是这样写的:<select id=”findActiveBlogLike”
parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test=”title != null”>
AND title like #{title}
</if>
<if test=”author != null && author.name != null”>
AND title like #{author.name}
</if>
</select>
select * from worker where deletestate=0
<dynamic prepend=""> <isNotEmpty property="id" prepend="and">
id= #id#
</isNotEmpty>
</dynamic>
</select>
或者用楼上说的用 <isNull>和 <isNotNull>
Ibatis3.x中改成了if,choose,when,otherwise,trim,set等了,和jstl类似