View Javadoc
1   /*
2    *    Copyright 2016-2026 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 issues.gh324;
17  
18  import static issues.gh324.NameTableDynamicSqlSupport.*;
19  import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
20  
21  import java.util.List;
22  import java.util.Optional;
23  
24  import org.apache.ibatis.annotations.Arg;
25  import org.apache.ibatis.annotations.CacheNamespace;
26  import org.apache.ibatis.annotations.SelectProvider;
27  import org.mybatis.dynamic.sql.BasicColumn;
28  import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
29  import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
30  import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
31  import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
32  import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
33  import org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper;
34  import org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper;
35  import org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper;
36  import org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper;
37  import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
38  
39  @CacheNamespace(implementation = ObservableCache.class)
40  public interface NameTableMapper extends CommonCountMapper, CommonDeleteMapper, CommonInsertMapper<NameRecord>, CommonUpdateMapper {
41      @SelectProvider(type=SqlProviderAdapter.class, method="select")
42      @Arg(column = "id", javaType = Integer.class, id = true)
43      @Arg(column = "name", javaType = String.class)
44      List<NameRecord> selectMany(SelectStatementProvider selectStatement);
45  
46      @SelectProvider(type=SqlProviderAdapter.class, method="select")
47      @Arg(column = "id", javaType = Integer.class, id = true)
48      @Arg(column = "name", javaType = String.class)
49      Optional<NameRecord> selectOne(SelectStatementProvider selectStatement);
50  
51      BasicColumn[] selectList = BasicColumn.columnList(id, name);
52  
53      default Optional<NameRecord> selectOne(SelectDSLCompleter completer) {
54          return MyBatis3Utils.selectOne(this::selectOne, selectList, nameTable, completer);
55      }
56  
57      default Optional<NameRecord> selectByPrimaryKey(Integer recordId) {
58          return selectOne(c ->
59                  c.where(id, isEqualTo(recordId))
60          );
61      }
62  
63      default int insert(NameRecord row) {
64          return MyBatis3Utils.insert(this::insert, row, nameTable, c ->
65                  c.map(id).toProperty("id")
66                          .map(name).toProperty("name")
67          );
68      }
69  
70      default int update(UpdateDSLCompleter completer) {
71          return MyBatis3Utils.update(this::update, nameTable, completer);
72      }
73  
74      default int updateByPrimaryKey(NameRecord row) {
75          return update(c ->
76                  c.set(name).equalTo(row::name)
77                          .where(id, isEqualTo(row::id))
78          );
79      }
80  
81      default int deleteAll() {
82          return MyBatis3Utils.deleteFrom(this::delete, nameTable, DeleteDSLCompleter.allRows());
83      }
84  }