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 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.*;
25  import org.mybatis.dynamic.sql.BasicColumn;
26  import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
27  import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
28  import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
29  import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
30  import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
31  import org.mybatis.dynamic.sql.util.mybatis3.*;
32  
33  @CacheNamespace(implementation = ObservableCache.class)
34  public interface NameTableMapper extends CommonCountMapper, CommonDeleteMapper, CommonInsertMapper<NameRecord>, CommonUpdateMapper {
35      @SelectProvider(type=SqlProviderAdapter.class, method="select")
36      @Results(id="NameTableResult", value={
37              @Result(column="id", property="id", id=true),
38              @Result(column="name", property="name")
39      })
40      List<NameRecord> selectMany(SelectStatementProvider selectStatement);
41  
42      @SelectProvider(type=SqlProviderAdapter.class, method="select")
43      @ResultMap("NameTableResult")
44      Optional<NameRecord> selectOne(SelectStatementProvider selectStatement);
45  
46      BasicColumn[] selectList = BasicColumn.columnList(id, name);
47  
48      default Optional<NameRecord> selectOne(SelectDSLCompleter completer) {
49          return MyBatis3Utils.selectOne(this::selectOne, selectList, nameTable, completer);
50      }
51  
52      default Optional<NameRecord> selectByPrimaryKey(Integer recordId) {
53          return selectOne(c ->
54                  c.where(id, isEqualTo(recordId))
55          );
56      }
57  
58      default int insert(NameRecord row) {
59          return MyBatis3Utils.insert(this::insert, row, nameTable, c ->
60                  c.map(id).toProperty("id")
61                          .map(name).toProperty("name")
62          );
63      }
64  
65      default int update(UpdateDSLCompleter completer) {
66          return MyBatis3Utils.update(this::update, nameTable, completer);
67      }
68  
69      default int updateByPrimaryKey(NameRecord row) {
70          return update(c ->
71                  c.set(name).equalTo(row::getName)
72                          .where(id, isEqualTo(row::getId))
73          );
74      }
75  
76      default int deleteAll() {
77          return MyBatis3Utils.deleteFrom(this::delete, nameTable, DeleteDSLCompleter.allRows());
78      }
79  }