mybatis使用if标签进行单个或多个属性修改数据

test:

//修改goods
    @Test
    public void testUpdateGoods(){
    
    
        GoodsDao mapper = session.getMapper(GoodsDao.class);
        Goods goods = new Goods();
        //如果goods中的某些属性没有写,则不修改
        goods.setGid(4);
        goods.setGprice(3155.55);
        goods.setGcount(30);
        int i = mapper.updateGoods(goods);
        System.out.println(i);
        System.out.println("==============");
        int[] gids ={
    
    4};
        List<Goods> goods1 = mapper.selGoodsByGids(gids);
        for (Goods g : goods1) {
    
    
            System.out.println(g);
        }
    }

映射文件:

<!--修改goods-->
    <update id="updateGoods" parameterType="Goods">
        update t_goods
        <set>
            <if test="gname != null">
                gname = #{
    
    gname},
            </if>
            <if test="gprice > 0">
                gprice = #{
    
    gprice},
            </if>
            <if test="gcount > 0">
                gcount = #{
    
    gcount},
            </if>
            <if test="gdate != null">
                gdate = #{
    
    gdate}
            </if>
        </set>
        <where>
            gid = #{
    
    gid}
        </where>
    </update>

猜你喜欢

转载自blog.csdn.net/qq_38152400/article/details/108345676