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 examples.simple;
17  
18  import static examples.simple.AddressDynamicSqlSupport.address;
19  import static examples.simple.PersonDynamicSqlSupport.birthDate;
20  import static examples.simple.PersonDynamicSqlSupport.employed;
21  import static examples.simple.PersonDynamicSqlSupport.firstName;
22  import static examples.simple.PersonDynamicSqlSupport.id;
23  import static examples.simple.PersonDynamicSqlSupport.lastName;
24  import static examples.simple.PersonDynamicSqlSupport.occupation;
25  import static examples.simple.PersonDynamicSqlSupport.person;
26  import static org.mybatis.dynamic.sql.SqlBuilder.*;
27  
28  import java.util.List;
29  import java.util.Optional;
30  
31  import org.apache.ibatis.annotations.Mapper;
32  import org.apache.ibatis.annotations.Result;
33  import org.apache.ibatis.annotations.ResultMap;
34  import org.apache.ibatis.annotations.Results;
35  import org.apache.ibatis.annotations.SelectProvider;
36  import org.apache.ibatis.type.EnumOrdinalTypeHandler;
37  import org.apache.ibatis.type.JdbcType;
38  import org.mybatis.dynamic.sql.BasicColumn;
39  import org.mybatis.dynamic.sql.dsl.CountDSL;
40  import org.mybatis.dynamic.sql.dsl.CountDSLCompleter;
41  import org.mybatis.dynamic.sql.dsl.SelectDSL;
42  import org.mybatis.dynamic.sql.dsl.SelectDSLCompleter;
43  import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
44  import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
45  import org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper;
46  import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
47  
48  /**
49   * This is a mapper that shows coding a join
50   */
51  @Mapper
52  public interface PersonWithAddressMapperV2 extends CommonCountMapper {
53  
54      @SelectProvider(type=SqlProviderAdapter.class, method="select")
55      @Results(id="PersonWithAddressResult", value= {
56              @Result(column="A_ID", property="id", jdbcType=JdbcType.INTEGER, id=true),
57              @Result(column="first_name", property="firstName", jdbcType=JdbcType.VARCHAR),
58              @Result(column="last_name", property="lastName", jdbcType=JdbcType.VARCHAR, typeHandler=LastNameTypeHandler.class),
59              @Result(column="birth_date", property="birthDate", jdbcType=JdbcType.DATE),
60              @Result(column="employed", property="employed", jdbcType=JdbcType.VARCHAR, typeHandler=YesNoTypeHandler.class),
61              @Result(column="occupation", property="occupation", jdbcType=JdbcType.VARCHAR),
62              @Result(column="address_id", property="address.id", jdbcType=JdbcType.INTEGER),
63              @Result(column="street_address", property="address.streetAddress", jdbcType=JdbcType.VARCHAR),
64              @Result(column="city", property="address.city", jdbcType=JdbcType.VARCHAR),
65              @Result(column="state", property="address.state", jdbcType=JdbcType.CHAR),
66              @Result(column="address_type", property="address.addressType", jdbcType=JdbcType.INTEGER,
67                      typeHandler = EnumOrdinalTypeHandler.class)
68      })
69      List<PersonWithAddress> selectMany(SelectStatementProvider selectStatement);
70  
71      @SelectProvider(type=SqlProviderAdapter.class, method="select")
72      @ResultMap("PersonWithAddressResult")
73      Optional<PersonWithAddress> selectOne(SelectStatementProvider selectStatement);
74  
75      BasicColumn[] selectList =
76              BasicColumn.columnList(id.as("A_ID"), firstName, lastName, birthDate, employed, occupation, address.id,
77                      address.streetAddress, address.city, address.state, address.addressType);
78  
79      default Optional<PersonWithAddress> selectOne(SelectDSLCompleter completer) {
80          SelectDSL start = SelectDSL.select(selectList).from(person)
81                  .join(address, on(person.addressId, isEqualTo(address.id))).endJoin();
82          return MyBatis3Utils.selectOne(this::selectOne, start, completer);
83      }
84  
85      default List<PersonWithAddress> select(SelectDSLCompleter completer) {
86          SelectDSL start = SelectDSL.select(selectList).from(person)
87                  .join(address, on(person.addressId, isEqualTo(address.id))).endJoin();
88          return MyBatis3Utils.selectList(this::selectMany, start, completer);
89      }
90  
91      default Optional<PersonWithAddress> selectByPrimaryKey(Integer recordId) {
92          return selectOne(c ->
93              c.where(id, isEqualTo(recordId))
94          );
95      }
96  
97      default long count(CountDSLCompleter completer) {
98          CountDSL start = CountDSL.countFrom(person)
99                  .join(address, on(person.addressId, isEqualTo(address.id))).endJoin();
100         return MyBatis3Utils.countFrom(this::count, start, completer);
101     }
102 }