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 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 '"
112 + parameterMapping.property + "'. "
113 + "Parameters of type java.sql.ResultSet require a resultmap.");
114 }
115 } else {
116 if (parameterMapping.typeHandler == null) {
117 throw new IllegalStateException("Type handler was null on parameter mapping for property '"
118 + parameterMapping.property + "'. It was either not specified and/or could not be found for the javaType ("
119 + parameterMapping.javaType.getName() + ") : jdbcType (" + parameterMapping.jdbcType + ") combination.");
120 }
121 }
122 }
123
124 private void resolveTypeHandler() {
125 if (parameterMapping.typeHandler == null && parameterMapping.javaType != null) {
126 Configuration configuration = parameterMapping.configuration;
127 TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
128 parameterMapping.typeHandler = typeHandlerRegistry.getTypeHandler(parameterMapping.javaType, parameterMapping.jdbcType);
129 }
130 }
131
132 }
133
134 public String getProperty() {
135 return property;
136 }
137
138
139
140
141
142
143 public ParameterMode getMode() {
144 return mode;
145 }
146
147
148
149
150
151
152 public Class<?> getJavaType() {
153 return javaType;
154 }
155
156
157
158
159
160
161 public JdbcType getJdbcType() {
162 return jdbcType;
163 }
164
165
166
167
168
169
170 public Integer getNumericScale() {
171 return numericScale;
172 }
173
174
175
176
177
178
179 public TypeHandler<?> getTypeHandler() {
180 return typeHandler;
181 }
182
183
184
185
186
187
188 public String getResultMapId() {
189 return resultMapId;
190 }
191
192
193
194
195
196
197 public String getJdbcTypeName() {
198 return jdbcTypeName;
199 }
200
201
202
203
204
205
206 public String getExpression() {
207 return expression;
208 }
209
210 @Override
211 public String toString() {
212 final StringBuilder sb = new StringBuilder("ParameterMapping{");
213
214 sb.append("property='").append(property).append('\'');
215 sb.append(", mode=").append(mode);
216 sb.append(", javaType=").append(javaType);
217 sb.append(", jdbcType=").append(jdbcType);
218 sb.append(", numericScale=").append(numericScale);
219
220 sb.append(", resultMapId='").append(resultMapId).append('\'');
221 sb.append(", jdbcTypeName='").append(jdbcTypeName).append('\'');
222 sb.append(", expression='").append(expression).append('\'');
223 sb.append('}');
224 return sb.toString();
225 }
226 }