定义
getInsertExpression: 动态生成Insert 语句
- tableEntity: 表类型的实体。
举例
Java Code:
@Test
public void testInsert() throws Exception {
Product newProduct = new Product();
Random random = new Random(10000L);
Integer id = random.nextInt();
newProduct.setProductID(id);
newProduct.setCategoryID(1);
String productName = "Product" + id;
newProduct.setProductName(productName);
// insert.
ParamExpression paramExpression = mybatisQueryProvider.getInsertExpression(newProduct);
Map<String, Object> insertParam = new HashMap<>();
insertParam.put("insertExpression", paramExpression.getExpression());
insertParam.putAll(paramExpression.getParamMap());
int result = northwindDao.insert(insertParam);
assertEquals(1, result);
}
mapper
@Mapper
public interface NorthwindDao {
// ...
int insert(Map<String, Object> params);
}
xml:
<insert id="insert" parameterType="java.util.Map">
${insertExpression}
</insert>
output:
JDBC Connection [ProxyConnection[PooledConnection[conn9: url=jdbc:h2:mem:default user=SA]]] will not be managed by Spring
==> Preparing: INSERT INTO product (product_id, price, product_name, category_id) VALUES (?, ?, ?, ?)
==> Parameters: -498702880(Integer), null, Product-498702880(String), 1(Integer)
<== Updates: 1