View Javadoc
1   /*
2    *    Copyright 2009-2022 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 org.apache.ibatis.submitted.mapper_type_parameter;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.apache.ibatis.annotations.InsertProvider;
22  import org.apache.ibatis.annotations.MapKey;
23  import org.apache.ibatis.annotations.Options;
24  import org.apache.ibatis.annotations.SelectProvider;
25  import org.apache.ibatis.annotations.UpdateProvider;
26  
27  public interface BaseMapper<S, T> {
28  
29    @SelectProvider(type = StatementProvider.class, method = "provideSelect")
30    S select(S param);
31  
32    @SelectProvider(type = StatementProvider.class, method = "provideSelect")
33    List<S> selectList(S param);
34  
35    @SelectProvider(type = StatementProvider.class, method = "provideSelect")
36    @MapKey("id")
37    Map<T, S> selectMap(S param);
38  
39    @InsertProvider(type = StatementProvider.class, method = "provideInsert")
40    @Options(useGeneratedKeys = true, keyProperty = "id")
41    int insert(List<S> param);
42  
43    @UpdateProvider(type = StatementProvider.class, method = "provideUpdate")
44    int update(S param);
45  
46    class StatementProvider {
47      public String provideSelect(Object param) {
48        StringBuilder sql = new StringBuilder("select * from ");
49        if (param == null || param instanceof Person) {
50          sql.append(" person ");
51          if (param != null && ((Person) param).getId() != null) {
52            sql.append(" where id = #{id}");
53          }
54        } else if (param instanceof Country) {
55          sql.append(" country ");
56          if (((Country) param).getId() != null) {
57            sql.append(" where id = #{id}");
58          }
59        }
60        sql.append(" order by id");
61        return sql.toString();
62      }
63  
64      public String provideInsert(Map<String, Object> map) {
65        List<?> params = (List<?>) map.get("list");
66        StringBuilder sql = null;
67        for (int i = 0; i < params.size(); i++) {
68          Object param = params.get(i);
69          if (sql == null) {
70            sql = new StringBuilder("insert into ");
71            sql.append(param instanceof Country ? " country " : " person");
72            sql.append(" (id, name) values ");
73          } else {
74            sql.append(",");
75          }
76          sql.append(" (#{list[").append(i).append("].id}, #{list[").append(i).append("].name})");
77        }
78        return sql == null ? "" : sql.toString();
79      }
80  
81      public String provideUpdate(Object param) {
82        StringBuilder sql = new StringBuilder("update ");
83        if (param instanceof Person) {
84          sql.append(" person set name = #{name} where id = #{id}");
85        } else if (param instanceof Country) {
86          sql.append(" country set name = #{name} where id = #{id}");
87        }
88        return sql.toString();
89      }
90    }
91  }