Mybatis 新增、修改


使用selectKey

  

        <!--數據插入操作-->
        <insert id="insertGoods" parameterType="com.imooc.mybatis.entity.GoodsEntity">
            <!-- t_goods數據庫字段屬性,values里javaBean字段屬性一一映射 -->
            insert into t_goods(title,sub_title,original_cost,current_price,discount,is_free_delivery,category_id)
            values (#{title},#{subTitle},#{originalCost},#{currentPrice},#{discount},#{isFreeDelivery},#{categoryId})

            <selectKey resultType="Integer" keyProperty="goodsId" order="AFTER">
                select last_insert_id()
            </selectKey>
        </insert>
    public void testInsert() throws Exception {
        SqlSession sqlSession = null;
        try {
            //獲取sql對象
            sqlSession = MybatisUtils.openSession();
            //實例化goods,插入數據
            GoodsEntity goodsEntity = new GoodsEntity();
            goodsEntity.setTitle("測試商品");
            goodsEntity.setSubTitle("測試商品子標題");
            goodsEntity.setOriginalCost(1000f);
            goodsEntity.setCurrentPrice(800f);
            goodsEntity.setDiscount(0.8f);
            goodsEntity.setIsFreeDelivery(1);
            goodsEntity.setCategoryId(43);
            //執行sql
            int num = sqlSession.insert("goods.insertGoods",goodsEntity);
            //提交數據
            sqlSession.commit();
            System.out.println(goodsEntity.getGoodsId());
            //查看連接狀態
            Connection conn = MybatisUtils.getConnection(sqlSession);

        }catch (Exception e){
            sqlSession.rollback();//數據回滾
            throw e;
        }finally {
            MybatisUtils.release(sqlSession);
        }
    }

使用 useGeneratedKeys

     <!--數據插入操作-->
        <insert id="insertCategory" parameterType="com.imooc.mybatis.entity.CategoryEntity" useGeneratedKeys="true"
                keyProperty="categoryId" keyColumn="category_id">
            insert into t_category(category_name,parent_id,category_level,category_order)
            values (#{categoryName},#{parentId},#{categoryLevel},#{categoryOrder})
        </insert>

useGeneratedKeys 只能使用於主鍵自增加的關系性數據庫

selectKey 適用於所有關系型數據庫

 

修改操作

        <!--數據修改操作-->
        <update id="updateGoods" parameterType="com.imooc.mybatis.entity.GoodsEntity">
            update t_goods set title = #{title} where goods_id = #{goodsId}
        </update>
    public void testUpdate() throws Exception {
        SqlSession sqlSession = null;
        try {
            //獲取sql對象
            sqlSession = MybatisUtils.openSession();
            //實例化goods,插入數據
            GoodsEntity goodsEntity = new GoodsEntity();
            goodsEntity.setTitle("修改測試");
            goodsEntity.setGoodsId(2674);
            //執行sql
            int num = sqlSession.update("goods.updateGoods",goodsEntity);
            //提交數據
            sqlSession.commit();
            System.out.println(goodsEntity.getTitle());
            //查看連接狀態
            Connection conn = MybatisUtils.getConnection(sqlSession);

        }catch (Exception e){
            sqlSession.rollback();//數據回滾
            throw e;
        }finally {
            MybatisUtils.release(sqlSession);
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM