1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }