View Javadoc
1   /*
2    *    Copyright 2009-2023 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.typehandler;
17  
18  import java.sql.CallableStatement;
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.SQLException;
22  
23  import org.apache.ibatis.type.BaseTypeHandler;
24  import org.apache.ibatis.type.JdbcType;
25  
26  public class Product {
27  
28    private ProductId id;
29  
30    private String name;
31  
32    public Product() {
33    }
34  
35    public Product(ProductId id, String name) {
36      this.id = id;
37      this.name = name;
38    }
39  
40    public ProductId getId() {
41      return id;
42    }
43  
44    public void setId(ProductId id) {
45      this.id = id;
46    }
47  
48    public String getName() {
49      return name;
50    }
51  
52    public void setName(String name) {
53      this.name = name;
54    }
55  
56    public static class ProductId {
57      private Integer value;
58  
59      public Integer getValue() {
60        return value;
61      }
62  
63      public void setValue(Integer value) {
64        this.value = value;
65      }
66    }
67  
68    public static class ProductIdTypeHandler extends BaseTypeHandler<ProductId> {
69      @Override
70      public void setNonNullParameter(PreparedStatement ps, int i, ProductId parameter, JdbcType jdbcType)
71          throws SQLException {
72        ps.setInt(i, parameter.getValue());
73      }
74  
75      @Override
76      public ProductId getNullableResult(ResultSet rs, String columnName) throws SQLException {
77        ProductId id = new ProductId();
78        id.setValue(rs.getInt(columnName));
79        return id;
80      }
81  
82      @Override
83      public ProductId getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
84        ProductId id = new ProductId();
85        id.setValue(rs.getInt(columnIndex));
86        return id;
87      }
88  
89      @Override
90      public ProductId getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
91        ProductId id = new ProductId();
92        id.setValue(cs.getInt(columnIndex));
93        return id;
94      }
95    }
96  
97    public static class ConstantProductIdTypeHandler extends BaseTypeHandler<ProductId> {
98      @Override
99      public void setNonNullParameter(PreparedStatement ps, int i, ProductId parameter, JdbcType jdbcType)
100         throws SQLException {
101     }
102 
103     @Override
104     public ProductId getNullableResult(ResultSet rs, String columnName) throws SQLException {
105       return getConstantId();
106     }
107 
108     @Override
109     public ProductId getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
110       return getConstantId();
111     }
112 
113     @Override
114     public ProductId getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
115       return getConstantId();
116     }
117 
118     private ProductId getConstantId() {
119       ProductId id = new ProductId();
120       id.setValue(999);
121       return id;
122     }
123   }
124 }