动态SQL实现商品的批量添加和删除(代码&结果图)

批量添加

int insertBatch(List<User> list);
<insert id="insertBatch">
    INSERT INTO t_user(id, name, del_flag)
    VALUES
    <foreach collection ="list" item="user" separator =",">
         (#{user.id}, #{user.name}, #{user.delFlag})
    </foreach >
</insert>


 批量删除

int booleanDeleteByIds(List<String> ids, int year); //mapper接口
<delete id="booleanDeleteByIds">
     update sys_user
     set bdel = 1
     where year = #{year}
     and id in
     <foreach collection="ids" item="id" open="(" separator="," close=")">
         #{id}
     </foreach>
 </delete>


 测试部分

@Test
    public void batchInsertAll() throws IOException {
        //加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        //获取SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //获取SqlSession工厂对象
        SqlSessionFactory build = builder.build(resourceAsStream);
        //获取SqlSession
        SqlSession sqlSession = build.openSession(true);//设置true自动提交
        //获取Mapper接口对象
        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
//        创建插入的数据
        List<Product> products = new ArrayList<>();
        Product product1=new Product();
        product1.setProductName("华为mate");
        product1.setDescription("麒麟");
        product1.setPrice("5000");
        product1.setImage("/com/yy/picture/picture1");
        product1.setInventory("100");
        product1.setIsSheelver(1);//表示已经上架
        product1.setAddTime("2022/3/11");
        product1.setUpdateTime("2022/3/16");
        products.add(product1);

        Product product2=new Product();
        product2.setProductName("荣耀V7");
        product2.setDescription("鲲鹏");
        product2.setPrice("3000");
        product2.setImage("/com/yy/picture/picture1");
        product2.setInventory("100");
        product2.setIsSheelver(1);//表示已经上架
        product2.setAddTime("2022/3/10");
        product2.setUpdateTime("2022/3/15");
        products.add(product2);

        Product product3=new Product();
        product3.setProductName("微软笔记本");
        product3.setDescription("四核处理器");
        product3.setPrice("3010");
        product3.setImage("/com/yy/picture/picture1");
        product3.setInventory("100");
        product3.setIsSheelver(1);//表示已经上架
        product3.setAddTime("2022/3/9");
        product3.setUpdateTime("2022/3/16");
        products.add(product3);

        int i = mapper.batchInsertProduct(products);
        System.out.println(i);
    }
    @Test
public void deleteByIds() throws IOException {
    //加载核心配置文件
    InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
    //获取SqlSessionFactoryBuilder
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    //获取SqlSession工厂对象
    SqlSessionFactory build = builder.build(resourceAsStream);
    //获取SqlSession
    SqlSession sqlSession = build.openSession(true);//设置true自动提交
    //获取Mapper接口对象
    ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);

    int[] ids = new int[]{20, 21, 22};
    int i = mapper.batchDeleteProduct(ids);
    System.out.println("运行结果:"+i);
    sqlSession.close();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

米莱虾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值