Skip to content

Commit d8b313d

Browse files
authored
Merge pull request #570 from jeffgbutler/common-general-insert-mapper
Add CommonGeneralInsert mapper
2 parents 599ca8a + 151c4e3 commit d8b313d

File tree

4 files changed

+62
-33
lines changed

4 files changed

+62
-33
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ The pull request for this change is ([#550](https://github.com/mybatis/mybatis-d
4343
please test to make sure it is supported in your database. ([#544](https://github.com/mybatis/mybatis-dynamic-sql/pull/544))
4444
2. Deprecated Kotlin DSL functions have been removed, as well as deprecated support for "EmptyListCallback" in the "in"
4545
conditions. ([#548](https://github.com/mybatis/mybatis-dynamic-sql/pull/548))
46+
3. Refactored the common insert mapper support for MyBatis3 by adding a CommonGeneralInsertMapper that can be used
47+
without a class that matches the table row. It includes methods for general insert and insert select.
48+
([#570](https://github.com/mybatis/mybatis-dynamic-sql/pull/570))
4649

4750

4851

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2016-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util.mybatis3;
17+
18+
import org.apache.ibatis.annotations.InsertProvider;
19+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
20+
import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider;
21+
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
22+
23+
/**
24+
* This is a general purpose mapper for executing various non-typed insert statements (general inserts and insert
25+
* selects). This mapper is appropriate for insert statements that do NOT expect generated keys.
26+
*/
27+
public interface CommonGeneralInsertMapper {
28+
/**
29+
* Execute an insert statement with input fields supplied directly.
30+
*
31+
* @param insertStatement
32+
* the insert statement
33+
*
34+
* @return the number of rows affected
35+
*/
36+
@InsertProvider(type = SqlProviderAdapter.class, method = "generalInsert")
37+
int generalInsert(GeneralInsertStatementProvider insertStatement);
38+
39+
/**
40+
* Execute an insert statement with input fields supplied by a select statement.
41+
*
42+
* @param insertSelectStatement
43+
* the insert statement
44+
*
45+
* @return the number of rows affected
46+
*/
47+
@InsertProvider(type = SqlProviderAdapter.class, method = "insertSelect")
48+
int insertSelect(InsertSelectStatementProvider insertSelectStatement);
49+
}

src/main/java/org/mybatis/dynamic/sql/util/mybatis3/CommonInsertMapper.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import org.apache.ibatis.annotations.Flush;
2121
import org.apache.ibatis.annotations.InsertProvider;
2222
import org.apache.ibatis.executor.BatchResult;
23-
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
24-
import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider;
2523
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
2624
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
2725
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
@@ -33,7 +31,7 @@
3331
* @param <T>
3432
* the type of record associated with this mapper
3533
*/
36-
public interface CommonInsertMapper<T> {
34+
public interface CommonInsertMapper<T> extends CommonGeneralInsertMapper {
3735
/**
3836
* Execute an insert statement with input fields mapped to values in a POJO.
3937
*
@@ -45,28 +43,6 @@ public interface CommonInsertMapper<T> {
4543
@InsertProvider(type = SqlProviderAdapter.class, method = "insert")
4644
int insert(InsertStatementProvider<T> insertStatement);
4745

48-
/**
49-
* Execute an insert statement with input fields supplied directly.
50-
*
51-
* @param insertStatement
52-
* the insert statement
53-
*
54-
* @return the number of rows affected
55-
*/
56-
@InsertProvider(type = SqlProviderAdapter.class, method = "generalInsert")
57-
int generalInsert(GeneralInsertStatementProvider insertStatement);
58-
59-
/**
60-
* Execute an insert statement with input fields supplied by a select statement.
61-
*
62-
* @param insertSelectStatement
63-
* the insert statement
64-
*
65-
* @return the number of rows affected
66-
*/
67-
@InsertProvider(type = SqlProviderAdapter.class, method = "insertSelect")
68-
int insertSelect(InsertSelectStatementProvider insertSelectStatement);
69-
7046
/**
7147
* Execute an insert statement that inserts multiple rows. The row values are supplied by mapping to values in a
7248
* List of POJOs.

src/site/markdown/docs/mybatis3.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ JDBC template.
2323
These mappers provide utility functions that execute simple queries. They can be used as-as, or can be extended. They
2424
provide methods as follows:
2525

26-
| Mapper | Methods(s) |
27-
|---|---|
28-
| `org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper` | `long count(SelectStatementProvider)` |
29-
| `org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper` | `int delete(DeleteStatementProvider)`
30-
| `org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper<T>` | `int insert(InsertStatementProvider<T>)`<br/>`int generalInsert(GeneralInsertStatementProvider)`<br/>`int insertSelect(InsertSelectStatementProvider)`<br/>`int insertMultiple(MultiRowInsertStatementProvider<T>)` |
31-
| `org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper` | `int update(UpdateStatementProvider)` |
32-
33-
These mappers, as well as the common selectmapper, can be used to create a general purpose CRUD mapper as follows:
26+
| Mapper | Methods(s) |
27+
|--------------------------------------------------------------------------------------------------------|---|
28+
| `org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper` | `long count(SelectStatementProvider)` |
29+
| `org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper` | `int delete(DeleteStatementProvider)`
30+
| `org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper<T>`<br/>(extends `CommonGeneralInsertMapper`) | `int insert(InsertStatementProvider<T>)`<br/>`int insertMultiple(MultiRowInsertStatementProvider<T>)` |
31+
| `org.mybatis.dynamic.sql.util.mybatis3.CommonGeneralInsertMapper` | `int generalInsert(GeneralInsertStatementProvider)`<br/>`int insertSelect(InsertSelectStatementProvider)` |
32+
| `org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper` | `int update(UpdateStatementProvider)` |
33+
34+
These mappers, as well as the `CommonSelectMapper`, can be used to create a general purpose CRUD mapper as follows:
3435

3536
```java
3637
import org.apache.ibatis.annotations.Mapper;

0 commit comments

Comments
 (0)