这是我的实体参数。
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 这一句
 

解决方案 »

  1.   

    ibatis好像要用<isNull>和<isNotNull>标签来判断nulllz这么写和sql里面直接写“xxx = null”的条件是一样的错误。good luck
      

  2.   

    ibatis3 没有<isNull>和 <isNotNull>这些标签。
    官方文档上是这样写的:<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>
      

  3.   

        <select id="findWorkerByMap" parameterClass="java.util.Map" resultMap="workerMap">
       select * from worker  where deletestate=0
        <dynamic prepend="">       <isNotEmpty property="id" prepend="and"> 
                 id= #id#
           </isNotEmpty>
       </dynamic>
        </select> 
     或者用楼上说的用 <isNull>和 <isNotNull>
      

  4.   

    LZ说了用的是Ibatis3,在3里面已经没有isNull,isNotEmpty 之类的。看来你的id类型是int,很可能他被解析成int,这是如果传进来的是null,那么他的值初始化是0,而不是null。你可以试试在映射的时候,java的类型设置成Integer,或者,把java里面id的类型变成String,这样应该就没问题了。
      

  5.   

    刚才测了一下,果然,int类型的变量,不设置值,就会初始化成0,而不是null。
      

  6.   

    Ibatis2.x的版本中有<isNotEmpty>标签
    Ibatis3.x中改成了if,choose,when,otherwise,trim,set等了,和jstl类似
      

  7.   

    这个问题可以解决,你可以在where后面加一个 1=1 的条件,这样就不怕有AND了