定义

getBulkInsertExpression: 动态生成批量 Insert 语句

  • entities: 表类型的集合。

举例

Java Code:

@Test
public void testBulkInsert() throws Exception {
        Product p1 = new Product();
        p1.setProductName("product1");
        p1.setPrice(BigDecimal.valueOf(500));
        p1.setCategoryID(2);

        Product p2 = new Product();
        p2.setProductName("product2");
        p2.setPrice(BigDecimal.valueOf(1000));
        p2.setCategoryID(2);

        Product[] products = new Product[]{p1, p2};

        ParamExpression paramExpression = mybatisQueryProvider.getBulkInsertExpression(products);
        Map<String, Object> insertParam = new HashMap<>();
        insertParam.put("insertExpression", paramExpression.getExpression());
        insertParam.putAll(paramExpression.getParamMap());

        int result = northwindDao.insert(insertParam);
        assertEquals(2, result);
}

mapper

@Mapper
public interface NorthwindDao {
    // ...
    int insert(Map<String, Object> params);
}

xml:

<insert id="insert" parameterType="java.util.Map">
     ${insertExpression}
</insert>

output:

==>  Preparing: INSERT INTO product (price, product_name, category_id) VALUES (?, ?, ?),(?, ?, ?) 
==> Parameters: 500(BigDecimal), product1(String), 2(Integer), 1000(BigDecimal), product2(String), 2(Integer)
<==    Updates: 2

results matching ""

    No results matching ""