MyBatis动态SQL学习

来源:互联网 发布:打印机端口没有usb001 编辑:程序博客网 时间:2024/05/16 04:50

MyBatis动态SQL学习


if标签

如果studentName是null或空字符串,此语句很可能报错或查询结果为空。

此时我们使用if动态sql语句先进行判断,如果值为null或等于空字符串,我们就不进行此条件的判断。

<!-- 查询学生list,like姓名 -->     <select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap">     SELECT * from STUDENT_TBL ST       WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')</select>

此时,当studentName的值为null或’’的时候,我们并不进行where条件的判断,
所以当studentName值为null或’’值,不附带这个条件,所以查询结果是全部。
由于参数是Java的实体类,所以我们可以把所有条件都附加上,使用时比较灵活, 
new一个这样的实体类,我们需要限制那个条件,只需要附上相应的值就会where这个条件,相反不去赋值就可以不在where中判断。

<!-- 查询学生list,like姓名,=性别、=生日、=班级,使用where,参数entity类型 --> <select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap">       SELECT * from STUDENT_TBL ST       <where>    <if test="studentName!=null and studentName!='' ">    ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')     </if>    <if test="studentSex!= null and studentSex!= '' ">    AND ST.STUDENT_SEX = #{studentSex}     </if>    <if test="studentBirthday!=null">    AND ST.STUDENT_BIRTHDAY = #{studentBirthday}     </if>    <if test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">    AND ST.CLASS_ID = #{classEntity.classID}     </if>    </where> </select> 

set 和 trim标记的使用 
和之前的where一样,set和trim也是智能标记 
在之前的user.xml中添加

<update id="updateUserSet" parameterType="User">update User<set><if test="userName != null">userName=#{userName},</if><if test="password != null">password=#{password},</if></set>where id=#{id}</update>

运行,执行的SQL语句是 update User SET userName=?, password=? where id=?
set 自动识别并把sql语句中第二个逗号去掉的。此时数据库user表中id为10的username和password都被修改了。


//trim

trim标识为格式化标识,可以与其他标识完成where和set的功能
prefix  前缀增加   suffix  后缀增加    prefixOverrides 自动判断前置   suffixOverrides 自动判断后置
接着来测试一下,在user.xml中添加

<update id="updateUserTrim" parameterType="User">UPDATE User       <trim prefix="SET" suffixOverrides="," suffix="WHERE id = #{id}" >      <if test="userName != null and userName != '' ">              userName = #{userName},        </if>        <if test="password != null and password != '' ">               password=#{password},        </if>    </trim>

与上面的set语句对比,prefix="SET" 是为SQL语句设置前缀,suffixOverrides是自动判断后缀 ","  
suffix="WHERE id = #{id}" 是自动在加上后缀
运行,执行的SQL代码还是UPDATE User SET userName = ?, password=? WHERE id = ? 
代替where的就是添加 prefix="where"  prefixOverrides="and|or"  trim就会自动的添加前缀where和自动识别和去掉不用的and或or

以下是不熟悉组合运用时分开的写法

    <insert id="insertByValue" parameterType="com.entity.TConsumableInfoEntity">        INSERT INTO            ga_dm.t_consumable_info            (              <trim suffixOverrides=",">                  consumable_info_no,                  <if test="machineSn != null">machine_sn,</if>                  <if test="swVer != null">sw_ver,</if>                  <if test="insertDatetime != null">insert_datetime,</if>                  <if test="updateDatetime != null">update_datetime,</if>              </trim>            )        VALUES            (              <trim suffixOverrides=",">                  #{consumableInfoNo},                  <if test="machineSn != null">#{machineSn},</if>                  <if test="swVer != null">#{swVer},</if>                  <if test="insertDatetime != null">#{insertDatetime},</if>                  <if test="updateDatetime != null">#{updateDatetime},</if>              </trim>            )        ;    </insert>

    <update id="updateByPK" parameterType="com.entity.TConsumableInfoEntity">        UPDATE            ga_dm.t_consumable_info        SET            <trim suffixOverrides=",">                <if test="update.machineSn != null">machine_sn = #{update.machineSn},</if>                <if test="update.machineSn == null">machine_sn = NULL,</if>                <if test="update.swVer != null">sw_ver = #{update.swVer},</if>                <if test="update.updateDatetime == null">update_datetime = NULL,</if>            </trim>        WHERE            consumable_info_no = #{search.consumableInfoNo}        ;    </update>

    <update id="updateByValue" parameterType="com.entity.TConsumableInfoEntity">        UPDATE            ga_dm.t_consumable_info        <set>            <if test="update.machineSnNullFlag == true">machine_sn = NULL,</if>            <if test="update.machineSnNullFlag == false"><if test="update.machineSn != null">machine_sn = #{update.machineSn},</if></if>            <if test="update.swVerNullFlag == true">sw_ver = NULL,</if>            <if test="update.swVerNullFlag == false"><if test="update.swVer != null">sw_ver = #{update.swVer},</if></if>            <if test="update.consumableStartDatetimeNullFlag == true">consumable_start_datetime = NULL,</if>            <if test="update.updateDatetimeNullFlag == true">update_datetime = NULL,</if>            <if test="update.updateDatetimeNullFlag == false"><if test="update.updateDatetime != null">update_datetime = #{update.updateDatetime},</if></if>        </set>        WHERE            consumable_info_no = #{search.consumableInfoNo}        ;    </update>


============

1 0
原创粉丝点击