Skip to content

Commit 2d1c71e

Browse files
authored
Merge pull request #263 from jeffgbutler/general-mappers
Add Common MyBatis Mappers for CRUD Operations
2 parents 881ac46 + c01e494 commit 2d1c71e

File tree

20 files changed

+372
-213
lines changed

20 files changed

+372
-213
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
1414

1515
- Added a callback capability to the "In" conditions that will be called before rendering when the conditions are empty. Also, removed the option that forced the library to render invalid SQL in that case. ([#241](https://github.com/mybatis/mybatis-dynamic-sql/pull/241))
1616
- Added a utility mapper for MyBatis that allows you to run any select query without having to predefine a result mapping. ([#255](https://github.com/mybatis/mybatis-dynamic-sql/pull/255))
17+
- Added utility mappers for MyBatis that allow you to run generic CRUD operations. ([#263](https://github.com/mybatis/mybatis-dynamic-sql/pull/263))
1718

1819
## Release 1.2.0 - August 19, 2020
1920

checkstyle-override.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
1818
-->
1919
<!DOCTYPE module PUBLIC
20-
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
21-
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
20+
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
21+
"https://checkstyle.org/dtds/configuration_1_3.dtd">
2222

2323
<!--
2424
Checkstyle configuration that checks the Google coding conventions from Google Java Style

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,21 @@
201201
<artifactId>kotlin-stdlib-jdk8</artifactId>
202202
<version>${kotlin.version}</version>
203203
<scope>provided</scope>
204+
<optional>true</optional>
204205
</dependency>
205206
<dependency>
206207
<groupId>org.springframework</groupId>
207208
<artifactId>spring-jdbc</artifactId>
208209
<version>5.2.9.RELEASE</version>
209210
<scope>provided</scope>
211+
<optional>true</optional>
210212
</dependency>
211213
<dependency>
212214
<groupId>org.mybatis</groupId>
213215
<artifactId>mybatis</artifactId>
214216
<version>3.5.5</version>
215217
<scope>provided</scope>
218+
<optional>true</optional>
216219
</dependency>
217220

218221
<dependency>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2016-2020 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+
* http://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.SelectProvider;
19+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
20+
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
21+
22+
/**
23+
* This is a general purpose MyBatis mapper for count statements. Count statements are select statements that always
24+
* return a long.
25+
*
26+
* <p>This mapper can be injected as-is into a MyBatis configuration, or it can be extended with existing mappers.
27+
*
28+
* @author Jeff Butler
29+
*/
30+
public interface CommonCountMapper {
31+
/**
32+
* Execute a select statement that returns a long (typically a select(count(*)) statement). This mapper
33+
* assumes the statement returns a single row with a single column that cen be retrieved as a long.
34+
*
35+
* @param selectStatement the select statement
36+
* @return the long value
37+
*/
38+
@SelectProvider(type = SqlProviderAdapter.class, method = "select")
39+
long count(SelectStatementProvider selectStatement);
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2016-2020 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+
* http://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.DeleteProvider;
19+
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
20+
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
21+
22+
/**
23+
* This is a general purpose MyBatis mapper for delete statements.
24+
*
25+
* <p>This mapper can be injected as-is into a MyBatis configuration, or it can be extended with existing mappers.
26+
*
27+
* @author Jeff Butler
28+
*/
29+
public interface CommonDeleteMapper {
30+
/**
31+
* Execute a delete statement.
32+
*
33+
* @param deleteStatement the delete statement
34+
* @return the number of rows affected
35+
*/
36+
@DeleteProvider(type = SqlProviderAdapter.class, method = "delete")
37+
int delete(DeleteStatementProvider deleteStatement);
38+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2016-2020 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+
* http://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.insert.render.InsertStatementProvider;
22+
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
23+
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
24+
25+
/**
26+
* This is a general purpose mapper for executing various types of insert statement.
27+
*
28+
* @param <T> the type of record associated with this mapper
29+
*/
30+
public interface CommonInsertMapper<T> {
31+
/**
32+
* Execute an insert statement with input fields mapped to values in a POJO.
33+
*
34+
* @param insertStatement the insert statement
35+
* @return the number of rows affected
36+
*/
37+
@InsertProvider(type = SqlProviderAdapter.class, method = "insert")
38+
int insert(InsertStatementProvider<T> insertStatement);
39+
40+
/**
41+
* Execute an insert statement with input fields supplied directly.
42+
*
43+
* @param insertStatement the insert statement
44+
* @return the number of rows affected
45+
*/
46+
@InsertProvider(type = SqlProviderAdapter.class, method = "generalInsert")
47+
int generalInsert(GeneralInsertStatementProvider insertStatement);
48+
49+
/**
50+
* Execute an insert statement with input fields supplied by a select statement.
51+
*
52+
* @param insertSelectStatement the insert statement
53+
* @return the number of rows affected
54+
*/
55+
@InsertProvider(type = SqlProviderAdapter.class, method = "insertSelect")
56+
int insertSelect(InsertSelectStatementProvider insertSelectStatement);
57+
58+
/**
59+
* Execute an insert statement that inserts multiple rows. The row values are supplied by mapping
60+
* to values in a List of POJOs.
61+
*
62+
* @param insertStatement the insert statement
63+
* @return the number of rows affected
64+
*/
65+
@InsertProvider(type = SqlProviderAdapter.class, method = "insertMultiple")
66+
int insertMultiple(MultiRowInsertStatementProvider<T> insertStatement);
67+
}

src/main/java/org/mybatis/dynamic/sql/util/mybatis3/GeneralMapper.java renamed to src/main/java/org/mybatis/dynamic/sql/util/mybatis3/CommonSelectMapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
2828

2929
/**
30-
* This is a general purpose MyBatis mapper. It allows you to execute select statements without having to
31-
* write a custom {@link org.apache.ibatis.annotations.ResultMap} for each statement.
30+
* This is a general purpose MyBatis mapper for select statements. It allows you to execute select statements without
31+
* having to write a custom {@link org.apache.ibatis.annotations.ResultMap} for each statement.
3232
*
3333
* <p>This mapper contains three types of methods:
3434
* <ul>
@@ -46,7 +46,7 @@
4646
*
4747
* @author Jeff Butler
4848
*/
49-
public interface GeneralMapper {
49+
public interface CommonSelectMapper {
5050
/**
5151
* Select a single row as a Map of values. The row may have any number of columns.
5252
* The Map key will be the column name as returned from the
@@ -67,7 +67,7 @@ public interface GeneralMapper {
6767
* the row values into a Map, and then a row mapper can retrieve values from the Map and use them
6868
* to construct a custom object.
6969
*
70-
* <p>See {@link GeneralMapper#selectOneMappedRow(SelectStatementProvider)} for details about
70+
* <p>See {@link CommonSelectMapper#selectOneMappedRow(SelectStatementProvider)} for details about
7171
* how MyBatis will construct the Map of values.
7272
*
7373
* @param selectStatement the select statement
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2016-2020 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+
* http://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.UpdateProvider;
19+
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
20+
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
21+
22+
/**
23+
* This is a general purpose MyBatis mapper for update statements.
24+
*
25+
* <p>This mapper can be injected as-is into a MyBatis configuration, or it can be extended with existing mappers.
26+
*
27+
* @author Jeff Butler
28+
*/
29+
public interface CommonUpdateMapper {
30+
/**
31+
* Execute an update statement.
32+
*
33+
* @param updateStatement the update statement
34+
* @return the number of rows affected
35+
*/
36+
@UpdateProvider(type = SqlProviderAdapter.class, method = "update")
37+
int update(UpdateStatementProvider updateStatement);
38+
}

0 commit comments

Comments
 (0)