1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.mapping;
17
18 import java.sql.ResultSet;
19
20 import org.apache.ibatis.session.Configuration;
21 import org.apache.ibatis.type.JdbcType;
22 import org.apache.ibatis.type.TypeHandler;
23 import org.apache.ibatis.type.TypeHandlerRegistry;
24
25
26
27
28 public class ParameterMapping {
29
30 private Configuration configuration;
31
32 private String property;
33 private ParameterMode mode;
34 private Class<?> javaType = Object.class;
35 private JdbcType jdbcType;
36 private Integer numericScale;
37 private TypeHandler<?> typeHandler;
38 private String resultMapId;
39 private String jdbcTypeName;
40 private String expression;
41
42 private ParameterMapping() {
43 }
44
45 public static class Builder {
46 private final ParameterMapping parameterMapping = new ParameterMapping();
47
48 public Builder(Configuration configuration, String property, TypeHandler<?> typeHandler) {
49 parameterMapping.configuration = configuration;
50 parameterMapping.property = property;
51 parameterMapping.typeHandler = typeHandler;
52 parameterMapping.mode = ParameterMode.IN;
53 }
54
55 public Builder(Configuration configuration, String property, Class<?> javaType) {
56 parameterMapping.configuration = configuration;
57 parameterMapping.property = property;
58 parameterMapping.javaType = javaType;
59 parameterMapping.mode = ParameterMode.IN;
60 }
61
62 public Builder mode(ParameterMode mode) {
63 parameterMapping.mode = mode;
64 return this;
65 }
66
67 public Builder javaType(Class<?> javaType) {
68 parameterMapping.javaType = javaType;
69 return this;
70 }
71
72 public Builder jdbcType(JdbcType jdbcType) {
73 parameterMapping.jdbcType = jdbcType;
74 return this;
75 }
76
77 public Builder numericScale(Integer numericScale) {
78 parameterMapping.numericScale = numericScale;
79 return this;
80 }
81
82 public Builder resultMapId(String resultMapId) {
83 parameterMapping.resultMapId = resultMapId;
84 return this;
85 }
86
87 public Builder typeHandler(TypeHandler<?> typeHandler) {
88 parameterMapping.typeHandler = typeHandler;
89 return this;
90 }
91
92 public Builder jdbcTypeName(String jdbcTypeName) {
93 parameterMapping.jdbcTypeName = jdbcTypeName;
94 return this;
95 }
96
97 public Builder expression(String expression) {
98 parameterMapping.expression = expression;
99 return this;
100 }
101
102 public ParameterMapping build() {
103 resolveTypeHandler();
104 validate();
105 return parameterMapping;
106 }
107
108 private void validate() {
109 if (ResultSet.class.equals(parameterMapping.javaType)) {
110 if (parameterMapping.resultMapId == null) {
111 throw new IllegalStateException("Missing resultmap in property '" + parameterMapping.property + "'. "
112 + "Parameters of type java.sql.ResultSet require a resultmap.");
113 }
114 } else if (parameterMapping.typeHandler == null) {
115 throw new IllegalStateException("Type handler was null on parameter mapping for property '"
116 + parameterMapping.property + "'. It was either not specified and/or could not be found for the javaType ("
117 + parameterMapping.javaType.getName() + ") : jdbcType (" + parameterMapping.jdbcType + ") combination.");
118 }
119 }
120
121 private void resolveTypeHandler() {
122 if (parameterMapping.typeHandler == null && parameterMapping.javaType != null) {
123 Configuration configuration = parameterMapping.configuration;
124 TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
125 parameterMapping.typeHandler = typeHandlerRegistry.getTypeHandler(parameterMapping.javaType,
126 parameterMapping.jdbcType);
127 }
128 }
129
130 }
131
132 public String getProperty() {
133 return property;
134 }
135
136
137
138
139
140
141 public ParameterMode getMode() {
142 return mode;
143 }
144
145
146
147
148
149
150 public Class<?> getJavaType() {
151 return javaType;
152 }
153
154
155
156
157
158
159 public JdbcType getJdbcType() {
160 return jdbcType;
161 }
162
163
164
165
166
167
168 public Integer getNumericScale() {
169 return numericScale;
170 }
171
172
173
174
175
176
177 public TypeHandler<?> getTypeHandler() {
178 return typeHandler;
179 }
180
181
182
183
184
185
186 public String getResultMapId() {
187 return resultMapId;
188 }
189
190
191
192
193
194
195 public String getJdbcTypeName() {
196 return jdbcTypeName;
197 }
198
199
200
201
202
203
204 public String getExpression() {
205 return expression;
206 }
207
208 @Override
209 public String toString() {
210 final StringBuilder sb = new StringBuilder("ParameterMapping{");
211
212 sb.append("property='").append(property).append('\'');
213 sb.append(", mode=").append(mode);
214 sb.append(", javaType=").append(javaType);
215 sb.append(", jdbcType=").append(jdbcType);
216 sb.append(", numericScale=").append(numericScale);
217
218 sb.append(", resultMapId='").append(resultMapId).append('\'');
219 sb.append(", jdbcTypeName='").append(jdbcTypeName).append('\'');
220 sb.append(", expression='").append(expression).append('\'');
221 sb.append('}');
222 return sb.toString();
223 }
224 }