mybatis动态sql问题 求解?

新手上路,请多包涵

select * from table a where a.project_id=#{projectId} and a.id != #{id} and a.status=3 and a.id_card = #{code} or a.unit_code = #{code}

select * from table a where a.project_id=#{projectId} and a.id != #{id} and a.status=3 and

<if test="type == 'idCard'">a.id_card = #{code}</if>
<if test="type == 'unitCode'">a.unit_code = #{code}</if>

<if test="type == idCard">a.id_card = #{code}</if>
<if test="type == unitCode">a.unit_code = #{code}</if>

<if test='type == "idCard"'>a.id_card = #{code}</if>
<if test='type == "unitCode"'>a.unit_code = #{code}</if>

下面的sql是对上面的优化 但是这样写运行时会把错(3种都会badSql) 求大佬解答

阅读 2.1k
2 个回答
select * from table a 
<where>
 a.project_id=#{projectId}
 and a.id != #{id}
 and a.status=3 
<choose>
    <when test="type == idCard"> 
        and a.id_card = #{code}
    </when>
<when test="type == unitCode">and a.unit_code = #{code}</when>
    <otherwise>  
    </otherwise>  
</choose>
</where>

<choose>

<when>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题