定义

getUpdateExpression: 动态生成update 语句

  • tableEntity: 表类型的实体。
  • filters: 筛选找出对应要更新数据。

举例

Java Code:

@Test
public void testUpdate() throws Exception {
        // find update row.
        FilterDescriptor idFilter =
                new FilterDescriptor(FilterCondition.AND, "productID", FilterOperator.EQUAL, "1");

        Product newProduct = new Product();
        // only update product name
        String productName = "modifiedName";
        newProduct.setProductName(productName);

        ParamExpression paramExpression = mybatisQueryProvider.getUpdateExpression(newProduct, idFilter);
        Map<String, Object> updateParam = new HashMap<>();
        updateParam.put("updateExpression", paramExpression.getExpression());
        updateParam.putAll(paramExpression.getParamMap());

        int result = northwindDao.update(updateParam);
        assertEquals(1, result);
}

mapper

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

xml:

<update id="update" parameterType="java.util.Map">
    ${updateExpression}
</update>

output:

JDBC Connection [ProxyConnection[PooledConnection[conn9: url=jdbc:h2:mem:default user=SA]]] will not be managed by Spring
==>  Preparing: UPDATE product SET `product_name`=? WHERE (product_id = ?) 
==> Parameters: modifiedName(String), 1(String)
<==    Updates: 1

results matching ""

    No results matching ""