mybatis动态SQL

来源:互联网 发布:日本军工 知乎 编辑:程序博客网 时间:2024/05/16 14:50

很多人已经把动态sql介绍过了。下面我只说一个小坑 。希望大家不要踩。

<select id="queryCarInfo" parameterType="com.huanqiu.car.pojo.CarPage" resultMap="BaseResultMap">    select     <include refid="Base_Column_List" />    from Car car,    Carteam team,    CarSection cs,    Cartype carType,    Section sec    <where> car.Carteam_id = team.Carteam_id    AND cs.Car_id = car.Car_id    AND sec.Section_id =cs.Section_id    and car.Cartype_id=carType.Cartype_id    <if test="carType != null">        and car.Cartype_id=#{carType}    </if>    <if test="carNumber != null">        and car.Car_number =#{carNumber}    </if>    </where>  </select>

上面这是一个完整的代码块。
其中

 <if test="carNumber != null">      and car.Car_number =#{carNumber}  </if>

为动态SQL
注意点为:

 <if test="#{carNumber != null}">        and car.Car_number =#{carNumber}    </if>

是错误的。虽然不报错。但是这样会把下面的一直输出

and car.Car_number =#{carNumber}

其他的也就没有什么了。 mybatis的动态SQL还是很好用的。

原创粉丝点击