JavaWeb——MyBatis(配置文件完成增删改)

1.添加

 

    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 add() throws IOException {
        //接受参数
        String brandName = "vivo";
        String companyName = "维沃移动通信有限公司";
        int ordered = 1000;
        String description = "致力于为消费者打造拥有极致拍照、畅快游戏、Hi-Fi音乐的智能手机产品";
        int status = 1;

        //封装对象
        Brand brand = new Brand();
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setOrdered(ordered);
        brand.setDescription(description);
        brand.setStatus(status);

        //1.加载核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2.获取对应的SqlSession对象,用它执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        SqlSession sqlSession = sqlSessionFactory.openSession(false);//关闭自动提交,默认
//        SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交

        //3获取userMapper接口的代理对象(接口的实现类对象)
        BrandMapper BrandMapper = sqlSession.getMapper(BrandMapper.class);

        //4.执行sql
        BrandMapper.add(brand);
        int id = brand.getId();
        System.out.println(id);

        //5.提交事务
        sqlSession.commit();

        //6.释放资源
        sqlSession.close();
    }

2.动态修改

    int update(Brand brand);//修改,返回影响的行数

 <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 and ordered != ''">
                ordered = #{ordered},
            </if>
            <if test="description != null and description != ''">
                description = #{description},
            </if>
            <if test="status != null and status != ''">
                status = #{status}
            </if>
        </set>
        where
        id = #{id}
    </update>

 @Test
    public void testUpdate() throws IOException {
        //接受参数
        String brandName = "vivo";
        String companyName = "维沃移动通信有限公司";
        int ordered = 300;
        String description = "致力于为消费者打造拥有极致拍照、畅快游戏、Hi-Fi音乐的智能手机产品";
        int status = 1;
        int id = 4;

        //封装对象
        Brand brand = new Brand();
//        brand.setBrandName(brandName);
//        brand.setCompanyName(companyName);
        brand.setOrdered(ordered);
        brand.setDescription(description);
//        brand.setStatus(status);
        brand.setId(id);

        //1.加载核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2.获取对应的SqlSession对象,用它执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交

        //3获取userMapper接口的代理对象(接口的实现类对象)
        BrandMapper BrandMapper = sqlSession.getMapper(BrandMapper.class);

        //4.执行sql
        int count = BrandMapper.update(brand);
        System.out.println(count);

        //5.释放资源
        sqlSession.close();
    }

3.删除

3.1删除一个

    void deleteById(int id);//根据id删除数据

 

  <delete id="deleteById">
        DELETE from tb_brand WHERE id = #{id}
  </delete>

    @Test
    public void testDeleteById() throws IOException {
        //接受参数
        int id = 5;

        //1.加载核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2.获取对应的SqlSession对象,用它执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交

        //3获取userMapper接口的代理对象(接口的实现类对象)
        BrandMapper BrandMapper = sqlSession.getMapper(BrandMapper.class);

        //4.执行sql
        BrandMapper.deleteById(id);

        //5.释放资源
        sqlSession.close();
    }

3.2批量删除

 

 

    void deleteByIds(@Param("ids") int[] ids);//批量删除数据

    //void deleteByIds(int[] ids);//批量删除数据

 

    <delete id="deleteByIds">
        DELETE FROM tb_brand WHERE id in (
        <foreach collection="ids" item="id" separator=",">
            #{id}
        </foreach>
        )
    </delete>


    <!--<delete id="deleteByIds">-->
        <!--DELETE FROM tb_brand WHERE id in (-->
        <!--<foreach collection="array" item="id" separator=",">-->
            <!--#{id}-->
        <!--</foreach>-->
        <!--)-->
    <!--</delete>-->

  @Test
    public void testDeleteByIds() throws IOException {
        //接受参数
        int[] ids = {6,7};

        //1.加载核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2.获取对应的SqlSession对象,用它执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交

        //3获取userMapper接口的代理对象(接口的实现类对象)
        BrandMapper BrandMapper = sqlSession.getMapper(BrandMapper.class);

        //4.执行sql
        BrandMapper.deleteByIds(ids);

        //5.释放资源
        sqlSession.close();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值