From 7d3e833786fa1fe9dd96518404a441318005c70b Mon Sep 17 00:00:00 2001 From: Jeff Butler Date: Mon, 28 Nov 2022 16:36:58 -0500 Subject: [PATCH 1/3] Create a new insert mapper for general inserts This mapper doesn't include the typed inserts, so it can easily be used without a class that matches a table row. --- .../mybatis3/CommonGeneralInsertMapper.java | 49 +++++++++++++++++++ .../sql/util/mybatis3/CommonInsertMapper.java | 26 +--------- 2 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 src/main/java/org/mybatis/dynamic/sql/util/mybatis3/CommonGeneralInsertMapper.java diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/CommonGeneralInsertMapper.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/CommonGeneralInsertMapper.java new file mode 100644 index 000000000..94989d526 --- /dev/null +++ b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/CommonGeneralInsertMapper.java @@ -0,0 +1,49 @@ +/* + * Copyright 2016-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.dynamic.sql.util.mybatis3; + +import org.apache.ibatis.annotations.InsertProvider; +import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider; +import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider; +import org.mybatis.dynamic.sql.util.SqlProviderAdapter; + +/** + * This is a general purpose mapper for executing various non-typed insert statements (general inserts and insert + * selects). This mapper is appropriate for insert statements that do NOT expect generated keys. + */ +public interface CommonGeneralInsertMapper { + /** + * Execute an insert statement with input fields supplied directly. + * + * @param insertStatement + * the insert statement + * + * @return the number of rows affected + */ + @InsertProvider(type = SqlProviderAdapter.class, method = "generalInsert") + int generalInsert(GeneralInsertStatementProvider insertStatement); + + /** + * Execute an insert statement with input fields supplied by a select statement. + * + * @param insertSelectStatement + * the insert statement + * + * @return the number of rows affected + */ + @InsertProvider(type = SqlProviderAdapter.class, method = "insertSelect") + int insertSelect(InsertSelectStatementProvider insertSelectStatement); +} diff --git a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/CommonInsertMapper.java b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/CommonInsertMapper.java index 86715d734..834a1574a 100644 --- a/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/CommonInsertMapper.java +++ b/src/main/java/org/mybatis/dynamic/sql/util/mybatis3/CommonInsertMapper.java @@ -20,8 +20,6 @@ import org.apache.ibatis.annotations.Flush; import org.apache.ibatis.annotations.InsertProvider; import org.apache.ibatis.executor.BatchResult; -import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider; -import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider; import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider; import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider; import org.mybatis.dynamic.sql.util.SqlProviderAdapter; @@ -33,7 +31,7 @@ * @param * the type of record associated with this mapper */ -public interface CommonInsertMapper { +public interface CommonInsertMapper extends CommonGeneralInsertMapper { /** * Execute an insert statement with input fields mapped to values in a POJO. * @@ -45,28 +43,6 @@ public interface CommonInsertMapper { @InsertProvider(type = SqlProviderAdapter.class, method = "insert") int insert(InsertStatementProvider insertStatement); - /** - * Execute an insert statement with input fields supplied directly. - * - * @param insertStatement - * the insert statement - * - * @return the number of rows affected - */ - @InsertProvider(type = SqlProviderAdapter.class, method = "generalInsert") - int generalInsert(GeneralInsertStatementProvider insertStatement); - - /** - * Execute an insert statement with input fields supplied by a select statement. - * - * @param insertSelectStatement - * the insert statement - * - * @return the number of rows affected - */ - @InsertProvider(type = SqlProviderAdapter.class, method = "insertSelect") - int insertSelect(InsertSelectStatementProvider insertSelectStatement); - /** * Execute an insert statement that inserts multiple rows. The row values are supplied by mapping to values in a * List of POJOs. From b55ca05b6f07703e9ec41bc45f712a7b2ef94363 Mon Sep 17 00:00:00 2001 From: Jeff Butler Date: Sat, 7 Jan 2023 14:00:01 -0500 Subject: [PATCH 2/3] Documentation for new mapper --- CHANGELOG.md | 2 ++ src/site/markdown/docs/mybatis3.md | 17 +++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e53d227fb..2fd3a9066 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,8 @@ The pull request for this change is ([#550](https://github.com/mybatis/mybatis-d please test to make sure it is supported in your database. ([#544](https://github.com/mybatis/mybatis-dynamic-sql/pull/544)) 2. Deprecated Kotlin DSL functions have been removed, as well as deprecated support for "EmptyListCallback" in the "in" conditions. ([#548](https://github.com/mybatis/mybatis-dynamic-sql/pull/548)) +3. Refactored the common insert mapper support for MyBatis3 by adding a CommonGeneralInsertMapper that can be used + without a class that matches the table row. It includes methods for general insert and insert select. diff --git a/src/site/markdown/docs/mybatis3.md b/src/site/markdown/docs/mybatis3.md index e3ae2ab60..17eb9e5d2 100644 --- a/src/site/markdown/docs/mybatis3.md +++ b/src/site/markdown/docs/mybatis3.md @@ -23,14 +23,15 @@ JDBC template. These mappers provide utility functions that execute simple queries. They can be used as-as, or can be extended. They provide methods as follows: -| Mapper | Methods(s) | -|---|---| -| `org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper` | `long count(SelectStatementProvider)` | -| `org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper` | `int delete(DeleteStatementProvider)` -| `org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper` | `int insert(InsertStatementProvider)`
`int generalInsert(GeneralInsertStatementProvider)`
`int insertSelect(InsertSelectStatementProvider)`
`int insertMultiple(MultiRowInsertStatementProvider)` | -| `org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper` | `int update(UpdateStatementProvider)` | - -These mappers, as well as the common selectmapper, can be used to create a general purpose CRUD mapper as follows: +| Mapper | Methods(s) | +|--------------------------------------------------------------------------------------------------------|---| +| `org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper` | `long count(SelectStatementProvider)` | +| `org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper` | `int delete(DeleteStatementProvider)` +| `org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper`
(extends `CommonGeneralInsertMapper`) | `int insert(InsertStatementProvider)`
`int insertMultiple(MultiRowInsertStatementProvider)` | +| `org.mybatis.dynamic.sql.util.mybatis3.CommonGeneralInsertMapper` | `int generalInsert(GeneralInsertStatementProvider)`
`int insertSelect(InsertSelectStatementProvider)` | +| `org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper` | `int update(UpdateStatementProvider)` | + +These mappers, as well as the `CommonSelectMapper`, can be used to create a general purpose CRUD mapper as follows: ```java import org.apache.ibatis.annotations.Mapper; From 151c4e3ad16053bf2cb04b84c058021c40147c90 Mon Sep 17 00:00:00 2001 From: Jeff Butler Date: Sat, 7 Jan 2023 14:02:32 -0500 Subject: [PATCH 3/3] Add PR to changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fd3a9066..dc8c73d6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,8 @@ The pull request for this change is ([#550](https://github.com/mybatis/mybatis-d 2. Deprecated Kotlin DSL functions have been removed, as well as deprecated support for "EmptyListCallback" in the "in" conditions. ([#548](https://github.com/mybatis/mybatis-dynamic-sql/pull/548)) 3. Refactored the common insert mapper support for MyBatis3 by adding a CommonGeneralInsertMapper that can be used - without a class that matches the table row. It includes methods for general insert and insert select. + without a class that matches the table row. It includes methods for general insert and insert select. + ([#570](https://github.com/mybatis/mybatis-dynamic-sql/pull/570))