Mybatis新增&修改

QixunQiu發表於2024-11-02

一 新增

點選檢視程式碼
void add(Brand brand);

<insert id="add" useGeneratedKeys="true" keyProperty="id"><!--主鍵返回-->
        insert into tb_brand (brand_name, company_name, ordered, description, status)
        values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});

    </insert>

@Test
    public void testAdd() throws IOException {
        //接收引數
        int status = 1;
        String companyName = "波導手機";
        String brandName = "波導";
        String description = "手機中的戰鬥機";
        int ordered = 100;


        //封裝物件
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);
        brand.setDescription(description);
        brand.setOrdered(ordered);

        //1. 獲取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 獲取SqlSession物件
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //3. 獲取Mapper介面的代理物件
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4. 執行方法

        brandMapper.add(brand);

        //提交事務
        sqlSession.commit();

        //5. 釋放資源
        sqlSession.close();

    }


    @Test
    public void testAdd2() throws IOException {
        //接收引數
        int status = 1;
        String companyName = "波導手機";
        String brandName = "波導";
        String description = "手機中的戰鬥機";
        int ordered = 100;


        //封裝物件
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);
        brand.setDescription(description);
        brand.setOrdered(ordered);

        //1. 獲取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 獲取SqlSession物件
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //3. 獲取Mapper介面的代理物件
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4. 執行方法

        brandMapper.add(brand);
        Integer id = brand.getId();
        System.out.println(id);

        //提交事務
        sqlSession.commit();

        //5. 釋放資源
        sqlSession.close();

    }

二 修改 1)修改全部欄位
點選檢視程式碼
int update(Brand brand);

<update id="update">
        update tb_brand
        set brand_name=#{brandName},
            company_name=#{companyName},
            ordered=#{ordered},
            description=#{description},
            status=#{status}
        where id =#{id};
    </update>
2)修改動態欄位
點選檢視程式碼
    <update id="update">
        update tb_brand
        <set>
            <if test="brandName != null and brandName != ''">
                brand_name = #{brandName},
            </if>
            <if test="companyName != null and companyName != ''">
                company_name = #{companyName},
            </if>
            <if test="ordered != null">
                ordered = #{ordered},
            </if>
            <if test="description != null and description != ''">
                description = #{description},
            </if>
            <if test="status != null">
                status = #{status}
            </if>
        </set>
        where id = #{id};
    </update>



@Test
    public void testUpdate() throws IOException {
        //接收引數
        int status = 0;
        String companyName = "波導手機";
        String brandName = "波導";
        String description = "波導手機,手機中的戰鬥機";
        int ordered = 200;
        int id = 6;


        //封裝物件
        Brand brand = new Brand();
        brand.setStatus(status);
//        brand.setCompanyName(companyName);
//        brand.setBrandName(brandName);
//        brand.setDescription(description);
//        brand.setOrdered(ordered);
        brand.setId(id);

        //1. 獲取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 獲取SqlSession物件
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //3. 獲取Mapper介面的代理物件
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4. 執行方法


        int count = brandMapper.update(brand);
        System.out.println(count);
        //提交事務
        sqlSession.commit();

        //5. 釋放資源
        sqlSession.close();

    }

相關文章