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>

猜你喜欢

转载自blog.csdn.net/qq_40261771/article/details/84450854
今日推荐