View Javadoc
1   /*
2    *    Copyright 2016-2025 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 examples.generated.always.mybatis;
17  
18  import static examples.generated.always.mybatis.GeneratedAlwaysDynamicSqlSupport.firstName;
19  import static examples.generated.always.mybatis.GeneratedAlwaysDynamicSqlSupport.id;
20  import static examples.generated.always.mybatis.GeneratedAlwaysDynamicSqlSupport.lastName;
21  import static examples.generated.always.mybatis.PersonDynamicSqlSupport.person;
22  
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.ibatis.annotations.Insert;
29  import org.apache.ibatis.annotations.InsertProvider;
30  import org.apache.ibatis.annotations.Options;
31  import org.apache.ibatis.annotations.Param;
32  import org.apache.ibatis.annotations.SelectProvider;
33  import org.mybatis.dynamic.sql.BasicColumn;
34  import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider;
35  import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
36  import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
37  import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
38  import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
39  import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
40  import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
41  
42  import examples.generated.always.PersonRecord;
43  
44  public interface PersonMapper {
45  
46      @InsertProvider(type = SqlProviderAdapter.class, method = "insertSelect")
47      @Options(useGeneratedKeys = true, keyProperty = "parameters.id")
48      int insertSelect(InsertSelectStatementProvider insertSelectStatement);
49  
50      @InsertProvider(type = SqlProviderAdapter.class, method = "insert")
51      @Options(useGeneratedKeys = true, keyProperty = "row.id")
52      int insert(InsertStatementProvider<PersonRecord> insertStatement);
53  
54      @InsertProvider(type=SqlProviderAdapter.class, method="insertMultipleWithGeneratedKeys")
55      @Options(useGeneratedKeys = true, keyProperty = "records.id")
56      int insertMultiple(@Param("insertStatement") String insertStatement, @Param("records") List<PersonRecord> records);
57  
58      default int insertMultiple(MultiRowInsertStatementProvider<PersonRecord> multiRowInsertStatement) {
59          return insertMultiple(multiRowInsertStatement.getInsertStatement(), multiRowInsertStatement.getRecords());
60      }
61  
62      @SelectProvider(type = SqlProviderAdapter.class, method="select")
63      List<PersonRecord> selectMany(SelectStatementProvider selectStatement);
64  
65      // insertSelect when there are multiple generated keys expected
66      @Insert({
67          "${insertStatement}"
68      })
69      @Options(useGeneratedKeys = true, keyProperty = "keys.key")
70      int insertSelectMultiple(@Param("insertStatement") String insertStatement, @Param("parameters") Map<String, Object> parameters,
71              @Param("keys") GeneratedKeyList keys);
72  
73      default int insertSelect(InsertSelectStatementProvider insertSelectStatement, GeneratedKeyList keys) {
74          return insertSelectMultiple(insertSelectStatement.getInsertStatement(), insertSelectStatement.getParameters(), keys);
75      }
76  
77      default int insert(PersonRecord row) {
78          return MyBatis3Utils.insert(this::insert, row, person, c ->
79                  c.map(firstName).toProperty("firstName")
80                          .map(lastName).toProperty("lastName"));
81      }
82  
83      default int insertMultiple(PersonRecord...records) {
84          return insertMultiple(Arrays.asList(records));
85      }
86  
87      default int insertMultiple(Collection<PersonRecord> records) {
88          return MyBatis3Utils.insertMultiple(this::insertMultiple, records, person, c ->
89                  c.map(firstName).toProperty("firstName")
90                          .map(lastName).toProperty("lastName"));
91      }
92  
93      BasicColumn[] selectList =
94              BasicColumn.columnList(id, firstName, lastName);
95  
96      default List<PersonRecord> select(SelectDSLCompleter completer) {
97          return MyBatis3Utils.selectList(this::selectMany, selectList, person, completer);
98      }
99  }