Dynamic SQL realizes batch addition and deletion of commodities (code & result graph)

Bulk add

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>


 batch deletion

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 section

@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();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325252029&siteId=291194637