1. 程式人生 > >Mybatis 動態查詢、插入、修改操作

Mybatis 動態查詢、插入、修改操作

簡單的記一下吧 

insert操作   特別注意裡面的判斷語句,一般都是用包裝類最好,另外 String型別最好加上一個判斷  != ''

  <!--插入專案資訊-->
    <insert id="insertItemInfo">
        insert into item_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pid != null">
                pid,
            </if>
            <if test="batch_name != null and batch_name =''">
                batch_name,
            </if>
            <if test="faculty != null and faculty != ''">
                faculty,
            </if>
            <if test="item_type != null and item_type != ''">
                item_type,
            </if>
            <if test="item_rank != null and item_rank != ''">
                item_rank,
            </if>
            <if test="item_apply_status != null">
                item_apply_status,
            </if>
            <if test="item_middle_status != null ">
                item_middle_status,
            </if>
            <if test="item_conclusion_status != null">
                item_conclusion_status,
            </if>
            <if test="item_sumMoney != null">
                item_sumMoney,
            </if>
            <if test="item_balanceMoney != null">
                item_balanceMoney,
            </if>
            <if test="apply_time != null">
                apply_time,
            </if>
            <if test="item_introduce != null">
                item_introduce,
            </if>
        </trim>

        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="pid != null">
                #{pid},
            </if>
            <if test="batch_name != null and batch_name =''">
                #{batch_name},
            </if>
            <if test="faculty != null and faculty != ''">
                #{faculty},
            </if>
            <if test="item_type != null and item_type != ''">
                #{item_type},
            </if>
            <if test="item_rank != null and item_rank != ''">
                #{item_rank},
            </if>
            <if test="item_apply_status != null">
                #{item_apply_status},
            </if>
            <if test="item_middle_status != null">
                #{item_middle_status},
            </if>
            <if test="item_conclusion_status != null">
                #{item_conclusion_status},
            </if>
            <if test="item_sumMoney != null">
                #{item_sumMoney},
            </if>
            <if test="item_balanceMoney != null">
                #{item_balanceMoney},
            </if>
            <if test="apply_time != null">
                #{apply_time},
            </if>
            <if test="item_introduce != null">
                #{item_introduce},
          </if>
        </trim>
    </insert>

 

 

select 操作   還是自己要去注意where標籤的用法

 <select id="getItemInfoByItemInfo" parameterType="com.jxau.dachuang.pojo.ItemInfo"             
   resultType="com.jxau.dachuang.pojo.ItemInfo">
        select * from item_info
        <where>
        <if test="faculty != null and faculty != ''">
            faculty = #{faculty} and
        </if>
        <if test="item_type != null and item_type != '' ">
            item_type = #{item_type} and
        </if>
        <if test="item_rank != null and item_rank !=''">
            item_rank = #{item_rank} and
        </if>
        <if test="pid != null and pid !=''">
            pid = #{pid} and
        </if>
        <if test="item_id != null and item_id != 0">
            item_id = #{item_id} and
        </if>
            1=1
        </where>
    </select>

 

update  這裡有一個set標籤的用法

  <update id="updateStudent" parameterType="com.jxau.dachuang.pojo.Student">
        update student
        <set>
            <if test="studName != null and studName != ''">
                studName = #{studName,jdbcType=VARCHAR},
            </if>
            <if test="stud_faculty != null and stud_faculty!=''">
                stud_faculty = #{stud_faculty,jdbcType=VARCHAR},
            </if>
            <if test="className != null and className != ''">
                className = #{className,jdbcType=VARCHAR},
            </if>
            <if test="qq != null and qq !='' ">
                qq = #{qq,jdbcType=VARCHAR},
            </if>
            <if test="sex != null and sex != ''">
                sex = #{sex,jdbcType=VARCHAR},
            </if>
            <if test="phoneNumber != null and phoneNumber !='' ">
                phoneNumber = #{phoneNumber,jdbcType=VARCHAR},
            </if>
            <if test="studNum != null and studNum !=''">
                studNum = #{studNum,jdbcType=VARCHAR},
            </if>
        </set>
        where item_id = #{item_id,jdbcType=INTEGER} and sign = #{sign}
    </update>