Commit ab78e9de8cd2987e951310aa9215b21eeb906a43
1 parent
c2088608
Exists in
master
1.upgrade repository version
Showing
31 changed files
with
293 additions
and
4833 deletions
Show diff stats
build.gradle
... | ... | @@ -21,9 +21,7 @@ dependencies { |
21 | 21 | compile("org.springframework.boot:spring-boot-starter:2.0.5.RELEASE") |
22 | 22 | compile("org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final") |
23 | 23 | compile('org.springframework:spring-jdbc:5.1.9.RELEASE') |
24 | - compile('com.alibaba:druid:1.2.4') | |
25 | - compile("org.apache.shardingsphere:shardingsphere-jdbc-core-spring-boot-starter:5.0.0-alpha") | |
26 | - compile("org.apache.shardingsphere:shardingsphere-infra-binder:6.0.0-alpha") | |
24 | + compile('com.alibaba:druid-spring-boot-starter:1.2.4') | |
27 | 25 | testCompile('mysql:mysql-connector-java:8.0.11') |
28 | 26 | } |
29 | 27 | |
... | ... | @@ -58,7 +56,7 @@ uploadArchives { |
58 | 56 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) |
59 | 57 | } |
60 | 58 | pom.project { |
61 | - version '2.2.4' | |
59 | + version '2.3.0' | |
62 | 60 | artifactId ARTIFACT_Id |
63 | 61 | groupId GROUP_ID |
64 | 62 | packaging TYPE | ... | ... |
src/main/java/com/taover/repository/CustomJdbcTemplate.java
... | ... | @@ -1,929 +0,0 @@ |
1 | -package com.taover.repository; | |
2 | - | |
3 | -import java.io.Serializable; | |
4 | -import java.lang.reflect.Field; | |
5 | -import java.lang.reflect.ParameterizedType; | |
6 | -import java.math.BigDecimal; | |
7 | -import java.sql.Connection; | |
8 | -import java.sql.PreparedStatement; | |
9 | -import java.sql.SQLException; | |
10 | -import java.util.ArrayList; | |
11 | -import java.util.HashMap; | |
12 | -import java.util.Iterator; | |
13 | -import java.util.List; | |
14 | -import java.util.Map; | |
15 | - | |
16 | -import javax.annotation.Resource; | |
17 | -import javax.persistence.Column; | |
18 | -import javax.persistence.Id; | |
19 | -import javax.persistence.Table; | |
20 | - | |
21 | -import org.springframework.dao.DataRetrievalFailureException; | |
22 | -import org.springframework.jdbc.core.ArgumentPreparedStatementSetter; | |
23 | -import org.springframework.jdbc.core.JdbcTemplate; | |
24 | -import org.springframework.jdbc.core.PreparedStatementCreator; | |
25 | -import org.springframework.jdbc.core.PreparedStatementSetter; | |
26 | -import org.springframework.jdbc.support.GeneratedKeyHolder; | |
27 | -import org.springframework.jdbc.support.KeyHolder; | |
28 | - | |
29 | -import com.taover.repository.mapper.CustomJdbcTemplateRowMapper; | |
30 | -import com.taover.repository.util.UtilsSql; | |
31 | - | |
32 | -/** | |
33 | - * | |
34 | - * @author root | |
35 | - * | |
36 | - * @param <T> | |
37 | - * @param <ID> | |
38 | - */ | |
39 | -public class CustomJdbcTemplate<T, ID extends Serializable> { | |
40 | - @Resource | |
41 | - private JdbcTemplate jdbcTemplateRead; | |
42 | - @Resource | |
43 | - private JdbcTemplate jdbcTemplateWrite; | |
44 | - | |
45 | - private Map<String, String> beanToTableField; | |
46 | - private Map<String, String> tableToBeanField; | |
47 | - private String tableFieldNameListGapWithComma; | |
48 | - private String idTableFieldName; | |
49 | - private String idBeanFieldName; | |
50 | - private String dbName; | |
51 | - private String tableName; | |
52 | - private Class<T> tClassInfo; | |
53 | - private CustomJdbcTemplateRowMapper customJdbcTemplateRowMapper; | |
54 | - | |
55 | - public CustomJdbcTemplateRowMapper getCustomJdbcTemplateRowMapper(){ | |
56 | - return this.customJdbcTemplateRowMapper; | |
57 | - } | |
58 | - | |
59 | - public CustomJdbcTemplate(JdbcTemplate jdbcTemplateWrite) throws Exception{ | |
60 | - this(); | |
61 | - this.jdbcTemplateWrite = jdbcTemplateWrite; | |
62 | - this.jdbcTemplateRead = jdbcTemplateWrite; | |
63 | - } | |
64 | - | |
65 | - public CustomJdbcTemplate() throws Exception{ | |
66 | - //获取泛型类Class | |
67 | - this.tClassInfo = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]; | |
68 | - | |
69 | - //检查实体声明 | |
70 | - Table annoTable = (Table) tClassInfo.getAnnotation(Table.class); | |
71 | - if(annoTable == null){ | |
72 | - throw new Exception("DAO层初始化失败,失败原因:"+tClassInfo.getName()+"实体类,没有@Table注解指定表名"); | |
73 | - } | |
74 | - this.tableName = annoTable.name(); | |
75 | - String schema = annoTable.schema(); | |
76 | - String catalog = annoTable.catalog(); | |
77 | - if(schema != null && !"".equals(schema)){ | |
78 | - this.dbName = schema; | |
79 | - }else if(catalog != null && !"".equals(catalog)){ | |
80 | - this.dbName = catalog; | |
81 | - } | |
82 | - | |
83 | - //初始化数据 | |
84 | - beanToTableField = new HashMap<String, String>(); | |
85 | - tableToBeanField = new HashMap<String, String>(); | |
86 | - tableFieldNameListGapWithComma = ""; | |
87 | - Field[] declaredFields = tClassInfo.getDeclaredFields(); | |
88 | - for(int i=0; i<declaredFields.length; ++i){ | |
89 | - Field currField = declaredFields[i]; | |
90 | - String fieldName = currField.getName(); | |
91 | - Id annoId = (Id)currField.getAnnotation(Id.class); | |
92 | - Column annoColumn = (Column)currField.getAnnotation(Column.class); | |
93 | - if(annoId != null){ | |
94 | - if(annoColumn == null){ | |
95 | - idTableFieldName = this.camelToUnderline(fieldName); | |
96 | - }else{ | |
97 | - idTableFieldName = annoColumn.name(); | |
98 | - } | |
99 | - idBeanFieldName = fieldName; | |
100 | - tableFieldNameListGapWithComma += idTableFieldName+","; | |
101 | - beanToTableField.put(fieldName, idTableFieldName); | |
102 | - tableToBeanField.put(idTableFieldName, fieldName); | |
103 | - }else{ | |
104 | - if(annoColumn != null){ | |
105 | - String tableFieldName = annoColumn.name(); | |
106 | - tableFieldNameListGapWithComma += tableFieldName+","; | |
107 | - beanToTableField.put(fieldName, tableFieldName); | |
108 | - tableToBeanField.put(tableFieldName, fieldName); | |
109 | - } | |
110 | - } | |
111 | - } | |
112 | - | |
113 | - //检验是否有属性 | |
114 | - if(beanToTableField.isEmpty()){ | |
115 | - throw new Exception("DAO层初始化失败,失败原因:"+this.tClassInfo.getName()+"实体类,没有在实体中找到属性信息"); | |
116 | - } | |
117 | - | |
118 | - //去除逗号 | |
119 | - tableFieldNameListGapWithComma = tableFieldNameListGapWithComma.substring(0, tableFieldNameListGapWithComma.length()-1); | |
120 | - | |
121 | - //创建rowmapper | |
122 | - this.customJdbcTemplateRowMapper = new CustomJdbcTemplateRowMapper(this.tClassInfo, this.tableToBeanField); | |
123 | - } | |
124 | - | |
125 | - /** | |
126 | - * 将驼峰式命名的字符串转换为下划线大写方式。如果转换前的驼峰式命名的字符串为空,则返回空字符串。</br> | |
127 | - * 例如:HelloWorld->HELLO_WORLD | |
128 | - * @param name 转换前的驼峰式命名的字符串 | |
129 | - * @return 转换后下划线大写方式命名的字符串 | |
130 | - */ | |
131 | - private String camelToUnderline(String name) { | |
132 | - StringBuilder result = new StringBuilder(); | |
133 | - if (name != null && name.length() > 0) { | |
134 | - // 将第一个字符处理成大写 | |
135 | - result.append(name.substring(0, 1).toUpperCase()); | |
136 | - // 循环处理其余字符 | |
137 | - for (int i = 1; i < name.length(); i++) { | |
138 | - String s = name.substring(i, i + 1); | |
139 | - // 在大写字母前添加下划线 | |
140 | - if (Character.isUpperCase(s.charAt(0)) && Character.isLetter(s.charAt(0))) { | |
141 | - result.append("_"); | |
142 | - } | |
143 | - // 其他字符直接转成大写 | |
144 | - result.append(s.toUpperCase()); | |
145 | - } | |
146 | - } | |
147 | - return result.toString(); | |
148 | - } | |
149 | - | |
150 | - /** | |
151 | - * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br> | |
152 | - * 例如:HELLO_WORLD->HelloWorld | |
153 | - * @param name 转换前的下划线大写方式命名的字符串 | |
154 | - * @return 转换后的驼峰式命名的字符串 | |
155 | - */ | |
156 | - private String underlineToCamel(String name) { | |
157 | - StringBuilder result = new StringBuilder(); | |
158 | - // 快速检查 | |
159 | - if (name == null || name.isEmpty()) { | |
160 | - // 没必要转换 | |
161 | - return ""; | |
162 | - } else if (!name.contains("_")) { | |
163 | - // 不含下划线,仅将首字母小写 | |
164 | - return name.substring(0, 1).toLowerCase() + name.substring(1); | |
165 | - } | |
166 | - // 用下划线将原始字符串分割 | |
167 | - String camels[] = name.split("_"); | |
168 | - for (String camel : camels) { | |
169 | - // 跳过原始字符串中开头、结尾的下换线或双重下划线 | |
170 | - if (camel.isEmpty()) { | |
171 | - continue; | |
172 | - } | |
173 | - // 处理真正的驼峰片段 | |
174 | - if (result.length() == 0) { | |
175 | - // 第一个驼峰片段,全部字母都小写 | |
176 | - result.append(camel.toLowerCase()); | |
177 | - } else { | |
178 | - // 其他的驼峰片段,首字母大写 | |
179 | - result.append(camel.substring(0, 1).toUpperCase()); | |
180 | - result.append(camel.substring(1).toLowerCase()); | |
181 | - } | |
182 | - } | |
183 | - return result.toString(); | |
184 | - } | |
185 | - | |
186 | - private void appendWhereCondition(StringBuffer sql, StringBuffer pql, List<Object> list, List<Object[]> condition) { | |
187 | - if (condition == null || condition.size() == 0) return; | |
188 | - Object[] con = condition.get(0); | |
189 | - int iLen = condition.size(); | |
190 | - sql.append(" WHERE "); | |
191 | - pql.append(" WHERE "); | |
192 | - sql.append(con[0]); | |
193 | - pql.append(con[0]); | |
194 | - if (null != con[1] && con[1] != "" && con[1] != "useArg[0]") { | |
195 | - sql.append(" " + con[1] + " ?"); | |
196 | - pql.append(" " + con[1] + " " + con[2]); | |
197 | - list.add(con[2]); | |
198 | - } | |
199 | - for (int i = 1; i < iLen; i++) { | |
200 | - con = condition.get(i); | |
201 | - sql.append(" AND "); | |
202 | - pql.append(" AND "); | |
203 | - sql.append(con[0]); | |
204 | - pql.append(con[0]); | |
205 | - if (null == con[1] || "" == con[1] || con[1] == "useArg[0]") continue; | |
206 | - sql.append(" " + con[1] + " ?"); | |
207 | - pql.append(" " + con[1] + " " + con[2]); | |
208 | - list.add(con[2]); | |
209 | - } | |
210 | - } | |
211 | - | |
212 | - private void appendWhereConditionForCount(StringBuffer sql, List<Object[]> condition) { | |
213 | - if (condition == null || condition.size() == 0) return; | |
214 | - Object[] con = condition.get(0); | |
215 | - int iLen = condition.size(); | |
216 | - sql.append(" WHERE "); | |
217 | - sql.append(con[0]); | |
218 | - if (null != con[1] && con[1] != "" && con[1] != "useArg[0]") { | |
219 | - sql.append(" " + con[1] + " ?"); | |
220 | - } | |
221 | - for (int i = 1; i < iLen; i++) { | |
222 | - con = condition.get(i); | |
223 | - sql.append(" AND "); | |
224 | - sql.append(con[0]); | |
225 | - if (null == con[1] || "" == con[1] || con[1] == "useArg[0]") continue; | |
226 | - sql.append(" " + con[1] + " ?"); | |
227 | - } | |
228 | - } | |
229 | - | |
230 | - private void appendSetSql(StringBuffer sql, StringBuffer pql, List<Object> list, List<Object[]> obj) { | |
231 | - for (Object[] arg : obj) { | |
232 | - if (arg.length > 2) { | |
233 | - sql.append(" " + arg[0] + " = " + arg[0] + arg[2] + " ?,"); | |
234 | - pql.append(" " + arg[0] + " = " + arg[0] + arg[2] + arg[1] +","); | |
235 | - list.add(arg[1]); | |
236 | - }else if(arg.length == 2) { | |
237 | - sql.append(" " + arg[0] + " = ?,"); | |
238 | - pql.append(" " + arg[0] + " = " + arg[1] + ","); | |
239 | - list.add(arg[1]); | |
240 | - }else if(arg.length == 1) { | |
241 | - sql.append(" " + arg[0] + ","); | |
242 | - pql.append(" " + arg[0] + ","); | |
243 | - } | |
244 | - } | |
245 | - } | |
246 | - | |
247 | - private String getTableSql(){ | |
248 | - return (this.dbName == null || "".equals(this.dbName.trim()))? | |
249 | - ("`"+this.tableName+"`"): | |
250 | - ("`"+this.dbName+"`.`"+this.tableName+"`"); | |
251 | - } | |
252 | - | |
253 | - /** | |
254 | - * 按主键查询 | |
255 | - */ | |
256 | - public T findEntityByID(ID id) { | |
257 | - return findEntityByID(id, true, false); | |
258 | - } | |
259 | - | |
260 | - /** | |
261 | - * 按主键查询 | |
262 | - * isLock 是否锁定, 默认不锁 | |
263 | - * fromWriteDB 是否从写库读写,默认从读库查询 | |
264 | - */ | |
265 | - public T findEntityByID(ID id, boolean fromWriteDB, boolean isLock) { | |
266 | - StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); | |
267 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
268 | - sql.append(" WHERE "+idTableFieldName+" = ?"); | |
269 | - pql.append(" WHERE "+idTableFieldName+" = " + id); | |
270 | - if (isLock) { | |
271 | - sql.append(" FOR UPDATE"); | |
272 | - pql.append(" FOR UPDATE"); | |
273 | - } | |
274 | - return (T) (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForObject(sql.toString(), this.customJdbcTemplateRowMapper, id); | |
275 | - } | |
276 | - | |
277 | - /** | |
278 | - * 根据条件List<Object[]>查询 | |
279 | - * Object[]数组长度是3 | |
280 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
281 | - */ | |
282 | - public T findEntityByCondition(List<Object[]> condition) throws Exception { | |
283 | - List<T> tempList = findListByCondition(condition, null, false); | |
284 | - if(tempList == null || tempList.size() == 0){ | |
285 | - return null; | |
286 | - } | |
287 | - if(tempList != null && tempList.size() == 1){ | |
288 | - return tempList.get(0); | |
289 | - }else{ | |
290 | - throw new Exception("found multi rows with condition,but this func expected one row"); | |
291 | - } | |
292 | - } | |
293 | - | |
294 | - /** | |
295 | - * 根据条件sql查询 | |
296 | - * sqlCondition 为where 后面的条件。 | |
297 | - */ | |
298 | - public T findEntityBySql(String sqlCondition) throws Exception{ | |
299 | - List<T> tempList = findListBySql(sqlCondition, false); | |
300 | - if(tempList == null || tempList.size() == 0){ | |
301 | - return null; | |
302 | - } | |
303 | - if(tempList != null && tempList.size() == 1){ | |
304 | - return tempList.get(0); | |
305 | - }else{ | |
306 | - throw new Exception("found multi rows with condition,but this func expected one row"); | |
307 | - } | |
308 | - } | |
309 | - | |
310 | - /** | |
311 | - * 根据条件List<Object[]>查询 | |
312 | - * Object[]数组长度是3 | |
313 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
314 | - */ | |
315 | - public List<T> findListByCondition(List<Object[]> condition) { | |
316 | - return findListByCondition(condition,null, false); | |
317 | - } | |
318 | - | |
319 | - /** | |
320 | - * 根据条件List<Object[]>查询 | |
321 | - * Object[]数组长度是3 | |
322 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
323 | - */ | |
324 | - public List<T> findListByCondition(List<Object[]> condition, String sortCondition, boolean fromWriteDB) { | |
325 | - StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); | |
326 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
327 | - List<Object> list = new ArrayList<Object>(); | |
328 | - this.appendWhereCondition(sql, pql, list, condition); | |
329 | - if(sortCondition != null && !sortCondition.equals("")){ | |
330 | - sql.append(" " + sortCondition + " "); | |
331 | - pql.append(" " + sortCondition + " "); | |
332 | - } | |
333 | - return (List<T>)(fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), this.customJdbcTemplateRowMapper, list.toArray()); | |
334 | - } | |
335 | - | |
336 | - /** | |
337 | - * 根据条件sql查询 | |
338 | - * sqlCondition 为where 后面的条件。 | |
339 | - */ | |
340 | - public List<T> findListBySql(String sqlCondition) { | |
341 | - return findListBySql(sqlCondition, false); | |
342 | - } | |
343 | - | |
344 | - /** | |
345 | - * 根据条件sql查询 | |
346 | - * sqlCondition 为where 后面的条件。 | |
347 | - */ | |
348 | - public List<T> findListBySql(String sqlCondition, boolean fromWriteDB) { | |
349 | - StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
350 | - return (List<T>)(fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), this.customJdbcTemplateRowMapper); | |
351 | - } | |
352 | - | |
353 | - /** | |
354 | - * 按条件分页查询 | |
355 | - * Object[]数组长度是3 | |
356 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
357 | - */ | |
358 | - public Map<String, Object> findPageByCondition(List<Object[]> condition,int page, int pageSize) { | |
359 | - return findPageByCondition(condition, null , page, pageSize, false); | |
360 | - } | |
361 | - | |
362 | - /** | |
363 | - * 按条件分页查询 | |
364 | - * Object[]数组长度是3 | |
365 | - * Object[]第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
366 | - * boolean isUseCache, 是否用缓存,默认用。 | |
367 | - * boolean isAddCache, 是否添加缓存,默认添加。 | |
368 | - */ | |
369 | - public Map<String, Object> findPageByCondition(List<Object[]> condition,String sortCondition, int page, int pageSize, boolean fromWriteDB) { | |
370 | - StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); | |
371 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
372 | - StringBuffer sqlCount = new StringBuffer("SELECT COUNT(1) rowCount FROM "+this.getTableSql()); | |
373 | - | |
374 | - List<Object> count_list = new ArrayList<Object>(); | |
375 | - List<Object> page_list = new ArrayList<Object>(); | |
376 | - this.appendWhereConditionForCount(sqlCount, condition); | |
377 | - this.appendWhereCondition(sql, pql, count_list, condition); | |
378 | - for (int i = 0; i < count_list.size(); i++) | |
379 | - page_list.add(count_list.get(i)); | |
380 | - | |
381 | - page_list.add((page - 1) * pageSize); | |
382 | - page_list.add(pageSize); | |
383 | - | |
384 | - if(sortCondition != null && !sortCondition.equals("")){ | |
385 | - sql.append(" " + sortCondition + " "); | |
386 | - pql.append(" " + sortCondition + " "); | |
387 | - } | |
388 | - | |
389 | - String pageSql = sql.toString() + " limit ?, ?"; | |
390 | - | |
391 | - Map<String, Object> totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString(), count_list.toArray()) ; | |
392 | - List<T> resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), this.customJdbcTemplateRowMapper, page_list.toArray()); | |
393 | - return UtilsSql.createPage(page, pageSize, Integer.valueOf(totalRowsMap.get("rowCount").toString()), resultList); | |
394 | - } | |
395 | - | |
396 | - /** | |
397 | - * 按sql分页查询, sqlCondition为where 后条件sql | |
398 | - */ | |
399 | - public Map<String, Object> findPageBySql(String sqlCondition, int page, int pageSize) { | |
400 | - return findPageBySql(sqlCondition, page, pageSize, false); | |
401 | - } | |
402 | - | |
403 | - /** | |
404 | - * 按sql分页查询, sqlCondition为where 后条件sql | |
405 | - */ | |
406 | - public Map<String, Object> findPageBySql(String sqlCondition, int page, int pageSize,boolean fromWriteDB) { | |
407 | - StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition ); | |
408 | - StringBuffer sqlCount = new StringBuffer("SELECT count(1) rowCount FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
409 | - String pageSql = sql.toString() + " limit ?, ?"; | |
410 | - String pagePql = sql.toString() + " limit " + ((page -1) * pageSize) + ", " + (page * pageSize); | |
411 | - List<Object> page_list = new ArrayList<Object>(); | |
412 | - page_list.add((page - 1) * pageSize); | |
413 | - page_list.add(page * pageSize); | |
414 | - | |
415 | - Map<String, Object> totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString()); | |
416 | - List<T> resultList =(fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), this.customJdbcTemplateRowMapper, page_list.toArray()); | |
417 | - return UtilsSql.createPage(page, pageSize, Integer.valueOf(totalRowsMap.get("rowCount").toString()), resultList); | |
418 | - } | |
419 | - | |
420 | - /** | |
421 | - * 添加 | |
422 | - */ | |
423 | - public Number addEntityForAutoincrementId(T entity) { | |
424 | - StringBuffer sqlForLog = new StringBuffer("INSERT INTO "+this.getTableSql()+"("); | |
425 | - StringBuffer sqlValueForLog = new StringBuffer(") VALUES ("); | |
426 | - StringBuffer sqlInsertPart = new StringBuffer("INSERT INTO "+this.getTableSql()+"("); | |
427 | - StringBuffer sqlColumnPart = new StringBuffer(") VALUES ("); | |
428 | - List<Object> paramList = new ArrayList<Object>(); | |
429 | - | |
430 | - Iterator<String> beanFieldIter = this.beanToTableField.keySet().iterator(); | |
431 | - while(beanFieldIter.hasNext()){ | |
432 | - String beanFieldName = beanFieldIter.next(); | |
433 | - String tableFieldName = this.beanToTableField.get(beanFieldName); | |
434 | - Field beanField; | |
435 | - Object beanFieldValue = null; | |
436 | - try { | |
437 | - beanField = this.tClassInfo.getDeclaredField(beanFieldName); | |
438 | - beanField.setAccessible(true); | |
439 | - beanFieldValue = beanField.get(entity); | |
440 | - } catch (Exception e) { | |
441 | - e.printStackTrace(); | |
442 | - } | |
443 | - | |
444 | - if(tableFieldName == null || beanFieldName == null || beanFieldValue == null){ | |
445 | - continue; | |
446 | - } | |
447 | - | |
448 | - sqlForLog.append("`"+tableFieldName+"`,"); | |
449 | - sqlValueForLog.append(beanFieldValue.toString()+","); | |
450 | - sqlInsertPart.append("`"+tableFieldName+"`,"); | |
451 | - sqlColumnPart.append(" ?,"); | |
452 | - paramList.add(beanFieldValue); | |
453 | - } | |
454 | - //打印日志内容 | |
455 | - sqlForLog.substring(0, sqlForLog.length()-1); | |
456 | - sqlForLog.append(") VALUES ("); | |
457 | - sqlForLog.append(sqlValueForLog+")"); | |
458 | - //UtilsLog.infoForMessage(sqlForLog.toString(), this.getClass()); | |
459 | - | |
460 | - //执行SQL | |
461 | - String exeSql = sqlInsertPart.substring(0, sqlInsertPart.length()-1)+sqlColumnPart.substring(0, sqlColumnPart.length()-1)+")"; | |
462 | - KeyHolder keyHolder = new GeneratedKeyHolder(new ArrayList<Map<String,Object>>(1)); | |
463 | - jdbcTemplateWrite.update(new PreparedStatementCreator() { | |
464 | - @Override | |
465 | - public PreparedStatement createPreparedStatement(Connection con) throws SQLException { | |
466 | - PreparedStatement stat = con.prepareStatement(exeSql, new String[] {idTableFieldName}); | |
467 | - PreparedStatementSetter setter = new ArgumentPreparedStatementSetter(paramList.toArray()); | |
468 | - setter.setValues(stat); | |
469 | - return stat; | |
470 | - } | |
471 | - | |
472 | - }, keyHolder); | |
473 | - | |
474 | - return keyHolder.getKey(); | |
475 | - } | |
476 | - | |
477 | - /** | |
478 | - * 批量添加 | |
479 | - * @throws Exception | |
480 | - */ | |
481 | - public List<Number> addEntityList(List<T> entityList) throws Exception { | |
482 | - if(entityList == null || entityList.isEmpty()) { | |
483 | - throw new Exception("entitylist is empty or null"); | |
484 | - } | |
485 | - //构造SQL语句及Entity Field列表 | |
486 | - List<Field> beanFieldList = new ArrayList<Field>(this.beanToTableField.size()); | |
487 | - StringBuffer exeSql = new StringBuffer(this.constructUpdateSql(entityList.get(0), beanFieldList)); | |
488 | - | |
489 | - //构造参数信息 | |
490 | - List<Object> args = new ArrayList<Object>(); | |
491 | - exeSql.append(" VALUES"); | |
492 | - for(int itemIndex=0; itemIndex<entityList.size(); ++itemIndex) { | |
493 | - T item = entityList.get(itemIndex); | |
494 | - exeSql.append("("); | |
495 | - for(int i=0; i<beanFieldList.size(); ++i) { | |
496 | - Field itemField = beanFieldList.get(i); | |
497 | - Object itemData = itemField.get(item); | |
498 | - if(itemData == null) { | |
499 | - args.add(this.getDefaultValueByFieldType(itemField)); | |
500 | - }else { | |
501 | - args.add(itemData); | |
502 | - } | |
503 | - exeSql.append("?,"); | |
504 | - } | |
505 | - exeSql.setCharAt(exeSql.length()-1, ' '); | |
506 | - exeSql.append("),"); | |
507 | - } | |
508 | - exeSql.setCharAt(exeSql.length()-1, ';'); | |
509 | - | |
510 | - //调用更新接口 | |
511 | - KeyHolder keyHolder = new GeneratedKeyHolder(new ArrayList<Map<String,Object>>(entityList.size())); | |
512 | - jdbcTemplateWrite.update(new PreparedStatementCreator() { | |
513 | - @Override | |
514 | - public PreparedStatement createPreparedStatement(Connection con) throws SQLException { | |
515 | - PreparedStatement stat = con.prepareStatement(exeSql.toString(), new String[] {idTableFieldName}); | |
516 | - PreparedStatementSetter setter = new ArgumentPreparedStatementSetter(args.toArray()); | |
517 | - setter.setValues(stat); | |
518 | - return stat; | |
519 | - } | |
520 | - | |
521 | - }, keyHolder); | |
522 | - | |
523 | - //处理结果数据 | |
524 | - List<Map<String, Object>> data = keyHolder.getKeyList(); | |
525 | - if(data.size() != entityList.size()) { | |
526 | - throw new Exception("param entity size not equal return generate key list size"); | |
527 | - } | |
528 | - List<Number> dataT = new ArrayList<Number>(data.size()); | |
529 | - for(Map<String, Object> item: data) { | |
530 | - Iterator<Object> keyIter = item.values().iterator(); | |
531 | - if (keyIter.hasNext()) { | |
532 | - Object key = keyIter.next(); | |
533 | - if (!(key instanceof Number)) { | |
534 | - throw new DataRetrievalFailureException( | |
535 | - "The generated key is not of a supported numeric type. " + | |
536 | - "Unable to cast [" + (key != null ? key.getClass().getName() : null) + | |
537 | - "] to [" + Number.class.getName() + "]"); | |
538 | - } | |
539 | - dataT.add((Number)key); | |
540 | - }else { | |
541 | - throw new DataRetrievalFailureException("Unable to retrieve the generated key. " + | |
542 | - "Check that the table has an identity column enabled."); | |
543 | - } | |
544 | - } | |
545 | - return dataT; | |
546 | - } | |
547 | - | |
548 | - private String constructUpdateSql(T entity, List<Field> beanFieldList) { | |
549 | - StringBuffer sqlInsertPart = new StringBuffer("INSERT INTO "+this.getTableSql()+"("); | |
550 | - Iterator<String> beanFieldIter = this.beanToTableField.keySet().iterator(); | |
551 | - while(beanFieldIter.hasNext()){ | |
552 | - String beanFieldName = beanFieldIter.next(); | |
553 | - String tableFieldName = this.beanToTableField.get(beanFieldName); | |
554 | - Field beanField = null; | |
555 | - try { | |
556 | - beanField = this.tClassInfo.getDeclaredField(beanFieldName); | |
557 | - beanField.setAccessible(true); | |
558 | - if(beanField.get(entity) == null) { | |
559 | - continue; | |
560 | - } | |
561 | - } catch (Exception e) { | |
562 | - continue; | |
563 | - } | |
564 | - | |
565 | - if(tableFieldName == null || beanFieldName == null){ | |
566 | - continue; | |
567 | - } | |
568 | - | |
569 | - beanFieldList.add(beanField); | |
570 | - sqlInsertPart.append("`"+tableFieldName+"`,"); | |
571 | - } | |
572 | - return sqlInsertPart.substring(0, sqlInsertPart.length()-1)+")"; | |
573 | - } | |
574 | - | |
575 | - private Object getDefaultValueByFieldType(Field itemField) { | |
576 | - String simpleName = itemField.getType().getSimpleName(); | |
577 | - if("String".equals(simpleName)) { | |
578 | - return ""; | |
579 | - }else if("Date".equals(simpleName) || "Timestamp".equals(simpleName)) { | |
580 | - return "2000-01-01 00:00:00"; | |
581 | - }else if("BigDecimal".equals(simpleName)){ | |
582 | - return BigDecimal.ZERO; | |
583 | - }else { | |
584 | - return 0; | |
585 | - } | |
586 | - } | |
587 | - | |
588 | - /** | |
589 | - * 按ID删除 | |
590 | - */ | |
591 | - public int deleteEntityByID(ID id) { | |
592 | - StringBuffer sql = new StringBuffer("DELETE FROM "+this.getTableSql()+" WHERE"); | |
593 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
594 | - pql.append(" "+this.idTableFieldName+" = " + id); | |
595 | - sql.append(" "+this.idTableFieldName+" = ?"); | |
596 | - return jdbcTemplateWrite.update(sql.toString(), id); | |
597 | - } | |
598 | - /** | |
599 | - * 删除按List<Object[]>条件 | |
600 | - * Object[]数组长度是3 | |
601 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
602 | - */ | |
603 | - public int deleteEntityByCondition(List<Object[]> condition) throws Exception{ | |
604 | - if (null == condition || condition.size() == 0) { | |
605 | - throw new Exception("params[condition] is empty"); | |
606 | - } | |
607 | - | |
608 | - List<Object> list = new ArrayList<Object>(); | |
609 | - StringBuffer sql = new StringBuffer("DELETE FROM "+this.getTableSql()+""); | |
610 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
611 | - this.appendWhereCondition(sql, pql, list, condition); | |
612 | - return jdbcTemplateWrite.update( sql.toString(), list.toArray()); | |
613 | - } | |
614 | - | |
615 | - /** | |
616 | - * 删除按condition条件 | |
617 | - * 建议使用deleteTByCondition(List<Object[]> condition), 如果removeTByCondition(List<Object[]> condition)满足不了where条件可以使用此方法。 | |
618 | - * condition为where后面的条件,condition不能为空。 | |
619 | - */ | |
620 | - public int deleteEntityBySql(String sqlCondition) throws Exception{ | |
621 | - if("".equals(sqlCondition)) { | |
622 | - throw new Exception("params[sqlCondition] is empty"); | |
623 | - } | |
624 | - return jdbcTemplateWrite.update( "DELETE FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
625 | - } | |
626 | - | |
627 | - /** | |
628 | - * 根据list对象逐个删除。 | |
629 | - */ | |
630 | - public List<Integer> deleteEntityList(List<T> entityList) { | |
631 | - List<Integer> result = new ArrayList<Integer>(); | |
632 | - for (T entity : entityList) { | |
633 | - Field beanField; | |
634 | - Object beanFieldValue = Integer.valueOf(0); | |
635 | - try { | |
636 | - beanField = this.tClassInfo.getDeclaredField(this.idBeanFieldName); | |
637 | - beanField.setAccessible(true); | |
638 | - beanFieldValue = beanField.get(entity); | |
639 | - } catch (Exception e) { | |
640 | - e.printStackTrace(); | |
641 | - } | |
642 | - result.add(deleteEntityByID((ID)beanFieldValue)); | |
643 | - } | |
644 | - return result; | |
645 | - } | |
646 | - | |
647 | - /** | |
648 | - * 根据ID修改指定的值 | |
649 | - */ | |
650 | - public int updateEntityById(List<Object[]> changeList, ID id) throws Exception{ | |
651 | - if(null == id){ | |
652 | - throw new Exception("params[id] is null"); | |
653 | - } | |
654 | - if (null == changeList || changeList.size() == 0) { | |
655 | - throw new Exception("params[changeList] is empty"); | |
656 | - } | |
657 | - | |
658 | - StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); | |
659 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
660 | - List<Object> list = new ArrayList<Object>(); | |
661 | - this.appendSetSql(sql, pql, list, changeList); | |
662 | - | |
663 | - String where = " WHERE "+this.idTableFieldName+"=?"; | |
664 | - String updateSql = sql.substring(0, sql.length()-1)+where; | |
665 | - list.add(id); | |
666 | - return jdbcTemplateWrite.update(updateSql, list.toArray()); | |
667 | - } | |
668 | - | |
669 | - /** | |
670 | - * List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 | |
671 | - * List<Object[]> condition 修改的条件, 数组长度是3, 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
672 | - */ | |
673 | - public int updateEntityByCondition(List<Object[]> updateObj, List<Object[]> condition) throws Exception{ | |
674 | - if (null == updateObj || updateObj.size() == 0) { | |
675 | - throw new Exception("params[updateObj] is empty"); | |
676 | - } | |
677 | - if (null == condition || condition.size() == 0) { | |
678 | - throw new Exception("params[condition] is empty"); | |
679 | - } | |
680 | - | |
681 | - StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); | |
682 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
683 | - List<Object> list = new ArrayList<Object>(); | |
684 | - this.appendSetSql(sql, pql, list, updateObj); | |
685 | - | |
686 | - StringBuffer where = new StringBuffer(""); | |
687 | - StringBuffer pwhere = new StringBuffer(""); | |
688 | - this.appendWhereCondition(where, pwhere, list, condition); | |
689 | - | |
690 | - String updateSql = sql.substring(0, sql.length()-1)+where.toString(); | |
691 | - return jdbcTemplateWrite.update(updateSql, list.toArray()); | |
692 | - } | |
693 | - | |
694 | - /** | |
695 | - * List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 | |
696 | - * String sqlCondition 修改的条件。 | |
697 | - */ | |
698 | - public int updateEntityBySql(List<Object[]> updateObj, String sqlCondition) throws Exception{ | |
699 | - if (null == updateObj || updateObj.size() == 0) { | |
700 | - throw new Exception("params[updateObj] is empty"); | |
701 | - } | |
702 | - if ("".equals(sqlCondition)) { | |
703 | - throw new Exception("params[sqlCondition] is empty"); | |
704 | - } | |
705 | - | |
706 | - StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); | |
707 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
708 | - List<Object> list = new ArrayList<Object>(); | |
709 | - this.appendSetSql(sql, pql, list, updateObj); | |
710 | - | |
711 | - String updateSql = sql.toString().substring(0, sql.length()-1) + " WHERE "+sqlCondition; | |
712 | - return jdbcTemplateWrite.update(updateSql, list.toArray()); | |
713 | - } | |
714 | - | |
715 | - public Map<String, Object> getPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize){ | |
716 | - try { | |
717 | - String[] splitedSql = UtilsSql.splitCoreSql(coreSql); | |
718 | - return this.getPageData(splitedSql[0], splitedSql[1], orderByPartSql, page, pageSize); | |
719 | - }catch (Exception e) { | |
720 | - return UtilsSql.createPage(page, pageSize, 0, new ArrayList<Object>()); | |
721 | - } | |
722 | - } | |
723 | - | |
724 | - public Map<String, Object> getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize){ | |
725 | - //构造查询语句 | |
726 | - String querySql = selectSql+" "+fromAndWhereSql+" "+orderByPartSql+" "+UtilsSql.getLimitCondition(page, pageSize); | |
727 | - | |
728 | - //构造统计计数语句 | |
729 | - String countSql = "select count(*) rowsCount from ( select 1 "+fromAndWhereSql+" ) t "; | |
730 | - | |
731 | - //执行查询 | |
732 | - List<Map<String, Object>> queryData = new ArrayList<Map<String, Object>>(); | |
733 | - Map<String, Object> countData = new HashMap<String, Object>(); | |
734 | - queryData = this.jdbcTemplateRead.queryForList(querySql); | |
735 | - countData = this.jdbcTemplateRead.queryForMap(countSql); | |
736 | - return UtilsSql.createPage(page, pageSize, Integer.valueOf(countData.get("rowsCount").toString()), queryData); | |
737 | - } | |
738 | - | |
739 | - public <E> Map<String, Object> getBeanPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize, Class<E> beanClass){ | |
740 | - try { | |
741 | - String[] splitedSql = UtilsSql.splitCoreSql(coreSql); | |
742 | - return this.getPageData(splitedSql[0], splitedSql[1], orderByPartSql, page, pageSize, beanClass); | |
743 | - }catch (Exception e) { | |
744 | - return UtilsSql.createPage(page, pageSize, 0, new ArrayList<Object>()); | |
745 | - } | |
746 | - } | |
747 | - | |
748 | - public <E> Map<String, Object> getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize, Class<E> beanClass){ | |
749 | - //构造查询语句 | |
750 | - String querySql = selectSql+" "+fromAndWhereSql+" "+orderByPartSql+" "+UtilsSql.getLimitCondition(page, pageSize); | |
751 | - | |
752 | - //构造统计计数语句 | |
753 | - String countSql = "select count(*) rowsCount from ( select 1 "+fromAndWhereSql+" ) t "; | |
754 | - | |
755 | - //执行查询 | |
756 | - List<E> queryData = new ArrayList<E>(); | |
757 | - Map<String, Object> countData = new HashMap<String, Object>(); | |
758 | - queryData = this.jdbcTemplateRead.queryForList(querySql, beanClass); | |
759 | - countData = this.jdbcTemplateRead.queryForMap(countSql); | |
760 | - return UtilsSql.createPage(page, pageSize, Integer.valueOf(countData.get("rowsCount").toString()), queryData); | |
761 | - } | |
762 | - | |
763 | - /** | |
764 | - * 按主键查询 | |
765 | - */ | |
766 | - public <E> E findBeanByID(ID id, Class<E> beanClass) { | |
767 | - return findBeanByID(id, true, false, beanClass); | |
768 | - } | |
769 | - | |
770 | - /** | |
771 | - * 按主键查询 | |
772 | - * isLock 是否锁定, 默认不锁 | |
773 | - * fromWriteDB 是否从写库读写,默认从读库查询 | |
774 | - */ | |
775 | - public <E> E findBeanByID(ID id, boolean fromWriteDB, boolean isLock, Class<E> beanClass) { | |
776 | - StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); | |
777 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
778 | - sql.append(" WHERE "+idTableFieldName+" = ?"); | |
779 | - pql.append(" WHERE "+idTableFieldName+" = " + id); | |
780 | - if (isLock) { | |
781 | - sql.append(" FOR UPDATE"); | |
782 | - pql.append(" FOR UPDATE"); | |
783 | - } | |
784 | - return (E) (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForObject(sql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField), id); | |
785 | - } | |
786 | - | |
787 | - /** | |
788 | - * 根据条件List<Object[]>查询 | |
789 | - * Object[]数组长度是3 | |
790 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
791 | - */ | |
792 | - public <E> E findBeanByCondition(List<Object[]> condition, Class<E> beanClass) throws Exception { | |
793 | - List<E> tempList = findBeanListByCondition(condition, null, false, beanClass); | |
794 | - if(tempList == null || tempList.size() == 0){ | |
795 | - return null; | |
796 | - } | |
797 | - if(tempList != null && tempList.size() == 1){ | |
798 | - return tempList.get(0); | |
799 | - }else{ | |
800 | - throw new Exception("found multi rows with condition,but this func expected one row"); | |
801 | - } | |
802 | - } | |
803 | - | |
804 | - /** | |
805 | - * 根据条件sql查询 | |
806 | - * sqlCondition 为where 后面的条件。 | |
807 | - */ | |
808 | - public <E> E findBeanBySql(String sqlCondition, Class<E> beanClass) throws Exception{ | |
809 | - List<E> tempList = findBeanListBySql(sqlCondition, false, beanClass); | |
810 | - if(tempList == null || tempList.size() == 0){ | |
811 | - return null; | |
812 | - } | |
813 | - if(tempList != null && tempList.size() == 1){ | |
814 | - return tempList.get(0); | |
815 | - }else{ | |
816 | - throw new Exception("found multi rows with condition,but this func expected one row"); | |
817 | - } | |
818 | - } | |
819 | - | |
820 | - /** | |
821 | - * 根据条件List<Object[]>查询 | |
822 | - * Object[]数组长度是3 | |
823 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
824 | - */ | |
825 | - public <E> List<E> findBeanListByCondition(List<Object[]> condition, Class<E> beanClass) { | |
826 | - return findBeanListByCondition(condition, null, false, beanClass); | |
827 | - } | |
828 | - | |
829 | - /** | |
830 | - * 根据条件List<Object[]>查询 | |
831 | - * Object[]数组长度是3 | |
832 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
833 | - */ | |
834 | - public <E> List<E> findBeanListByCondition(List<Object[]> condition, String sortCondition, boolean fromWriteDB, Class<E> beanClass) { | |
835 | - StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); | |
836 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
837 | - List<Object> list = new ArrayList<Object>(); | |
838 | - this.appendWhereCondition(sql, pql, list, condition); | |
839 | - if(sortCondition != null && !sortCondition.equals("")){ | |
840 | - sql.append(" " + sortCondition + " "); | |
841 | - pql.append(" " + sortCondition + " "); | |
842 | - } | |
843 | - return (List<E>)(fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField), list.toArray()); | |
844 | - } | |
845 | - | |
846 | - /** | |
847 | - * 根据条件sql查询 | |
848 | - * sqlCondition 为where 后面的条件。 | |
849 | - */ | |
850 | - public <E> List<E> findBeanListBySql(String sqlCondition, Class<E> beanClass) { | |
851 | - return findBeanListBySql(sqlCondition, false, beanClass); | |
852 | - } | |
853 | - | |
854 | - /** | |
855 | - * 根据条件sql查询 | |
856 | - * sqlCondition 为where 后面的条件。 | |
857 | - */ | |
858 | - public <E> List<E> findBeanListBySql(String sqlCondition, boolean fromWriteDB, Class<E> beanClass) { | |
859 | - StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
860 | - return (List<E>)(fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField)); | |
861 | - } | |
862 | - | |
863 | - /** | |
864 | - * 按条件分页查询 | |
865 | - * Object[]数组长度是3 | |
866 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
867 | - */ | |
868 | - public <E> Map<String, Object> findBeanPageByCondition(List<Object[]> condition, int page, int pageSize, Class<E> beanClass) { | |
869 | - return findBeanPageByCondition(condition, null , page, pageSize, false, beanClass); | |
870 | - } | |
871 | - | |
872 | - /** | |
873 | - * 按条件分页查询 | |
874 | - * Object[]数组长度是3 | |
875 | - * Object[]第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
876 | - * boolean isUseCache, 是否用缓存,默认用。 | |
877 | - * boolean isAddCache, 是否添加缓存,默认添加。 | |
878 | - */ | |
879 | - public <E> Map<String, Object> findBeanPageByCondition(List<Object[]> condition,String sortCondition, int page, int pageSize, boolean fromWriteDB, Class<E> beanClass) { | |
880 | - StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); | |
881 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
882 | - StringBuffer sqlCount = new StringBuffer("SELECT COUNT(1) rowCount FROM "+this.getTableSql()); | |
883 | - | |
884 | - List<Object> count_list = new ArrayList<Object>(); | |
885 | - List<Object> page_list = new ArrayList<Object>(); | |
886 | - this.appendWhereConditionForCount(sqlCount, condition); | |
887 | - this.appendWhereCondition(sql, pql, count_list, condition); | |
888 | - for (int i = 0; i < count_list.size(); i++) | |
889 | - page_list.add(count_list.get(i)); | |
890 | - | |
891 | - page_list.add((page - 1) * pageSize); | |
892 | - page_list.add(pageSize); | |
893 | - | |
894 | - if(sortCondition != null && !sortCondition.equals("")){ | |
895 | - sql.append(" " + sortCondition + " "); | |
896 | - pql.append(" " + sortCondition + " "); | |
897 | - } | |
898 | - | |
899 | - String pageSql = sql.toString() + " limit ?, ?"; | |
900 | - | |
901 | - Map<String, Object> totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString(), count_list.toArray()) ; | |
902 | - List<E> resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField), page_list.toArray()); | |
903 | - return UtilsSql.createPage(page, pageSize, Integer.valueOf(totalRowsMap.get("rowCount").toString()), resultList); | |
904 | - } | |
905 | - | |
906 | - /** | |
907 | - * 按sql分页查询, sqlCondition为where 后条件sql | |
908 | - */ | |
909 | - public <E> Map<String, Object> findBeanPageBySql(String sqlCondition, int page, int pageSize, Class<E> beanClass) { | |
910 | - return findBeanPageBySql(sqlCondition, page, pageSize, false, beanClass); | |
911 | - } | |
912 | - | |
913 | - /** | |
914 | - * 按sql分页查询, sqlCondition为where 后条件sql | |
915 | - */ | |
916 | - public <E> Map<String, Object> findBeanPageBySql(String sqlCondition, int page, int pageSize,boolean fromWriteDB, Class<E> beanClass) { | |
917 | - StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition ); | |
918 | - StringBuffer sqlCount = new StringBuffer("SELECT count(1) rowCount FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
919 | - String pageSql = sql.toString() + " limit ?, ?"; | |
920 | - String pagePql = sql.toString() + " limit " + ((page -1) * pageSize) + ", " + (page * pageSize); | |
921 | - List<Object> page_list = new ArrayList<Object>(); | |
922 | - page_list.add((page - 1) * pageSize); | |
923 | - page_list.add(page * pageSize); | |
924 | - | |
925 | - Map<String, Object> totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString()); | |
926 | - List<E> resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField), page_list.toArray()); | |
927 | - return UtilsSql.createPage(page, pageSize, Integer.valueOf(totalRowsMap.get("rowCount").toString()), resultList); | |
928 | - } | |
929 | -} |
src/main/java/com/taover/repository/CustomJdbcTemplateBroadcast.java
... | ... | @@ -1,602 +0,0 @@ |
1 | -package com.taover.repository; | |
2 | - | |
3 | -import java.io.Serializable; | |
4 | -import java.lang.reflect.Field; | |
5 | -import java.lang.reflect.ParameterizedType; | |
6 | -import java.math.BigDecimal; | |
7 | -import java.sql.Connection; | |
8 | -import java.sql.PreparedStatement; | |
9 | -import java.sql.SQLException; | |
10 | -import java.util.ArrayList; | |
11 | -import java.util.Collections; | |
12 | -import java.util.HashMap; | |
13 | -import java.util.Iterator; | |
14 | -import java.util.List; | |
15 | -import java.util.Map; | |
16 | - | |
17 | -import javax.annotation.Resource; | |
18 | -import javax.persistence.Column; | |
19 | -import javax.persistence.Id; | |
20 | -import javax.persistence.Table; | |
21 | - | |
22 | -import org.springframework.dao.DataAccessException; | |
23 | -import org.springframework.dao.DataRetrievalFailureException; | |
24 | -import org.springframework.jdbc.core.ArgumentPreparedStatementSetter; | |
25 | -import org.springframework.jdbc.core.PreparedStatementCreator; | |
26 | -import org.springframework.jdbc.core.PreparedStatementSetter; | |
27 | -import org.springframework.jdbc.support.GeneratedKeyHolder; | |
28 | -import org.springframework.jdbc.support.KeyHolder; | |
29 | - | |
30 | -import com.taover.repository.exception.MultiRowException; | |
31 | -import com.taover.repository.exception.NoContainTenantException; | |
32 | -import com.taover.repository.exception.NotFoundException; | |
33 | -import com.taover.repository.exception.ObjectReflectException; | |
34 | -import com.taover.repository.jdbctemplate.JdbcTemplateBroadcast; | |
35 | -import com.taover.repository.mapper.CustomJdbcTemplateRowMapper; | |
36 | -import com.taover.repository.util.UtilsSql; | |
37 | - | |
38 | -/** | |
39 | - * | |
40 | - * @author root | |
41 | - * | |
42 | - * @param <T> | |
43 | - * @param <ID> | |
44 | - */ | |
45 | -public class CustomJdbcTemplateBroadcast<T, ID extends Serializable> implements CustomJdbcTemplateBroadcastInterface<T, ID>{ | |
46 | - @Resource | |
47 | - private JdbcTemplateBroadcast _jdbcTemplateBroadcast; | |
48 | - | |
49 | - private Map<String, String> _beanToTableField; | |
50 | - private Map<String, String> _tableToBeanField; | |
51 | - private String _tableFieldNameListGapWithComma; | |
52 | - private String _idTableFieldName; | |
53 | - private String _dbName; | |
54 | - private String _tableName; | |
55 | - private Class<T> _tClassInfo; | |
56 | - private CustomJdbcTemplateRowMapper<T> _customJdbcTemplateRowMapper; | |
57 | - | |
58 | - public CustomJdbcTemplateRowMapper<T> getCustomJdbcTemplateRowMapper(){ | |
59 | - return this._customJdbcTemplateRowMapper; | |
60 | - } | |
61 | - | |
62 | - @SuppressWarnings("unchecked") | |
63 | - public CustomJdbcTemplateBroadcast() throws Exception{ | |
64 | - //获取泛型类Class | |
65 | - this._tClassInfo = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]; | |
66 | - | |
67 | - //检查实体声明 | |
68 | - Table annoTable = (Table) _tClassInfo.getAnnotation(Table.class); | |
69 | - if(annoTable == null){ | |
70 | - throw new Exception("DAO层初始化失败,失败原因:"+_tClassInfo.getName()+"实体类,没有@Table注解指定表名"); | |
71 | - } | |
72 | - this._tableName = annoTable.name(); | |
73 | - String schema = annoTable.schema(); | |
74 | - String catalog = annoTable.catalog(); | |
75 | - if(schema != null && !"".equals(schema)){ | |
76 | - this._dbName = schema; | |
77 | - }else if(catalog != null && !"".equals(catalog)){ | |
78 | - this._dbName = catalog; | |
79 | - } | |
80 | - | |
81 | - //初始化数据 | |
82 | - _beanToTableField = new HashMap<String, String>(); | |
83 | - _tableToBeanField = new HashMap<String, String>(); | |
84 | - _tableFieldNameListGapWithComma = ""; | |
85 | - Field[] declaredFields = _tClassInfo.getDeclaredFields(); | |
86 | - for(int i=0; i<declaredFields.length; ++i){ | |
87 | - Field currField = declaredFields[i]; | |
88 | - String fieldName = currField.getName(); | |
89 | - Id annoId = (Id)currField.getAnnotation(Id.class); | |
90 | - Column annoColumn = (Column)currField.getAnnotation(Column.class); | |
91 | - if(annoId != null){ | |
92 | - if(annoColumn == null){ | |
93 | - _idTableFieldName = this.camelToUnderline(fieldName); | |
94 | - }else{ | |
95 | - _idTableFieldName = annoColumn.name(); | |
96 | - } | |
97 | - _tableFieldNameListGapWithComma += _idTableFieldName+","; | |
98 | - _beanToTableField.put(fieldName, _idTableFieldName); | |
99 | - _tableToBeanField.put(_idTableFieldName, fieldName); | |
100 | - }else{ | |
101 | - if(annoColumn != null){ | |
102 | - String tableFieldName = annoColumn.name(); | |
103 | - _tableFieldNameListGapWithComma += tableFieldName+","; | |
104 | - _beanToTableField.put(fieldName, tableFieldName); | |
105 | - _tableToBeanField.put(tableFieldName, fieldName); | |
106 | - } | |
107 | - } | |
108 | - } | |
109 | - | |
110 | - //检验是否有属性 | |
111 | - if(_beanToTableField.isEmpty()){ | |
112 | - throw new Exception("DAO层初始化失败,失败原因:"+this._tClassInfo.getName()+"实体类,没有在实体中找到属性信息"); | |
113 | - } | |
114 | - | |
115 | - //去除逗号 | |
116 | - _tableFieldNameListGapWithComma = _tableFieldNameListGapWithComma.substring(0, _tableFieldNameListGapWithComma.length()-1); | |
117 | - | |
118 | - //创建rowmapper | |
119 | - this._customJdbcTemplateRowMapper = new CustomJdbcTemplateRowMapper<T>(this._tClassInfo, this._tableToBeanField); | |
120 | - } | |
121 | - | |
122 | - /** | |
123 | - * 将驼峰式命名的字符串转换为下划线大写方式。如果转换前的驼峰式命名的字符串为空,则返回空字符串。</br> | |
124 | - * 例如:HelloWorld->HELLO_WORLD | |
125 | - * @param name 转换前的驼峰式命名的字符串 | |
126 | - * @return 转换后下划线大写方式命名的字符串 | |
127 | - */ | |
128 | - private String camelToUnderline(String name) { | |
129 | - StringBuilder result = new StringBuilder(); | |
130 | - if (name != null && name.length() > 0) { | |
131 | - // 将第一个字符处理成大写 | |
132 | - result.append(name.substring(0, 1).toUpperCase()); | |
133 | - // 循环处理其余字符 | |
134 | - for (int i = 1; i < name.length(); i++) { | |
135 | - String s = name.substring(i, i + 1); | |
136 | - // 在大写字母前添加下划线 | |
137 | - if (Character.isUpperCase(s.charAt(0)) && Character.isLetter(s.charAt(0))) { | |
138 | - result.append("_"); | |
139 | - } | |
140 | - // 其他字符直接转成大写 | |
141 | - result.append(s.toUpperCase()); | |
142 | - } | |
143 | - } | |
144 | - return result.toString(); | |
145 | - } | |
146 | - | |
147 | - private void appendWhereCondition(StringBuffer sql, StringBuffer pql, List<Object> list, List<Object[]> condition) { | |
148 | - if (condition == null || condition.size() == 0) return; | |
149 | - Object[] con = condition.get(0); | |
150 | - int iLen = condition.size(); | |
151 | - sql.append(" WHERE "); | |
152 | - pql.append(" WHERE "); | |
153 | - sql.append(con[0]); | |
154 | - pql.append(con[0]); | |
155 | - if (null != con[1] && con[1] != "" && con[1] != "useArg[0]") { | |
156 | - sql.append(" " + con[1] + " ?"); | |
157 | - pql.append(" " + con[1] + " " + con[2]); | |
158 | - list.add(con[2]); | |
159 | - } | |
160 | - for (int i = 1; i < iLen; i++) { | |
161 | - con = condition.get(i); | |
162 | - sql.append(" AND "); | |
163 | - pql.append(" AND "); | |
164 | - sql.append(con[0]); | |
165 | - pql.append(con[0]); | |
166 | - if (null == con[1] || "" == con[1] || con[1] == "useArg[0]") continue; | |
167 | - sql.append(" " + con[1] + " ?"); | |
168 | - pql.append(" " + con[1] + " " + con[2]); | |
169 | - list.add(con[2]); | |
170 | - } | |
171 | - } | |
172 | - | |
173 | - private void appendWhereConditionForCount(StringBuffer sql, List<Object[]> condition) { | |
174 | - if (condition == null || condition.size() == 0) return; | |
175 | - Object[] con = condition.get(0); | |
176 | - int iLen = condition.size(); | |
177 | - sql.append(" WHERE "); | |
178 | - sql.append(con[0]); | |
179 | - if (null != con[1] && con[1] != "" && con[1] != "useArg[0]") { | |
180 | - sql.append(" " + con[1] + " ?"); | |
181 | - } | |
182 | - for (int i = 1; i < iLen; i++) { | |
183 | - con = condition.get(i); | |
184 | - sql.append(" AND "); | |
185 | - sql.append(con[0]); | |
186 | - if (null == con[1] || "" == con[1] || con[1] == "useArg[0]") continue; | |
187 | - sql.append(" " + con[1] + " ?"); | |
188 | - } | |
189 | - } | |
190 | - | |
191 | - private void appendSetSql(StringBuffer sql, StringBuffer pql, List<Object> list, List<Object[]> obj) { | |
192 | - for (Object[] arg : obj) { | |
193 | - if (arg.length > 2) { | |
194 | - sql.append(" " + arg[0] + " = " + arg[0] + arg[2] + " ?,"); | |
195 | - pql.append(" " + arg[0] + " = " + arg[0] + arg[2] + arg[1] +","); | |
196 | - list.add(arg[1]); | |
197 | - }else if(arg.length == 2) { | |
198 | - sql.append(" " + arg[0] + " = ?,"); | |
199 | - pql.append(" " + arg[0] + " = " + arg[1] + ","); | |
200 | - list.add(arg[1]); | |
201 | - }else if(arg.length == 1) { | |
202 | - sql.append(" " + arg[0] + ","); | |
203 | - pql.append(" " + arg[0] + ","); | |
204 | - } | |
205 | - } | |
206 | - } | |
207 | - | |
208 | - private String getTableSql(){ | |
209 | - return (this._dbName == null || "".equals(this._dbName.trim()))? | |
210 | - ("`"+this._tableName+"`"): | |
211 | - ("`"+this._dbName+"`.`"+this._tableName+"`"); | |
212 | - } | |
213 | - | |
214 | - private String constructUpdateSql(T entity, List<Field> beanFieldList) { | |
215 | - StringBuffer sqlInsertPart = new StringBuffer("INSERT INTO "+this.getTableSql()+"("); | |
216 | - Iterator<String> beanFieldIter = this._beanToTableField.keySet().iterator(); | |
217 | - while(beanFieldIter.hasNext()){ | |
218 | - String beanFieldName = beanFieldIter.next(); | |
219 | - String tableFieldName = this._beanToTableField.get(beanFieldName); | |
220 | - Field beanField = null; | |
221 | - try { | |
222 | - beanField = this._tClassInfo.getDeclaredField(beanFieldName); | |
223 | - beanField.setAccessible(true); | |
224 | - if(beanField.get(entity) == null) { | |
225 | - continue; | |
226 | - } | |
227 | - } catch (Exception e) { | |
228 | - continue; | |
229 | - } | |
230 | - | |
231 | - if(tableFieldName == null || beanFieldName == null){ | |
232 | - continue; | |
233 | - } | |
234 | - | |
235 | - beanFieldList.add(beanField); | |
236 | - sqlInsertPart.append("`"+tableFieldName+"`,"); | |
237 | - } | |
238 | - return sqlInsertPart.substring(0, sqlInsertPart.length()-1)+")"; | |
239 | - } | |
240 | - | |
241 | - private Object getDefaultValueByFieldType(Field itemField) { | |
242 | - String simpleName = itemField.getType().getSimpleName(); | |
243 | - if("String".equals(simpleName)) { | |
244 | - return ""; | |
245 | - }else if("Date".equals(simpleName) || "Timestamp".equals(simpleName)) { | |
246 | - return "2000-01-01 00:00:00"; | |
247 | - }else if("BigDecimal".equals(simpleName)){ | |
248 | - return BigDecimal.ZERO; | |
249 | - }else { | |
250 | - return 0; | |
251 | - } | |
252 | - } | |
253 | - | |
254 | - @Override | |
255 | - public T findEntityByID(ID id) throws NotFoundException { | |
256 | - return findEntityByID(id, false); | |
257 | - } | |
258 | - | |
259 | - @Override | |
260 | - public T findEntityByID(ID id, boolean isLock) throws NotFoundException { | |
261 | - StringBuffer sql = new StringBuffer("SELECT "+this._tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); | |
262 | - sql.append(" WHERE "+_idTableFieldName+" = ? "); | |
263 | - if (isLock) { | |
264 | - sql.append(" FOR UPDATE"); | |
265 | - } | |
266 | - try { | |
267 | - return (T) _jdbcTemplateBroadcast.queryForObject(sql.toString(), this._customJdbcTemplateRowMapper, new String[] {this._tableName}, id); | |
268 | - }catch (DataAccessException e) { | |
269 | - throw new NotFoundException(e); | |
270 | - } | |
271 | - } | |
272 | - | |
273 | - @Override | |
274 | - public T findEntityByCondition(List<Object[]> condition) throws NotFoundException, MultiRowException, NoContainTenantException { | |
275 | - List<T> tempList = findListByCondition(condition); | |
276 | - if(tempList == null || tempList.size() == 0){ | |
277 | - throw new NotFoundException(); | |
278 | - } | |
279 | - if(tempList != null && tempList.size() == 1){ | |
280 | - return tempList.get(0); | |
281 | - }else{ | |
282 | - throw new MultiRowException(); | |
283 | - } | |
284 | - } | |
285 | - | |
286 | - @Override | |
287 | - public T findEntityBySql(String sqlCondition) throws NotFoundException, MultiRowException { | |
288 | - List<T> tempList = findListBySql(sqlCondition); | |
289 | - if(tempList == null || tempList.size() == 0){ | |
290 | - throw new NotFoundException(); | |
291 | - } | |
292 | - if(tempList != null && tempList.size() == 1){ | |
293 | - return tempList.get(0); | |
294 | - }else{ | |
295 | - throw new MultiRowException(); | |
296 | - } | |
297 | - } | |
298 | - | |
299 | - @Override | |
300 | - public List<T> findListByCondition(List<Object[]> condition){ | |
301 | - return findListByCondition(condition, null); | |
302 | - } | |
303 | - | |
304 | - @Override | |
305 | - public List<T> findListByCondition(List<Object[]> condition, String sortCondition){ | |
306 | - StringBuffer sql = new StringBuffer("SELECT "+this._tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); | |
307 | - List<Object> list = new ArrayList<Object>(); | |
308 | - this.appendWhereCondition(sql, new StringBuffer(), list, condition); | |
309 | - if(sortCondition != null && !sortCondition.equals("")){ | |
310 | - sql.append(" " + sortCondition + " "); | |
311 | - } | |
312 | - return (List<T>)_jdbcTemplateBroadcast.query(sql.toString(), this._customJdbcTemplateRowMapper, new String[] {this._tableName}, list.toArray()); | |
313 | - } | |
314 | - | |
315 | - @Override | |
316 | - public List<T> findListBySql(String sqlCondition){ | |
317 | - StringBuffer sql = new StringBuffer("SELECT "+this._tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
318 | - return (List<T>)_jdbcTemplateBroadcast.query(sql.toString(), this._customJdbcTemplateRowMapper, new String[] {this._tableName}); | |
319 | - } | |
320 | - | |
321 | - @Override | |
322 | - public Map<String, Object> findPageByCondition(List<Object[]> condition, int page, int pageSize){ | |
323 | - return findPageByCondition(condition, null , page, pageSize); | |
324 | - } | |
325 | - | |
326 | - @Override | |
327 | - public Map<String, Object> findPageByCondition(List<Object[]> condition, String sortCondition, int page, int pageSize){ | |
328 | - StringBuffer sql = new StringBuffer("SELECT "+this._tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); | |
329 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
330 | - StringBuffer sqlCount = new StringBuffer("SELECT COUNT(1) rowCount FROM "+this.getTableSql()); | |
331 | - List<Object> count_list = new ArrayList<Object>(); | |
332 | - List<Object> page_list = new ArrayList<Object>(); | |
333 | - this.appendWhereConditionForCount(sqlCount, condition); | |
334 | - this.appendWhereCondition(sql, pql, count_list, condition); | |
335 | - for (int i = 0; i < count_list.size(); i++) | |
336 | - page_list.add(count_list.get(i)); | |
337 | - | |
338 | - page_list.add((page - 1) * pageSize); | |
339 | - page_list.add(pageSize); | |
340 | - | |
341 | - if(sortCondition != null && !sortCondition.equals("")){ | |
342 | - sql.append(" " + sortCondition + " "); | |
343 | - pql.append(" " + sortCondition + " "); | |
344 | - } | |
345 | - | |
346 | - String pageSql = sql.toString() + " limit ?, ?"; | |
347 | - Map<String, Object> totalRowsMap = _jdbcTemplateBroadcast.queryForMap(sqlCount.toString(), new String[] {this._tableName}, count_list.toArray()) ; | |
348 | - List<T> resultList = _jdbcTemplateBroadcast.query(pageSql.toString(), this._customJdbcTemplateRowMapper, new String[] {this._tableName}, page_list.toArray()); | |
349 | - return UtilsSql.createPage(page, pageSize, Integer.valueOf(totalRowsMap.get("rowCount").toString()), resultList); | |
350 | - } | |
351 | - | |
352 | - @Override | |
353 | - public Map<String, Object> findPageBySql(String sqlCondition, int page, int pageSize){ | |
354 | - StringBuffer sql = new StringBuffer("SELECT "+this._tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
355 | - StringBuffer sqlCount = new StringBuffer("SELECT count(1) rowCount FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
356 | - sql.append(" limit ?, ?"); | |
357 | - List<Object> page_list = new ArrayList<Object>(); | |
358 | - page_list.add((page - 1) * pageSize); | |
359 | - page_list.add(page * pageSize); | |
360 | - | |
361 | - Map<String, Object> totalRowsMap = _jdbcTemplateBroadcast.queryForMap(sqlCount.toString(), new String[] {this._tableName}); | |
362 | - List<T> resultList = _jdbcTemplateBroadcast.query(sql.toString(), this._customJdbcTemplateRowMapper, new String[] {this._tableName}, page_list.toArray()); | |
363 | - return UtilsSql.createPage(page, pageSize, Integer.valueOf(totalRowsMap.get("rowCount").toString()), resultList); | |
364 | - } | |
365 | - | |
366 | - @Override | |
367 | - public Number addEntity(T entity) { | |
368 | - StringBuffer sqlInsertPart = new StringBuffer("INSERT INTO "+this.getTableSql()+"("); | |
369 | - StringBuffer sqlColumnPart = new StringBuffer(") VALUES ("); | |
370 | - List<Object> paramList = new ArrayList<Object>(); | |
371 | - | |
372 | - Iterator<String> beanFieldIter = this._beanToTableField.keySet().iterator(); | |
373 | - while(beanFieldIter.hasNext()){ | |
374 | - String beanFieldName = beanFieldIter.next(); | |
375 | - String tableFieldName = this._beanToTableField.get(beanFieldName); | |
376 | - Field beanField; | |
377 | - Object beanFieldValue = null; | |
378 | - try { | |
379 | - beanField = this._tClassInfo.getDeclaredField(beanFieldName); | |
380 | - beanField.setAccessible(true); | |
381 | - beanFieldValue = beanField.get(entity); | |
382 | - } catch (Exception e) { | |
383 | - e.printStackTrace(); | |
384 | - } | |
385 | - | |
386 | - if(tableFieldName == null || beanFieldName == null || beanFieldValue == null){ | |
387 | - continue; | |
388 | - } | |
389 | - sqlInsertPart.append("`"+tableFieldName+"`,"); | |
390 | - sqlColumnPart.append(" ?,"); | |
391 | - paramList.add(beanFieldValue); | |
392 | - } | |
393 | - | |
394 | - //执行SQL | |
395 | - String exeSql = sqlInsertPart.substring(0, sqlInsertPart.length()-1)+sqlColumnPart.substring(0, sqlColumnPart.length()-1)+")"; | |
396 | - KeyHolder _keyHolder = new GeneratedKeyHolder(new ArrayList<Map<String,Object>>(1)); | |
397 | - _jdbcTemplateBroadcast.update(new PreparedStatementCreator() { | |
398 | - @Override | |
399 | - public PreparedStatement createPreparedStatement(Connection con) throws SQLException { | |
400 | - PreparedStatement stat = con.prepareStatement(exeSql, new String[] {_idTableFieldName}); | |
401 | - PreparedStatementSetter setter = new ArgumentPreparedStatementSetter(paramList.toArray()); | |
402 | - setter.setValues(stat); | |
403 | - return stat; | |
404 | - } | |
405 | - | |
406 | - }, _keyHolder, new String[] {this._tableName}); | |
407 | - | |
408 | - return _keyHolder.getKey(); | |
409 | - } | |
410 | - | |
411 | - @Override | |
412 | - public List<Number> addEntityList(List<T> entityList) throws Exception { | |
413 | - if(entityList == null || entityList.isEmpty()) { | |
414 | - return Collections.EMPTY_LIST; | |
415 | - } | |
416 | - //构造SQL语句及Entity Field列表 | |
417 | - List<Field> beanFieldList = new ArrayList<Field>(this._beanToTableField.size()); | |
418 | - StringBuffer exeSql = new StringBuffer(this.constructUpdateSql(entityList.get(0), beanFieldList)); | |
419 | - | |
420 | - //构造参数信息 | |
421 | - List<Object> args = new ArrayList<Object>(); | |
422 | - exeSql.append(" VALUES"); | |
423 | - for(int itemIndex=0; itemIndex<entityList.size(); ++itemIndex) { | |
424 | - T item = entityList.get(itemIndex); | |
425 | - exeSql.append("("); | |
426 | - for(int i=0; i<beanFieldList.size(); ++i) { | |
427 | - Field itemField = beanFieldList.get(i); | |
428 | - Object itemData = null; | |
429 | - try { | |
430 | - itemData = itemField.get(item); | |
431 | - } catch (Exception e) { | |
432 | - throw new ObjectReflectException(); | |
433 | - } | |
434 | - if(itemData == null) { | |
435 | - args.add(this.getDefaultValueByFieldType(itemField)); | |
436 | - }else { | |
437 | - args.add(itemData); | |
438 | - } | |
439 | - exeSql.append("?,"); | |
440 | - } | |
441 | - exeSql.setCharAt(exeSql.length()-1, ' '); | |
442 | - exeSql.append("),"); | |
443 | - } | |
444 | - exeSql.setCharAt(exeSql.length()-1, ';'); | |
445 | - | |
446 | - //调用更新接口 | |
447 | - KeyHolder _keyHolder = new GeneratedKeyHolder(new ArrayList<Map<String,Object>>(entityList.size())); | |
448 | - _jdbcTemplateBroadcast.update(new PreparedStatementCreator() { | |
449 | - @Override | |
450 | - public PreparedStatement createPreparedStatement(Connection con) throws SQLException { | |
451 | - PreparedStatement stat = con.prepareStatement(exeSql.toString(), new String[] {_idTableFieldName}); | |
452 | - PreparedStatementSetter setter = new ArgumentPreparedStatementSetter(args.toArray()); | |
453 | - setter.setValues(stat); | |
454 | - return stat; | |
455 | - } | |
456 | - | |
457 | - }, _keyHolder, new String[] {this._tableName}); | |
458 | - | |
459 | - //处理结果数据 | |
460 | - List<Map<String, Object>> data = _keyHolder.getKeyList(); | |
461 | - if(data.size() != entityList.size()) { | |
462 | - throw new Exception("param entity size not equal return generate key list size"); | |
463 | - } | |
464 | - List<Number> dataT = new ArrayList<Number>(data.size()); | |
465 | - for(Map<String, Object> item: data) { | |
466 | - Iterator<Object> keyIter = item.values().iterator(); | |
467 | - if (keyIter.hasNext()) { | |
468 | - Object key = keyIter.next(); | |
469 | - if (!(key instanceof Number)) { | |
470 | - throw new DataRetrievalFailureException( | |
471 | - "The generated key is not of a supported numeric type. " + | |
472 | - "Unable to cast [" + (key != null ? key.getClass().getName() : null) + | |
473 | - "] to [" + Number.class.getName() + "]"); | |
474 | - } | |
475 | - dataT.add((Number)key); | |
476 | - }else { | |
477 | - throw new DataRetrievalFailureException("Unable to retrieve the generated key. " + | |
478 | - "Check that the table has an identity column enabled."); | |
479 | - } | |
480 | - } | |
481 | - return dataT; | |
482 | - } | |
483 | - | |
484 | - @Override | |
485 | - public int deleteEntityByID(ID id) { | |
486 | - StringBuffer sql = new StringBuffer("DELETE FROM "+this.getTableSql()+" WHERE"); | |
487 | - sql.append(" "+this._idTableFieldName+" = ? "); | |
488 | - return _jdbcTemplateBroadcast.update(sql.toString(), new String[] {this._tableName}, id); | |
489 | - } | |
490 | - | |
491 | - @Override | |
492 | - public int deleteEntityByCondition(List<Object[]> condition){ | |
493 | - if (null == condition || condition.size() == 0) { | |
494 | - throw new RuntimeException("params[condition] is empty"); | |
495 | - } | |
496 | - | |
497 | - List<Object> list = new ArrayList<Object>(); | |
498 | - StringBuffer sql = new StringBuffer("DELETE FROM "+this.getTableSql()+""); | |
499 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
500 | - this.appendWhereCondition(sql, pql, list, condition); | |
501 | - return _jdbcTemplateBroadcast.update( sql.toString(), new String[] {this._tableName}, list.toArray()); | |
502 | - } | |
503 | - | |
504 | - @Override | |
505 | - public int deleteEntityBySql(String sqlCondition){ | |
506 | - if("".equals(sqlCondition.trim())) { | |
507 | - throw new RuntimeException("params[sqlCondition] is empty"); | |
508 | - } | |
509 | - return _jdbcTemplateBroadcast.update( "DELETE FROM "+this.getTableSql()+" WHERE " + sqlCondition, new String[] {this._tableName}); | |
510 | - } | |
511 | - | |
512 | - @Override | |
513 | - public int deleteEntityList(List<ID> idList){ | |
514 | - StringBuffer idSb = new StringBuffer(); | |
515 | - for(ID id: idList) { | |
516 | - idSb.append(id); | |
517 | - } | |
518 | - return this.deleteEntityBySql(this._idTableFieldName + " in ("+idSb.toString().substring(0, idSb.length()-1)+") "); | |
519 | - } | |
520 | - | |
521 | - @Override | |
522 | - public int updateEntityById(List<Object[]> changeList, ID id) { | |
523 | - if(null == id){ | |
524 | - throw new RuntimeException("params[id] is null"); | |
525 | - } | |
526 | - if (null == changeList || changeList.size() == 0) { | |
527 | - throw new RuntimeException("params[changeList] is empty"); | |
528 | - } | |
529 | - | |
530 | - StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); | |
531 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
532 | - List<Object> list = new ArrayList<Object>(); | |
533 | - this.appendSetSql(sql, pql, list, changeList); | |
534 | - | |
535 | - String where = " WHERE "+this._idTableFieldName+"=? "; | |
536 | - String updateSql = sql.substring(0, sql.length()-1)+where; | |
537 | - list.add(id); | |
538 | - return _jdbcTemplateBroadcast.update(updateSql, new String[] {this._tableName}, list.toArray()); | |
539 | - } | |
540 | - | |
541 | - @Override | |
542 | - public int updateEntityByCondition(List<Object[]> updateObj, List<Object[]> condition){ | |
543 | - if (null == updateObj || updateObj.size() == 0) { | |
544 | - throw new RuntimeException("params[updateObj] is empty"); | |
545 | - } | |
546 | - if (null == condition || condition.size() == 0) { | |
547 | - throw new RuntimeException("params[condition] is empty"); | |
548 | - } | |
549 | - | |
550 | - StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); | |
551 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
552 | - List<Object> list = new ArrayList<Object>(); | |
553 | - this.appendSetSql(sql, pql, list, updateObj); | |
554 | - | |
555 | - StringBuffer where = new StringBuffer(""); | |
556 | - StringBuffer pwhere = new StringBuffer(""); | |
557 | - this.appendWhereCondition(where, pwhere, list, condition); | |
558 | - | |
559 | - String updateSql = sql.substring(0, sql.length()-1)+where.toString(); | |
560 | - return _jdbcTemplateBroadcast.update(updateSql, new String[] {this._tableName}, list.toArray()); | |
561 | - } | |
562 | - | |
563 | - @Override | |
564 | - public int updateEntityBySql(List<Object[]> updateObj, String sqlCondition){ | |
565 | - if (null == updateObj || updateObj.size() == 0) { | |
566 | - throw new RuntimeException("params[updateObj] is empty"); | |
567 | - } | |
568 | - if ("".equals(sqlCondition)) { | |
569 | - throw new RuntimeException("params[sqlCondition] is empty"); | |
570 | - } | |
571 | - | |
572 | - StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); | |
573 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
574 | - List<Object> list = new ArrayList<Object>(); | |
575 | - this.appendSetSql(sql, pql, list, updateObj); | |
576 | - | |
577 | - String updateSql = sql.toString().substring(0, sql.length()-1) + " WHERE "+sqlCondition; | |
578 | - return _jdbcTemplateBroadcast.update(updateSql, new String[] {this._tableName}, list.toArray()); | |
579 | - } | |
580 | - | |
581 | - @Override | |
582 | - public Map<String, Object> getPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize){ | |
583 | - String[] splitedSql = UtilsSql.splitCoreSql(coreSql); | |
584 | - return this.getPageData(splitedSql[0], splitedSql[1], orderByPartSql, page, pageSize); | |
585 | - } | |
586 | - | |
587 | - @Override | |
588 | - public Map<String, Object> getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize){ | |
589 | - //构造查询语句 | |
590 | - String querySql = selectSql+" "+fromAndWhereSql+" "+orderByPartSql+" "+UtilsSql.getLimitCondition(page, pageSize); | |
591 | - | |
592 | - //构造统计计数语句 | |
593 | - String countSql = "select count(*) rowsCount from ( select 1 "+fromAndWhereSql+" ) t "; | |
594 | - | |
595 | - //执行查询 | |
596 | - List<Map<String, Object>> queryData = new ArrayList<Map<String, Object>>(); | |
597 | - Map<String, Object> countData = new HashMap<String, Object>(); | |
598 | - queryData = this._jdbcTemplateBroadcast.queryForList(querySql, new String[] {this._tableName}); | |
599 | - countData = this._jdbcTemplateBroadcast.queryForMap(countSql, new String[] {this._tableName}); | |
600 | - return UtilsSql.createPage(page, pageSize, Integer.valueOf(countData.get("rowsCount").toString()), queryData); | |
601 | - } | |
602 | -} |
src/main/java/com/taover/repository/CustomJdbcTemplateBroadcastInterface.java
... | ... | @@ -1,153 +0,0 @@ |
1 | -package com.taover.repository; | |
2 | - | |
3 | -import java.io.Serializable; | |
4 | -import java.util.List; | |
5 | -import java.util.Map; | |
6 | - | |
7 | -import com.taover.repository.exception.MultiRowException; | |
8 | -import com.taover.repository.exception.NoContainTenantException; | |
9 | -import com.taover.repository.exception.NotFoundException; | |
10 | - | |
11 | -public interface CustomJdbcTemplateBroadcastInterface<T, ID extends Serializable> { | |
12 | - | |
13 | - /** | |
14 | - * 按主键查询 | |
15 | - */ | |
16 | - public T findEntityByID(ID id) throws NotFoundException; | |
17 | - | |
18 | - /** | |
19 | - * 按主键查询 | |
20 | - * isLock 是否锁定, 默认不锁 | |
21 | - * fromWriteDB 是否从写库读写,默认从读库查询 | |
22 | - */ | |
23 | - public T findEntityByID(ID id, boolean isLock) throws NotFoundException; | |
24 | - | |
25 | - /** | |
26 | - * 根据条件List<Object[]>查询 | |
27 | - * Object[]数组长度是3 | |
28 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
29 | - */ | |
30 | - public T findEntityByCondition(List<Object[]> condition) throws NotFoundException,MultiRowException,NoContainTenantException; | |
31 | - | |
32 | - /** | |
33 | - * 根据条件sql查询 | |
34 | - * sqlCondition 为where 后面的条件。 | |
35 | - */ | |
36 | - public T findEntityBySql(String sqlCondition) throws NotFoundException,MultiRowException,NoContainTenantException; | |
37 | - | |
38 | - /** | |
39 | - * 根据条件List<Object[]>查询 | |
40 | - * Object[]数组长度是3 | |
41 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
42 | - */ | |
43 | - public List<T> findListByCondition(List<Object[]> condition); | |
44 | - | |
45 | - /** | |
46 | - * 根据条件List<Object[]>查询 | |
47 | - * Object[]数组长度是3 | |
48 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
49 | - */ | |
50 | - public List<T> findListByCondition(List<Object[]> condition, String sortCondition); | |
51 | - | |
52 | - /** | |
53 | - * 根据条件sql查询 | |
54 | - * sqlCondition 为where 后面的条件。 | |
55 | - */ | |
56 | - public List<T> findListBySql(String sqlCondition); | |
57 | - | |
58 | - /** | |
59 | - * 按条件分页查询 | |
60 | - * Object[]数组长度是3 | |
61 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
62 | - */ | |
63 | - public Map<String, Object> findPageByCondition(List<Object[]> condition, int page, int pageSize); | |
64 | - | |
65 | - /** | |
66 | - * 按条件分页查询 | |
67 | - * Object[]数组长度是3 | |
68 | - * Object[]第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
69 | - * boolean isUseCache, 是否用缓存,默认用。 | |
70 | - * boolean isAddCache, 是否添加缓存,默认添加。 | |
71 | - */ | |
72 | - public Map<String, Object> findPageByCondition(List<Object[]> condition, String sortCondition, int page, int pageSize); | |
73 | - | |
74 | - /** | |
75 | - * 按sql分页查询, sqlCondition为where 后条件sql | |
76 | - */ | |
77 | - public Map<String, Object> findPageBySql(String sqlCondition, int page, int pageSize); | |
78 | - | |
79 | - /** | |
80 | - * 添加 | |
81 | - */ | |
82 | - public Number addEntity(T entity); | |
83 | - | |
84 | - /** | |
85 | - * 批量添加 | |
86 | - * @throws Exception | |
87 | - */ | |
88 | - public List<Number> addEntityList(List<T> entityList) throws Exception; | |
89 | - | |
90 | - /** | |
91 | - * 按ID删除 | |
92 | - */ | |
93 | - public int deleteEntityByID(ID id); | |
94 | - | |
95 | - /** | |
96 | - * 删除按List<Object[]>条件 | |
97 | - * Object[]数组长度是3 | |
98 | - * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
99 | - */ | |
100 | - public int deleteEntityByCondition(List<Object[]> condition); | |
101 | - | |
102 | - /** | |
103 | - * 删除按condition条件 | |
104 | - * 建议使用deleteTByCondition(List<Object[]> condition), 如果removeTByCondition(List<Object[]> condition)满足不了where条件可以使用此方法。 | |
105 | - * condition为where后面的条件,condition不能为空。 | |
106 | - */ | |
107 | - public int deleteEntityBySql(String sqlCondition); | |
108 | - | |
109 | - /** | |
110 | - * 根据list对象逐个删除。 | |
111 | - * @throws NoContainTenantException | |
112 | - */ | |
113 | - public int deleteEntityList(List<ID> idList); | |
114 | - | |
115 | - /** | |
116 | - * 根据ID修改指定的值 | |
117 | - */ | |
118 | - public int updateEntityById(List<Object[]> changeList, ID id); | |
119 | - | |
120 | - /** | |
121 | - * List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 | |
122 | - * List<Object[]> condition 修改的条件, 数组长度是3, 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
123 | - */ | |
124 | - public int updateEntityByCondition(List<Object[]> updateObj, List<Object[]> condition); | |
125 | - | |
126 | - /** | |
127 | - * List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 | |
128 | - * String sqlCondition 修改的条件。 | |
129 | - */ | |
130 | - public int updateEntityBySql(List<Object[]> updateObj, String sqlCondition); | |
131 | - | |
132 | - /** | |
133 | - * 获取分页数据 | |
134 | - * @param coreSql | |
135 | - * @param orderByPartSql | |
136 | - * @param page | |
137 | - * @param pageSize | |
138 | - * @return | |
139 | - */ | |
140 | - public Map<String, Object> getPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize); | |
141 | - | |
142 | - /** | |
143 | - * 获取分页数据 | |
144 | - * @param selectSql | |
145 | - * @param fromAndWhereSql | |
146 | - * @param orderByPartSql | |
147 | - * @param page | |
148 | - * @param pageSize | |
149 | - * @return | |
150 | - */ | |
151 | - public Map<String, Object> getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize); | |
152 | - | |
153 | -} |
src/main/java/com/taover/repository/CustomJdbcTemplateWrapperTenant.java
... | ... | @@ -58,6 +58,10 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
58 | 58 | public CustomJdbcTemplateRowMapper<T> getCustomJdbcTemplateRowMapper(){ |
59 | 59 | return this._customJdbcTemplateRowMapper; |
60 | 60 | } |
61 | + public CustomJdbcTemplateWrapperTenant(JdbcTemplateWrapperTenant jdbcTemplate) throws Exception{ | |
62 | + this(); | |
63 | + this._jdbcTemplateWrapperTenant = jdbcTemplate; | |
64 | + } | |
61 | 65 | |
62 | 66 | @SuppressWarnings("unchecked") |
63 | 67 | public CustomJdbcTemplateWrapperTenant() throws Exception{ |
... | ... | @@ -396,7 +400,7 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
396 | 400 | //执行SQL |
397 | 401 | String exeSql = sqlInsertPart.substring(0, sqlInsertPart.length()-1)+sqlColumnPart.substring(0, sqlColumnPart.length()-1)+")"; |
398 | 402 | KeyHolder _keyHolder = new GeneratedKeyHolder(new ArrayList<Map<String,Object>>(1)); |
399 | - _jdbcTemplateWrapperTenant.update(new PreparedStatementCreator() { | |
403 | + _jdbcTemplateWrapperTenant.getJdbcTemplate().update(new PreparedStatementCreator() { | |
400 | 404 | @Override |
401 | 405 | public PreparedStatement createPreparedStatement(Connection con) throws SQLException { |
402 | 406 | PreparedStatement stat = con.prepareStatement(exeSql, new String[] {_idTableFieldName}); |
... | ... | @@ -405,7 +409,7 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
405 | 409 | return stat; |
406 | 410 | } |
407 | 411 | |
408 | - }, _keyHolder, tenantId); | |
412 | + }, _keyHolder); | |
409 | 413 | |
410 | 414 | return _keyHolder.getKey(); |
411 | 415 | } |
... | ... | @@ -447,7 +451,7 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
447 | 451 | |
448 | 452 | //调用更新接口 |
449 | 453 | KeyHolder _keyHolder = new GeneratedKeyHolder(new ArrayList<Map<String,Object>>(entityList.size())); |
450 | - _jdbcTemplateWrapperTenant.update(new PreparedStatementCreator() { | |
454 | + _jdbcTemplateWrapperTenant.getJdbcTemplate().update(new PreparedStatementCreator() { | |
451 | 455 | @Override |
452 | 456 | public PreparedStatement createPreparedStatement(Connection con) throws SQLException { |
453 | 457 | PreparedStatement stat = con.prepareStatement(exeSql.toString(), new String[] {_idTableFieldName}); |
... | ... | @@ -456,7 +460,7 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
456 | 460 | return stat; |
457 | 461 | } |
458 | 462 | |
459 | - }, _keyHolder, tenantId); | |
463 | + }, _keyHolder); | |
460 | 464 | |
461 | 465 | //处理结果数据 |
462 | 466 | List<Map<String, Object>> data = _keyHolder.getKeyList(); | ... | ... |
src/main/java/com/taover/repository/advice/EntityPointCut.java
src/main/java/com/taover/repository/autoconfigure/ShardingSphereKeyGeneratorConfiguration.java
... | ... | @@ -1,31 +0,0 @@ |
1 | -package com.taover.repository.autoconfigure; | |
2 | - | |
3 | -import org.springframework.boot.context.properties.ConfigurationProperties; | |
4 | -import org.springframework.context.annotation.Configuration; | |
5 | - | |
6 | -@Configuration | |
7 | -@ConfigurationProperties(prefix = "taover.sharding") | |
8 | -public class ShardingSphereKeyGeneratorConfiguration { | |
9 | - private String workerId; | |
10 | - private String maxTolerateTimeDifferenceMilliseconds; | |
11 | - private String maxVibrationOffset; | |
12 | - | |
13 | - public String getWorkerId() { | |
14 | - return workerId; | |
15 | - } | |
16 | - public void setWorkerId(String workerId) { | |
17 | - this.workerId = workerId; | |
18 | - } | |
19 | - public String getMaxTolerateTimeDifferenceMilliseconds() { | |
20 | - return maxTolerateTimeDifferenceMilliseconds; | |
21 | - } | |
22 | - public void setMaxTolerateTimeDifferenceMilliseconds(String maxTolerateTimeDifferenceMilliseconds) { | |
23 | - this.maxTolerateTimeDifferenceMilliseconds = maxTolerateTimeDifferenceMilliseconds; | |
24 | - } | |
25 | - public String getMaxVibrationOffset() { | |
26 | - return maxVibrationOffset; | |
27 | - } | |
28 | - public void setMaxVibrationOffset(String maxVibrationOffset) { | |
29 | - this.maxVibrationOffset = maxVibrationOffset; | |
30 | - } | |
31 | -} |
src/main/java/com/taover/repository/autoconfigure/TaoverRepositoryAutoConfiguration.java
... | ... | @@ -1,55 +0,0 @@ |
1 | -package com.taover.repository.autoconfigure; | |
2 | - | |
3 | -import javax.annotation.Resource; | |
4 | - | |
5 | -import org.springframework.boot.autoconfigure.AutoConfigureAfter; | |
6 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | |
7 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | |
8 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | |
9 | -import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; | |
10 | -import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
11 | -import org.springframework.context.annotation.Bean; | |
12 | -import org.springframework.context.annotation.Configuration; | |
13 | -import org.springframework.jdbc.core.JdbcTemplate; | |
14 | - | |
15 | -import com.taover.repository.jdbctemplate.JdbcTemplateBroadcast; | |
16 | -import com.taover.repository.jdbctemplate.JdbcTemplateBroadcastImpl; | |
17 | -import com.taover.repository.jdbctemplate.JdbcTemplateWrapperTenant; | |
18 | -import com.taover.repository.jdbctemplate.JdbcTemplateWrapperTenantImpl; | |
19 | -import com.taover.repository.shardingsphere.ShardingInfoRepository; | |
20 | -import com.taover.repository.shardingsphere.ShardingSphereService; | |
21 | - | |
22 | -@Configuration | |
23 | -@EnableConfigurationProperties({ShardingSphereKeyGeneratorConfiguration.class}) | |
24 | -@AutoConfigureAfter(JdbcTemplateAutoConfiguration.class) | |
25 | -@ConditionalOnProperty(prefix = "spring.shardingsphere", name = "enabled", havingValue = "true", matchIfMissing = true) | |
26 | -@ConditionalOnClass({ShardingSphereService.class}) | |
27 | -@ConditionalOnMissingBean(ShardingSphereService.class) | |
28 | -public class TaoverRepositoryAutoConfiguration { | |
29 | - @Resource | |
30 | - private JdbcTemplate jdbcTemplate; | |
31 | - @Resource | |
32 | - private ShardingSphereKeyGeneratorConfiguration config; | |
33 | - @Resource | |
34 | - private ShardingSphereService shardingSphereService; | |
35 | - | |
36 | - @Bean | |
37 | - public ShardingSphereService shardingSphereService() { | |
38 | - return new ShardingSphereService(config); | |
39 | - } | |
40 | - | |
41 | - @Bean | |
42 | - public ShardingInfoRepository shardingInfoRepository() throws Exception { | |
43 | - return new ShardingInfoRepository(); | |
44 | - } | |
45 | - | |
46 | - @Bean | |
47 | - public JdbcTemplateWrapperTenant jdbcTemplateWrapperTenant() { | |
48 | - return new JdbcTemplateWrapperTenantImpl(this.jdbcTemplate, this.shardingSphereService); | |
49 | - } | |
50 | - | |
51 | - @Bean | |
52 | - public JdbcTemplateBroadcast jdbcTemplateWrapperBroadcast() { | |
53 | - return new JdbcTemplateBroadcastImpl(this.jdbcTemplate, this.shardingSphereService); | |
54 | - } | |
55 | -} |
src/main/java/com/taover/repository/bean/TenantIdentity.java
src/main/java/com/taover/repository/exception/NoContainTenantException.java
1 | 1 | package com.taover.repository.exception; |
2 | 2 | |
3 | -public class NoContainTenantException extends Exception { | |
3 | +public class NoContainTenantException extends RuntimeException { | |
4 | + private static final long serialVersionUID = 1L; | |
5 | + private String sql; | |
4 | 6 | |
7 | + public NoContainTenantException(String sql) { | |
8 | + super("sql : "+sql); | |
9 | + this.sql = sql; | |
10 | + } | |
11 | + | |
12 | + public String getSql() { | |
13 | + return sql; | |
14 | + } | |
15 | + | |
16 | + public void setSql(String sql) { | |
17 | + this.sql = sql; | |
18 | + } | |
5 | 19 | } | ... | ... |
src/main/java/com/taover/repository/jdbctemplate/JdbcTemplateBroadcast.java
... | ... | @@ -1,976 +0,0 @@ |
1 | -package com.taover.repository.jdbctemplate; | |
2 | - | |
3 | -import java.util.Collection; | |
4 | -import java.util.List; | |
5 | -import java.util.Map; | |
6 | - | |
7 | -import org.springframework.dao.DataAccessException; | |
8 | -import org.springframework.dao.IncorrectResultSizeDataAccessException; | |
9 | -import org.springframework.jdbc.core.BatchPreparedStatementSetter; | |
10 | -import org.springframework.jdbc.core.CallableStatementCallback; | |
11 | -import org.springframework.jdbc.core.CallableStatementCreator; | |
12 | -import org.springframework.jdbc.core.ColumnMapRowMapper; | |
13 | -import org.springframework.jdbc.core.ConnectionCallback; | |
14 | -import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter; | |
15 | -import org.springframework.jdbc.core.PreparedStatementCallback; | |
16 | -import org.springframework.jdbc.core.PreparedStatementCreator; | |
17 | -import org.springframework.jdbc.core.PreparedStatementCreatorFactory; | |
18 | -import org.springframework.jdbc.core.PreparedStatementSetter; | |
19 | -import org.springframework.jdbc.core.ResultSetExtractor; | |
20 | -import org.springframework.jdbc.core.RowCallbackHandler; | |
21 | -import org.springframework.jdbc.core.RowMapper; | |
22 | -import org.springframework.jdbc.core.SingleColumnRowMapper; | |
23 | -import org.springframework.jdbc.core.SqlParameter; | |
24 | -import org.springframework.jdbc.core.SqlParameterValue; | |
25 | -import org.springframework.jdbc.core.SqlRowSetResultSetExtractor; | |
26 | -import org.springframework.jdbc.core.StatementCallback; | |
27 | -import org.springframework.jdbc.support.KeyHolder; | |
28 | -import org.springframework.jdbc.support.rowset.SqlRowSet; | |
29 | -import org.springframework.lang.Nullable; | |
30 | - | |
31 | -public interface JdbcTemplateBroadcast { | |
32 | - /** | |
33 | - * 加载分片信息 | |
34 | - * @param tenantId | |
35 | - */ | |
36 | - public void loadShardingInfo(String[] broadcastTableNames); | |
37 | - | |
38 | - /** | |
39 | - * 清空分片信息 | |
40 | - */ | |
41 | - public void clearShardingInfo(); | |
42 | - | |
43 | - //------------------------------------------------------------------------- | |
44 | - // Methods dealing with a plain java.sql.Connection | |
45 | - //------------------------------------------------------------------------- | |
46 | - | |
47 | - /** | |
48 | - * Execute a JDBC data access operation, implemented as callback action | |
49 | - * working on a JDBC Connection. This allows for implementing arbitrary | |
50 | - * data access operations, within Spring's managed JDBC environment: | |
51 | - * that is, participating in Spring-managed transactions and converting | |
52 | - * JDBC SQLExceptions into Spring's DataAccessException hierarchy. | |
53 | - * <p>The callback action can return a result object, for example a domain | |
54 | - * object or a collection of domain objects. | |
55 | - * @param action a callback object that specifies the action | |
56 | - * @return a result object returned by the action, or {@code null} if none | |
57 | - * @throws DataAccessException if there is any problem | |
58 | - */ | |
59 | - @Nullable | |
60 | - <T> T execute(ConnectionCallback<T> action, String[] broadcastTableNames) throws DataAccessException; | |
61 | - | |
62 | - | |
63 | - //------------------------------------------------------------------------- | |
64 | - // Methods dealing with static SQL (java.sql.Statement) | |
65 | - //------------------------------------------------------------------------- | |
66 | - | |
67 | - /** | |
68 | - * Execute a JDBC data access operation, implemented as callback action | |
69 | - * working on a JDBC Statement. This allows for implementing arbitrary data | |
70 | - * access operations on a single Statement, within Spring's managed JDBC | |
71 | - * environment: that is, participating in Spring-managed transactions and | |
72 | - * converting JDBC SQLExceptions into Spring's DataAccessException hierarchy. | |
73 | - * <p>The callback action can return a result object, for example a domain | |
74 | - * object or a collection of domain objects. | |
75 | - * @param action a callback that specifies the action | |
76 | - * @return a result object returned by the action, or {@code null} if none | |
77 | - * @throws DataAccessException if there is any problem | |
78 | - */ | |
79 | - @Nullable | |
80 | - <T> T execute(StatementCallback<T> action, String[] broadcastTableNames) throws DataAccessException; | |
81 | - | |
82 | - /** | |
83 | - * Issue a single SQL execute, typically a DDL statement. | |
84 | - * @param sql static SQL to execute | |
85 | - * @throws DataAccessException if there is any problem | |
86 | - */ | |
87 | - void execute(String sql, String[] broadcastTableNames) throws DataAccessException; | |
88 | - | |
89 | - /** | |
90 | - * Execute a query given static SQL, reading the ResultSet with a | |
91 | - * ResultSetExtractor. | |
92 | - * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to | |
93 | - * execute a static query with a PreparedStatement, use the overloaded | |
94 | - * {@code query} method with {@code null} as argument array. | |
95 | - * @param sql the SQL query to execute | |
96 | - * @param rse a callback that will extract all rows of results | |
97 | - * @return an arbitrary result object, as returned by the ResultSetExtractor | |
98 | - * @throws DataAccessException if there is any problem executing the query | |
99 | - * @see #query(String, Object[], ResultSetExtractor) | |
100 | - */ | |
101 | - @Nullable | |
102 | - <T> T query(String sql, ResultSetExtractor<T> rse, String[] broadcastTableNames) throws DataAccessException; | |
103 | - | |
104 | - /** | |
105 | - * Execute a query given static SQL, reading the ResultSet on a per-row | |
106 | - * basis with a RowCallbackHandler. | |
107 | - * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to | |
108 | - * execute a static query with a PreparedStatement, use the overloaded | |
109 | - * {@code query} method with {@code null} as argument array. | |
110 | - * @param sql the SQL query to execute | |
111 | - * @param rch a callback that will extract results, one row at a time | |
112 | - * @throws DataAccessException if there is any problem executing the query | |
113 | - * @see #query(String, Object[], RowCallbackHandler) | |
114 | - */ | |
115 | - void query(String sql, RowCallbackHandler rch, String[] broadcastTableNames) throws DataAccessException; | |
116 | - | |
117 | - /** | |
118 | - * Execute a query given static SQL, mapping each row to a result object | |
119 | - * via a RowMapper. | |
120 | - * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to | |
121 | - * execute a static query with a PreparedStatement, use the overloaded | |
122 | - * {@code query} method with {@code null} as argument array. | |
123 | - * @param sql the SQL query to execute | |
124 | - * @param rowMapper a callback that will map one object per row | |
125 | - * @return the result List, containing mapped objects | |
126 | - * @throws DataAccessException if there is any problem executing the query | |
127 | - * @see #query(String, Object[], RowMapper) | |
128 | - */ | |
129 | - <T> List<T> query(String sql, RowMapper<T> rowMapper, String[] broadcastTableNames) throws DataAccessException; | |
130 | - | |
131 | - /** | |
132 | - * Execute a query given static SQL, mapping a single result row to a | |
133 | - * result object via a RowMapper. | |
134 | - * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to | |
135 | - * execute a static query with a PreparedStatement, use the overloaded | |
136 | - * {@link #queryForObject(String, RowMapper, Object...)} method with | |
137 | - * {@code null} as argument array. | |
138 | - * @param sql the SQL query to execute | |
139 | - * @param rowMapper a callback that will map one object per row | |
140 | - * @return the single mapped object (may be {@code null} if the given | |
141 | - * {@link RowMapper} returned {@code} null) | |
142 | - * @throws IncorrectResultSizeDataAccessException if the query does not | |
143 | - * return exactly one row | |
144 | - * @throws DataAccessException if there is any problem executing the query | |
145 | - * @see #queryForObject(String, Object[], RowMapper) | |
146 | - */ | |
147 | - @Nullable | |
148 | - <T> T queryForObject(String sql, RowMapper<T> rowMapper, String[] broadcastTableNames) throws DataAccessException; | |
149 | - | |
150 | - /** | |
151 | - * Execute a query for a result object, given static SQL. | |
152 | - * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to | |
153 | - * execute a static query with a PreparedStatement, use the overloaded | |
154 | - * {@link #queryForObject(String, Class, Object...)} method with | |
155 | - * {@code null} as argument array. | |
156 | - * <p>This method is useful for running static SQL with a known outcome. | |
157 | - * The query is expected to be a single row/single column query; the returned | |
158 | - * result will be directly mapped to the corresponding object type. | |
159 | - * @param sql the SQL query to execute | |
160 | - * @param requiredType the type that the result object is expected to match | |
161 | - * @return the result object of the required type, or {@code null} in case of SQL NULL | |
162 | - * @throws IncorrectResultSizeDataAccessException if the query does not return | |
163 | - * exactly one row, or does not return exactly one column in that row | |
164 | - * @throws DataAccessException if there is any problem executing the query | |
165 | - * @see #queryForObject(String, Object[], Class) | |
166 | - */ | |
167 | - @Nullable | |
168 | - <T> T queryForObject(String sql, Class<T> requiredType, String[] broadcastTableNames) throws DataAccessException; | |
169 | - | |
170 | - /** | |
171 | - * Execute a query for a result map, given static SQL. | |
172 | - * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to | |
173 | - * execute a static query with a PreparedStatement, use the overloaded | |
174 | - * {@link #queryForMap(String, Object...)} method with {@code null} | |
175 | - * as argument array. | |
176 | - * <p>The query is expected to be a single row query; the result row will be | |
177 | - * mapped to a Map (one entry for each column, using the column name as the key). | |
178 | - * @param sql the SQL query to execute | |
179 | - * @return the result Map (one entry per column, with column name as key) | |
180 | - * @throws IncorrectResultSizeDataAccessException if the query does not | |
181 | - * return exactly one row | |
182 | - * @throws DataAccessException if there is any problem executing the query | |
183 | - * @see #queryForMap(String, Object[]) | |
184 | - * @see ColumnMapRowMapper | |
185 | - */ | |
186 | - Map<String, Object> queryForMap(String sql, String[] broadcastTableNames) throws DataAccessException; | |
187 | - | |
188 | - /** | |
189 | - * Execute a query for a result list, given static SQL. | |
190 | - * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to | |
191 | - * execute a static query with a PreparedStatement, use the overloaded | |
192 | - * {@code queryForList} method with {@code null} as argument array. | |
193 | - * <p>The results will be mapped to a List (one entry for each row) of | |
194 | - * result objects, each of them matching the specified element type. | |
195 | - * @param sql the SQL query to execute | |
196 | - * @param elementType the required type of element in the result list | |
197 | - * (for example, {@code Integer.class}) | |
198 | - * @return a List of objects that match the specified element type | |
199 | - * @throws DataAccessException if there is any problem executing the query | |
200 | - * @see #queryForList(String, Object[], Class) | |
201 | - * @see SingleColumnRowMapper | |
202 | - */ | |
203 | - <T> List<T> queryForList(String sql, Class<T> elementType, String[] broadcastTableNames) throws DataAccessException; | |
204 | - | |
205 | - /** | |
206 | - * Execute a query for a result list, given static SQL. | |
207 | - * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to | |
208 | - * execute a static query with a PreparedStatement, use the overloaded | |
209 | - * {@code queryForList} method with {@code null} as argument array. | |
210 | - * <p>The results will be mapped to a List (one entry for each row) of | |
211 | - * Maps (one entry for each column using the column name as the key). | |
212 | - * Each element in the list will be of the form returned by this interface's | |
213 | - * {@code queryForMap} methods. | |
214 | - * @param sql the SQL query to execute | |
215 | - * @return an List that contains a Map per row | |
216 | - * @throws DataAccessException if there is any problem executing the query | |
217 | - * @see #queryForList(String, Object[]) | |
218 | - */ | |
219 | - List<Map<String, Object>> queryForList(String sql, String[] broadcastTableNames) throws DataAccessException; | |
220 | - | |
221 | - /** | |
222 | - * Execute a query for a SqlRowSet, given static SQL. | |
223 | - * <p>Uses a JDBC Statement, not a PreparedStatement. If you want to | |
224 | - * execute a static query with a PreparedStatement, use the overloaded | |
225 | - * {@code queryForRowSet} method with {@code null} as argument array. | |
226 | - * <p>The results will be mapped to an SqlRowSet which holds the data in a | |
227 | - * disconnected fashion. This wrapper will translate any SQLExceptions thrown. | |
228 | - * <p>Note that, for the default implementation, JDBC RowSet support needs to | |
229 | - * be available at runtime: by default, Sun's {@code com.sun.rowset.CachedRowSetImpl} | |
230 | - * class is used, which is part of JDK 1.5+ and also available separately as part of | |
231 | - * Sun's JDBC RowSet Implementations download (rowset.jar). | |
232 | - * @param sql the SQL query to execute | |
233 | - * @return a SqlRowSet representation (possibly a wrapper around a | |
234 | - * {@code javax.sql.rowset.CachedRowSet}) | |
235 | - * @throws DataAccessException if there is any problem executing the query | |
236 | - * @see #queryForRowSet(String, Object[]) | |
237 | - * @see SqlRowSetResultSetExtractor | |
238 | - * @see javax.sql.rowset.CachedRowSet | |
239 | - */ | |
240 | - SqlRowSet queryForRowSet(String sql, String[] broadcastTableNames) throws DataAccessException; | |
241 | - | |
242 | - /** | |
243 | - * Issue a single SQL update operation (such as an insert, update or delete statement). | |
244 | - * @param sql static SQL to execute | |
245 | - * @return the number of rows affected | |
246 | - * @throws DataAccessException if there is any problem. | |
247 | - */ | |
248 | - int update(String sql, String[] broadcastTableNames) throws DataAccessException; | |
249 | - | |
250 | - /** | |
251 | - * Issue multiple SQL updates on a single JDBC Statement using batching. | |
252 | - * <p>Will fall back to separate updates on a single Statement if the JDBC | |
253 | - * driver does not support batch updates. | |
254 | - * @param sql defining an array of SQL statements that will be executed. | |
255 | - * @return an array of the number of rows affected by each statement | |
256 | - * @throws DataAccessException if there is any problem executing the batch | |
257 | - */ | |
258 | - int[] batchUpdate(String[] broadcastTableNames, String... sql) throws DataAccessException; | |
259 | - | |
260 | - | |
261 | - //------------------------------------------------------------------------- | |
262 | - // Methods dealing with prepared statements | |
263 | - //------------------------------------------------------------------------- | |
264 | - | |
265 | - /** | |
266 | - * Execute a JDBC data access operation, implemented as callback action | |
267 | - * working on a JDBC PreparedStatement. This allows for implementing arbitrary | |
268 | - * data access operations on a single Statement, within Spring's managed JDBC | |
269 | - * environment: that is, participating in Spring-managed transactions and | |
270 | - * converting JDBC SQLExceptions into Spring's DataAccessException hierarchy. | |
271 | - * <p>The callback action can return a result object, for example a domain | |
272 | - * object or a collection of domain objects. | |
273 | - * @param psc a callback that creates a PreparedStatement given a Connection | |
274 | - * @param action a callback that specifies the action | |
275 | - * @return a result object returned by the action, or {@code null} if none | |
276 | - * @throws DataAccessException if there is any problem | |
277 | - */ | |
278 | - @Nullable | |
279 | - <T> T execute(PreparedStatementCreator psc, PreparedStatementCallback<T> action, String[] broadcastTableNames) throws DataAccessException; | |
280 | - | |
281 | - /** | |
282 | - * Execute a JDBC data access operation, implemented as callback action | |
283 | - * working on a JDBC PreparedStatement. This allows for implementing arbitrary | |
284 | - * data access operations on a single Statement, within Spring's managed JDBC | |
285 | - * environment: that is, participating in Spring-managed transactions and | |
286 | - * converting JDBC SQLExceptions into Spring's DataAccessException hierarchy. | |
287 | - * <p>The callback action can return a result object, for example a domain | |
288 | - * object or a collection of domain objects. | |
289 | - * @param sql the SQL to execute | |
290 | - * @param action a callback that specifies the action | |
291 | - * @return a result object returned by the action, or {@code null} if none | |
292 | - * @throws DataAccessException if there is any problem | |
293 | - */ | |
294 | - @Nullable | |
295 | - <T> T execute(String sql, PreparedStatementCallback<T> action, String[] broadcastTableNames) throws DataAccessException; | |
296 | - | |
297 | - /** | |
298 | - * Query using a prepared statement, reading the ResultSet with a ResultSetExtractor. | |
299 | - * <p>A PreparedStatementCreator can either be implemented directly or | |
300 | - * configured through a PreparedStatementCreatorFactory. | |
301 | - * @param psc a callback that creates a PreparedStatement given a Connection | |
302 | - * @param rse a callback that will extract results | |
303 | - * @return an arbitrary result object, as returned by the ResultSetExtractor | |
304 | - * @throws DataAccessException if there is any problem | |
305 | - * @see PreparedStatementCreatorFactory | |
306 | - */ | |
307 | - @Nullable | |
308 | - <T> T query(PreparedStatementCreator psc, ResultSetExtractor<T> rse, String[] broadcastTableNames) throws DataAccessException; | |
309 | - | |
310 | - /** | |
311 | - * Query using a prepared statement, reading the ResultSet with a ResultSetExtractor. | |
312 | - * @param sql the SQL query to execute | |
313 | - * @param pss a callback that knows how to set values on the prepared statement. | |
314 | - * If this is {@code null}, the SQL will be assumed to contain no bind parameters. | |
315 | - * Even if there are no bind parameters, this callback may be used to set the | |
316 | - * fetch size and other performance options. | |
317 | - * @param rse a callback that will extract results | |
318 | - * @return an arbitrary result object, as returned by the ResultSetExtractor | |
319 | - * @throws DataAccessException if there is any problem | |
320 | - */ | |
321 | - @Nullable | |
322 | - <T> T query(String sql, @Nullable PreparedStatementSetter pss, ResultSetExtractor<T> rse, String[] broadcastTableNames) throws DataAccessException; | |
323 | - | |
324 | - /** | |
325 | - * Query given SQL to create a prepared statement from SQL and a list of arguments | |
326 | - * to bind to the query, reading the ResultSet with a ResultSetExtractor. | |
327 | - * @param sql the SQL query to execute | |
328 | - * @param args arguments to bind to the query | |
329 | - * @param argTypes the SQL types of the arguments | |
330 | - * (constants from {@code java.sql.Types}) | |
331 | - * @param rse a callback that will extract results | |
332 | - * @return an arbitrary result object, as returned by the ResultSetExtractor | |
333 | - * @throws DataAccessException if the query fails | |
334 | - * @see java.sql.Types | |
335 | - */ | |
336 | - @Nullable | |
337 | - <T> T query(String sql, Object[] args, int[] argTypes, ResultSetExtractor<T> rse, String[] broadcastTableNames) throws DataAccessException; | |
338 | - | |
339 | - /** | |
340 | - * Query given SQL to create a prepared statement from SQL and a list of arguments | |
341 | - * to bind to the query, reading the ResultSet with a ResultSetExtractor. | |
342 | - * @param sql the SQL query to execute | |
343 | - * @param args arguments to bind to the query | |
344 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
345 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
346 | - * only the argument value but also the SQL type and optionally the scale | |
347 | - * @param rse a callback that will extract results | |
348 | - * @return an arbitrary result object, as returned by the ResultSetExtractor | |
349 | - * @throws DataAccessException if the query fails | |
350 | - */ | |
351 | - @Nullable | |
352 | - <T> T query(String sql, Object[] args, ResultSetExtractor<T> rse, String[] broadcastTableNames) throws DataAccessException; | |
353 | - | |
354 | - /** | |
355 | - * Query given SQL to create a prepared statement from SQL and a list of arguments | |
356 | - * to bind to the query, reading the ResultSet with a ResultSetExtractor. | |
357 | - * @param sql the SQL query to execute | |
358 | - * @param rse a callback that will extract results | |
359 | - * @param args arguments to bind to the query | |
360 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
361 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
362 | - * only the argument value but also the SQL type and optionally the scale | |
363 | - * @return an arbitrary result object, as returned by the ResultSetExtractor | |
364 | - * @throws DataAccessException if the query fails | |
365 | - * @since 3.0.1 | |
366 | - */ | |
367 | - @Nullable | |
368 | - <T> T query(String sql, ResultSetExtractor<T> rse, String[] broadcastTableNames, @Nullable Object... args) throws DataAccessException; | |
369 | - | |
370 | - /** | |
371 | - * Query using a prepared statement, reading the ResultSet on a per-row basis | |
372 | - * with a RowCallbackHandler. | |
373 | - * <p>A PreparedStatementCreator can either be implemented directly or | |
374 | - * configured through a PreparedStatementCreatorFactory. | |
375 | - * @param psc a callback that creates a PreparedStatement given a Connection | |
376 | - * @param rch a callback that will extract results, one row at a time | |
377 | - * @throws DataAccessException if there is any problem | |
378 | - * @see PreparedStatementCreatorFactory | |
379 | - */ | |
380 | - void query(PreparedStatementCreator psc, RowCallbackHandler rch, String[] broadcastTableNames) throws DataAccessException; | |
381 | - | |
382 | - /** | |
383 | - * Query given SQL to create a prepared statement from SQL and a | |
384 | - * PreparedStatementSetter implementation that knows how to bind values to the | |
385 | - * query, reading the ResultSet on a per-row basis with a RowCallbackHandler. | |
386 | - * @param sql the SQL query to execute | |
387 | - * @param pss a callback that knows how to set values on the prepared statement. | |
388 | - * If this is {@code null}, the SQL will be assumed to contain no bind parameters. | |
389 | - * Even if there are no bind parameters, this callback may be used to set the | |
390 | - * fetch size and other performance options. | |
391 | - * @param rch a callback that will extract results, one row at a time | |
392 | - * @throws DataAccessException if the query fails | |
393 | - */ | |
394 | - void query(String sql, @Nullable PreparedStatementSetter pss, RowCallbackHandler rch, String[] broadcastTableNames) throws DataAccessException; | |
395 | - | |
396 | - /** | |
397 | - * Query given SQL to create a prepared statement from SQL and a list of | |
398 | - * arguments to bind to the query, reading the ResultSet on a per-row basis | |
399 | - * with a RowCallbackHandler. | |
400 | - * @param sql the SQL query to execute | |
401 | - * @param args arguments to bind to the query | |
402 | - * @param argTypes the SQL types of the arguments | |
403 | - * (constants from {@code java.sql.Types}) | |
404 | - * @param rch a callback that will extract results, one row at a time | |
405 | - * @throws DataAccessException if the query fails | |
406 | - * @see java.sql.Types | |
407 | - */ | |
408 | - void query(String sql, Object[] args, int[] argTypes, RowCallbackHandler rch, String[] broadcastTableNames) throws DataAccessException; | |
409 | - | |
410 | - /** | |
411 | - * Query given SQL to create a prepared statement from SQL and a list of | |
412 | - * arguments to bind to the query, reading the ResultSet on a per-row basis | |
413 | - * with a RowCallbackHandler. | |
414 | - * @param sql the SQL query to execute | |
415 | - * @param args arguments to bind to the query | |
416 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
417 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
418 | - * only the argument value but also the SQL type and optionally the scale | |
419 | - * @param rch a callback that will extract results, one row at a time | |
420 | - * @throws DataAccessException if the query fails | |
421 | - */ | |
422 | - void query(String sql, Object[] args, RowCallbackHandler rch, String[] broadcastTableNames) throws DataAccessException; | |
423 | - | |
424 | - /** | |
425 | - * Query given SQL to create a prepared statement from SQL and a list of | |
426 | - * arguments to bind to the query, reading the ResultSet on a per-row basis | |
427 | - * with a RowCallbackHandler. | |
428 | - * @param sql the SQL query to execute | |
429 | - * @param rch a callback that will extract results, one row at a time | |
430 | - * @param args arguments to bind to the query | |
431 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
432 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
433 | - * only the argument value but also the SQL type and optionally the scale | |
434 | - * @throws DataAccessException if the query fails | |
435 | - * @since 3.0.1 | |
436 | - */ | |
437 | - void query(String sql, RowCallbackHandler rch, String[] broadcastTableNames, @Nullable Object... args) throws DataAccessException; | |
438 | - | |
439 | - /** | |
440 | - * Query using a prepared statement, mapping each row to a result object | |
441 | - * via a RowMapper. | |
442 | - * <p>A PreparedStatementCreator can either be implemented directly or | |
443 | - * configured through a PreparedStatementCreatorFactory. | |
444 | - * @param psc a callback that creates a PreparedStatement given a Connection | |
445 | - * @param rowMapper a callback that will map one object per row | |
446 | - * @return the result List, containing mapped objects | |
447 | - * @throws DataAccessException if there is any problem | |
448 | - * @see PreparedStatementCreatorFactory | |
449 | - */ | |
450 | - <T> List<T> query(PreparedStatementCreator psc, RowMapper<T> rowMapper, String[] broadcastTableNames) throws DataAccessException; | |
451 | - | |
452 | - /** | |
453 | - * Query given SQL to create a prepared statement from SQL and a | |
454 | - * PreparedStatementSetter implementation that knows how to bind values | |
455 | - * to the query, mapping each row to a result object via a RowMapper. | |
456 | - * @param sql the SQL query to execute | |
457 | - * @param pss a callback that knows how to set values on the prepared statement. | |
458 | - * If this is {@code null}, the SQL will be assumed to contain no bind parameters. | |
459 | - * Even if there are no bind parameters, this callback may be used to set the | |
460 | - * fetch size and other performance options. | |
461 | - * @param rowMapper a callback that will map one object per row | |
462 | - * @return the result List, containing mapped objects | |
463 | - * @throws DataAccessException if the query fails | |
464 | - */ | |
465 | - <T> List<T> query(String sql, @Nullable PreparedStatementSetter pss, RowMapper<T> rowMapper, String[] broadcastTableNames) throws DataAccessException; | |
466 | - | |
467 | - /** | |
468 | - * Query given SQL to create a prepared statement from SQL and a list of | |
469 | - * arguments to bind to the query, mapping each row to a result object | |
470 | - * via a RowMapper. | |
471 | - * @param sql the SQL query to execute | |
472 | - * @param args arguments to bind to the query | |
473 | - * @param argTypes the SQL types of the arguments | |
474 | - * (constants from {@code java.sql.Types}) | |
475 | - * @param rowMapper a callback that will map one object per row | |
476 | - * @return the result List, containing mapped objects | |
477 | - * @throws DataAccessException if the query fails | |
478 | - * @see java.sql.Types | |
479 | - */ | |
480 | - <T> List<T> query(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper, String[] broadcastTableNames) throws DataAccessException; | |
481 | - | |
482 | - /** | |
483 | - * Query given SQL to create a prepared statement from SQL and a list of | |
484 | - * arguments to bind to the query, mapping each row to a result object | |
485 | - * via a RowMapper. | |
486 | - * @param sql the SQL query to execute | |
487 | - * @param args arguments to bind to the query | |
488 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
489 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
490 | - * only the argument value but also the SQL type and optionally the scale | |
491 | - * @param rowMapper a callback that will map one object per row | |
492 | - * @return the result List, containing mapped objects | |
493 | - * @throws DataAccessException if the query fails | |
494 | - */ | |
495 | - <T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper, String[] broadcastTableNames) throws DataAccessException; | |
496 | - | |
497 | - /** | |
498 | - * Query given SQL to create a prepared statement from SQL and a list of | |
499 | - * arguments to bind to the query, mapping each row to a result object | |
500 | - * via a RowMapper. | |
501 | - * @param sql the SQL query to execute | |
502 | - * @param rowMapper a callback that will map one object per row | |
503 | - * @param args arguments to bind to the query | |
504 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
505 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
506 | - * only the argument value but also the SQL type and optionally the scale | |
507 | - * @return the result List, containing mapped objects | |
508 | - * @throws DataAccessException if the query fails | |
509 | - * @since 3.0.1 | |
510 | - */ | |
511 | - <T> List<T> query(String sql, RowMapper<T> rowMapper, String[] broadcastTableNames, @Nullable Object... args) throws DataAccessException; | |
512 | - | |
513 | - /** | |
514 | - * Query given SQL to create a prepared statement from SQL and a list | |
515 | - * of arguments to bind to the query, mapping a single result row to a | |
516 | - * result object via a RowMapper. | |
517 | - * @param sql the SQL query to execute | |
518 | - * @param args arguments to bind to the query | |
519 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type) | |
520 | - * @param argTypes the SQL types of the arguments | |
521 | - * (constants from {@code java.sql.Types}) | |
522 | - * @param rowMapper a callback that will map one object per row | |
523 | - * @return the single mapped object (may be {@code null} if the given | |
524 | - * {@link RowMapper} returned {@code} null) | |
525 | - * @throws IncorrectResultSizeDataAccessException if the query does not | |
526 | - * return exactly one row | |
527 | - * @throws DataAccessException if the query fails | |
528 | - */ | |
529 | - @Nullable | |
530 | - <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper, String[] broadcastTableNames) | |
531 | - throws DataAccessException; | |
532 | - | |
533 | - /** | |
534 | - * Query given SQL to create a prepared statement from SQL and a list | |
535 | - * of arguments to bind to the query, mapping a single result row to a | |
536 | - * result object via a RowMapper. | |
537 | - * @param sql the SQL query to execute | |
538 | - * @param args arguments to bind to the query | |
539 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
540 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
541 | - * only the argument value but also the SQL type and optionally the scale | |
542 | - * @param rowMapper a callback that will map one object per row | |
543 | - * @return the single mapped object (may be {@code null} if the given | |
544 | - * {@link RowMapper} returned {@code} null) | |
545 | - * @throws IncorrectResultSizeDataAccessException if the query does not | |
546 | - * return exactly one row | |
547 | - * @throws DataAccessException if the query fails | |
548 | - */ | |
549 | - @Nullable | |
550 | - <T> T queryForObject(String sql, Object[] args, RowMapper<T> rowMapper, String[] broadcastTableNames) throws DataAccessException; | |
551 | - | |
552 | - /** | |
553 | - * Query given SQL to create a prepared statement from SQL and a list | |
554 | - * of arguments to bind to the query, mapping a single result row to a | |
555 | - * result object via a RowMapper. | |
556 | - * @param sql the SQL query to execute | |
557 | - * @param rowMapper a callback that will map one object per row | |
558 | - * @param args arguments to bind to the query | |
559 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
560 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
561 | - * only the argument value but also the SQL type and optionally the scale | |
562 | - * @return the single mapped object (may be {@code null} if the given | |
563 | - * {@link RowMapper} returned {@code} null) | |
564 | - * @throws IncorrectResultSizeDataAccessException if the query does not | |
565 | - * return exactly one row | |
566 | - * @throws DataAccessException if the query fails | |
567 | - * @since 3.0.1 | |
568 | - */ | |
569 | - @Nullable | |
570 | - <T> T queryForObject(String sql, RowMapper<T> rowMapper, String[] broadcastTableNames, @Nullable Object... args) throws DataAccessException; | |
571 | - | |
572 | - /** | |
573 | - * Query given SQL to create a prepared statement from SQL and a list of | |
574 | - * arguments to bind to the query, expecting a result object. | |
575 | - * <p>The query is expected to be a single row/single column query; the returned | |
576 | - * result will be directly mapped to the corresponding object type. | |
577 | - * @param sql the SQL query to execute | |
578 | - * @param args arguments to bind to the query | |
579 | - * @param argTypes the SQL types of the arguments | |
580 | - * (constants from {@code java.sql.Types}) | |
581 | - * @param requiredType the type that the result object is expected to match | |
582 | - * @return the result object of the required type, or {@code null} in case of SQL NULL | |
583 | - * @throws IncorrectResultSizeDataAccessException if the query does not return | |
584 | - * exactly one row, or does not return exactly one column in that row | |
585 | - * @throws DataAccessException if the query fails | |
586 | - * @see #queryForObject(String, Class) | |
587 | - * @see java.sql.Types | |
588 | - */ | |
589 | - @Nullable | |
590 | - <T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T> requiredType, String[] broadcastTableNames) | |
591 | - throws DataAccessException; | |
592 | - | |
593 | - /** | |
594 | - * Query given SQL to create a prepared statement from SQL and a list of | |
595 | - * arguments to bind to the query, expecting a result object. | |
596 | - * <p>The query is expected to be a single row/single column query; the returned | |
597 | - * result will be directly mapped to the corresponding object type. | |
598 | - * @param sql the SQL query to execute | |
599 | - * @param args arguments to bind to the query | |
600 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
601 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
602 | - * only the argument value but also the SQL type and optionally the scale | |
603 | - * @param requiredType the type that the result object is expected to match | |
604 | - * @return the result object of the required type, or {@code null} in case of SQL NULL | |
605 | - * @throws IncorrectResultSizeDataAccessException if the query does not return | |
606 | - * exactly one row, or does not return exactly one column in that row | |
607 | - * @throws DataAccessException if the query fails | |
608 | - * @see #queryForObject(String, Class) | |
609 | - */ | |
610 | - @Nullable | |
611 | - <T> T queryForObject(String sql, Object[] args, Class<T> requiredType, String[] broadcastTableNames) throws DataAccessException; | |
612 | - | |
613 | - /** | |
614 | - * Query given SQL to create a prepared statement from SQL and a list of | |
615 | - * arguments to bind to the query, expecting a result object. | |
616 | - * <p>The query is expected to be a single row/single column query; the returned | |
617 | - * result will be directly mapped to the corresponding object type. | |
618 | - * @param sql the SQL query to execute | |
619 | - * @param requiredType the type that the result object is expected to match | |
620 | - * @param args arguments to bind to the query | |
621 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
622 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
623 | - * only the argument value but also the SQL type and optionally the scale | |
624 | - * @return the result object of the required type, or {@code null} in case of SQL NULL | |
625 | - * @throws IncorrectResultSizeDataAccessException if the query does not return | |
626 | - * exactly one row, or does not return exactly one column in that row | |
627 | - * @throws DataAccessException if the query fails | |
628 | - * @since 3.0.1 | |
629 | - * @see #queryForObject(String, Class) | |
630 | - */ | |
631 | - @Nullable | |
632 | - <T> T queryForObject(String sql, Class<T> requiredType, String[] broadcastTableNames, @Nullable Object... args) throws DataAccessException; | |
633 | - | |
634 | - /** | |
635 | - * Query given SQL to create a prepared statement from SQL and a list of | |
636 | - * arguments to bind to the query, expecting a result map. | |
637 | - * <p>The query is expected to be a single row query; the result row will be | |
638 | - * mapped to a Map (one entry for each column, using the column name as the key). | |
639 | - * @param sql the SQL query to execute | |
640 | - * @param args arguments to bind to the query | |
641 | - * @param argTypes the SQL types of the arguments | |
642 | - * (constants from {@code java.sql.Types}) | |
643 | - * @return the result Map (one entry per column, with column name as key) | |
644 | - * @throws IncorrectResultSizeDataAccessException if the query does not | |
645 | - * return exactly one row | |
646 | - * @throws DataAccessException if the query fails | |
647 | - * @see #queryForMap(String) | |
648 | - * @see ColumnMapRowMapper | |
649 | - * @see java.sql.Types | |
650 | - */ | |
651 | - Map<String, Object> queryForMap(String sql, Object[] args, int[] argTypes, String[] broadcastTableNames) throws DataAccessException; | |
652 | - | |
653 | - /** | |
654 | - * Query given SQL to create a prepared statement from SQL and a list of | |
655 | - * arguments to bind to the query, expecting a result map. | |
656 | - * <p>The {@code queryForMap} methods defined by this interface are appropriate | |
657 | - * when you don't have a domain model. Otherwise, consider using one of the | |
658 | - * {@code queryForObject} methods. | |
659 | - * <p>The query is expected to be a single row query; the result row will be | |
660 | - * mapped to a Map (one entry for each column, using the column name as the key). | |
661 | - * @param sql the SQL query to execute | |
662 | - * @param args arguments to bind to the query | |
663 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
664 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
665 | - * only the argument value but also the SQL type and optionally the scale | |
666 | - * @return the result Map (one entry for each column, using the | |
667 | - * column name as the key) | |
668 | - * @throws IncorrectResultSizeDataAccessException if the query does not | |
669 | - * return exactly one row | |
670 | - * @throws DataAccessException if the query fails | |
671 | - * @see #queryForMap(String) | |
672 | - * @see ColumnMapRowMapper | |
673 | - */ | |
674 | - Map<String, Object> queryForMap(String sql, String[] broadcastTableNames, @Nullable Object... args) throws DataAccessException; | |
675 | - | |
676 | - /** | |
677 | - * Query given SQL to create a prepared statement from SQL and a list of | |
678 | - * arguments to bind to the query, expecting a result list. | |
679 | - * <p>The results will be mapped to a List (one entry for each row) of | |
680 | - * result objects, each of them matching the specified element type. | |
681 | - * @param sql the SQL query to execute | |
682 | - * @param args arguments to bind to the query | |
683 | - * @param argTypes the SQL types of the arguments | |
684 | - * (constants from {@code java.sql.Types}) | |
685 | - * @param elementType the required type of element in the result list | |
686 | - * (for example, {@code Integer.class}) | |
687 | - * @return a List of objects that match the specified element type | |
688 | - * @throws DataAccessException if the query fails | |
689 | - * @see #queryForList(String, Class) | |
690 | - * @see SingleColumnRowMapper | |
691 | - */ | |
692 | - <T> List<T> queryForList(String sql, Object[] args, int[] argTypes, Class<T> elementType, String[] broadcastTableNames) | |
693 | - throws DataAccessException; | |
694 | - | |
695 | - /** | |
696 | - * Query given SQL to create a prepared statement from SQL and a list of | |
697 | - * arguments to bind to the query, expecting a result list. | |
698 | - * <p>The results will be mapped to a List (one entry for each row) of | |
699 | - * result objects, each of them matching the specified element type. | |
700 | - * @param sql the SQL query to execute | |
701 | - * @param args arguments to bind to the query | |
702 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
703 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
704 | - * only the argument value but also the SQL type and optionally the scale | |
705 | - * @param elementType the required type of element in the result list | |
706 | - * (for example, {@code Integer.class}) | |
707 | - * @return a List of objects that match the specified element type | |
708 | - * @throws DataAccessException if the query fails | |
709 | - * @see #queryForList(String, Class) | |
710 | - * @see SingleColumnRowMapper | |
711 | - */ | |
712 | - <T> List<T> queryForList(String sql, Object[] args, Class<T> elementType, String[] broadcastTableNames) throws DataAccessException; | |
713 | - | |
714 | - /** | |
715 | - * Query given SQL to create a prepared statement from SQL and a list of | |
716 | - * arguments to bind to the query, expecting a result list. | |
717 | - * <p>The results will be mapped to a List (one entry for each row) of | |
718 | - * result objects, each of them matching the specified element type. | |
719 | - * @param sql the SQL query to execute | |
720 | - * @param elementType the required type of element in the result list | |
721 | - * (for example, {@code Integer.class}) | |
722 | - * @param args arguments to bind to the query | |
723 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
724 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
725 | - * only the argument value but also the SQL type and optionally the scale | |
726 | - * @return a List of objects that match the specified element type | |
727 | - * @throws DataAccessException if the query fails | |
728 | - * @since 3.0.1 | |
729 | - * @see #queryForList(String, Class) | |
730 | - * @see SingleColumnRowMapper | |
731 | - */ | |
732 | - <T> List<T> queryForList(String sql, Class<T> elementType, String[] broadcastTableNames, @Nullable Object... args) throws DataAccessException; | |
733 | - | |
734 | - /** | |
735 | - * Query given SQL to create a prepared statement from SQL and a list of | |
736 | - * arguments to bind to the query, expecting a result list. | |
737 | - * <p>The results will be mapped to a List (one entry for each row) of | |
738 | - * Maps (one entry for each column, using the column name as the key). | |
739 | - * Each element in the list will be of the form returned by this interface's | |
740 | - * {@code queryForMap} methods. | |
741 | - * @param sql the SQL query to execute | |
742 | - * @param args arguments to bind to the query | |
743 | - * @param argTypes the SQL types of the arguments | |
744 | - * (constants from {@code java.sql.Types}) | |
745 | - * @return a List that contains a Map per row | |
746 | - * @throws DataAccessException if the query fails | |
747 | - * @see #queryForList(String) | |
748 | - * @see java.sql.Types | |
749 | - */ | |
750 | - List<Map<String, Object>> queryForList(String sql, Object[] args, int[] argTypes, String[] broadcastTableNames) throws DataAccessException; | |
751 | - | |
752 | - /** | |
753 | - * Query given SQL to create a prepared statement from SQL and a list of | |
754 | - * arguments to bind to the query, expecting a result list. | |
755 | - * <p>The results will be mapped to a List (one entry for each row) of | |
756 | - * Maps (one entry for each column, using the column name as the key). | |
757 | - * Each element in the list will be of the form returned by this interface's | |
758 | - * {@code queryForMap} methods. | |
759 | - * @param sql the SQL query to execute | |
760 | - * @param args arguments to bind to the query | |
761 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
762 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
763 | - * only the argument value but also the SQL type and optionally the scale | |
764 | - * @return a List that contains a Map per row | |
765 | - * @throws DataAccessException if the query fails | |
766 | - * @see #queryForList(String) | |
767 | - */ | |
768 | - List<Map<String, Object>> queryForList(String sql, String[] broadcastTableNames, @Nullable Object... args) throws DataAccessException; | |
769 | - | |
770 | - /** | |
771 | - * Query given SQL to create a prepared statement from SQL and a list of | |
772 | - * arguments to bind to the query, expecting a SqlRowSet. | |
773 | - * <p>The results will be mapped to an SqlRowSet which holds the data in a | |
774 | - * disconnected fashion. This wrapper will translate any SQLExceptions thrown. | |
775 | - * <p>Note that, for the default implementation, JDBC RowSet support needs to | |
776 | - * be available at runtime: by default, Sun's {@code com.sun.rowset.CachedRowSetImpl} | |
777 | - * class is used, which is part of JDK 1.5+ and also available separately as part of | |
778 | - * Sun's JDBC RowSet Implementations download (rowset.jar). | |
779 | - * @param sql the SQL query to execute | |
780 | - * @param args arguments to bind to the query | |
781 | - * @param argTypes the SQL types of the arguments | |
782 | - * (constants from {@code java.sql.Types}) | |
783 | - * @return a SqlRowSet representation (possibly a wrapper around a | |
784 | - * {@code javax.sql.rowset.CachedRowSet}) | |
785 | - * @throws DataAccessException if there is any problem executing the query | |
786 | - * @see #queryForRowSet(String) | |
787 | - * @see SqlRowSetResultSetExtractor | |
788 | - * @see javax.sql.rowset.CachedRowSet | |
789 | - * @see java.sql.Types | |
790 | - */ | |
791 | - SqlRowSet queryForRowSet(String sql, Object[] args, int[] argTypes, String[] broadcastTableNames) throws DataAccessException; | |
792 | - | |
793 | - /** | |
794 | - * Query given SQL to create a prepared statement from SQL and a list of | |
795 | - * arguments to bind to the query, expecting a SqlRowSet. | |
796 | - * <p>The results will be mapped to an SqlRowSet which holds the data in a | |
797 | - * disconnected fashion. This wrapper will translate any SQLExceptions thrown. | |
798 | - * <p>Note that, for the default implementation, JDBC RowSet support needs to | |
799 | - * be available at runtime: by default, Sun's {@code com.sun.rowset.CachedRowSetImpl} | |
800 | - * class is used, which is part of JDK 1.5+ and also available separately as part of | |
801 | - * Sun's JDBC RowSet Implementations download (rowset.jar). | |
802 | - * @param sql the SQL query to execute | |
803 | - * @param args arguments to bind to the query | |
804 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
805 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
806 | - * only the argument value but also the SQL type and optionally the scale | |
807 | - * @return a SqlRowSet representation (possibly a wrapper around a | |
808 | - * {@code javax.sql.rowset.CachedRowSet}) | |
809 | - * @throws DataAccessException if there is any problem executing the query | |
810 | - * @see #queryForRowSet(String) | |
811 | - * @see SqlRowSetResultSetExtractor | |
812 | - * @see javax.sql.rowset.CachedRowSet | |
813 | - */ | |
814 | - SqlRowSet queryForRowSet(String sql, String[] broadcastTableNames, @Nullable Object... args) throws DataAccessException; | |
815 | - | |
816 | - /** | |
817 | - * Issue a single SQL update operation (such as an insert, update or delete | |
818 | - * statement) using a PreparedStatementCreator to provide SQL and any | |
819 | - * required parameters. | |
820 | - * <p>A PreparedStatementCreator can either be implemented directly or | |
821 | - * configured through a PreparedStatementCreatorFactory. | |
822 | - * @param psc a callback that provides SQL and any necessary parameters | |
823 | - * @return the number of rows affected | |
824 | - * @throws DataAccessException if there is any problem issuing the update | |
825 | - * @see PreparedStatementCreatorFactory | |
826 | - */ | |
827 | - int update(PreparedStatementCreator psc, String[] broadcastTableNames) throws DataAccessException; | |
828 | - | |
829 | - /** | |
830 | - * Issue an update statement using a PreparedStatementCreator to provide SQL and | |
831 | - * any required parameters. Generated keys will be put into the given KeyHolder. | |
832 | - * <p>Note that the given PreparedStatementCreator has to create a statement | |
833 | - * with activated extraction of generated keys (a JDBC 3.0 feature). This can | |
834 | - * either be done directly or through using a PreparedStatementCreatorFactory. | |
835 | - * @param psc a callback that provides SQL and any necessary parameters | |
836 | - * @param generatedKeyHolder a KeyHolder that will hold the generated keys | |
837 | - * @return the number of rows affected | |
838 | - * @throws DataAccessException if there is any problem issuing the update | |
839 | - * @see PreparedStatementCreatorFactory | |
840 | - * @see org.springframework.jdbc.support.GeneratedKeyHolder | |
841 | - */ | |
842 | - int update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder, String[] broadcastTableNames) throws DataAccessException; | |
843 | - | |
844 | - /** | |
845 | - * Issue an update statement using a PreparedStatementSetter to set bind parameters, | |
846 | - * with given SQL. Simpler than using a PreparedStatementCreator as this method | |
847 | - * will create the PreparedStatement: The PreparedStatementSetter just needs to | |
848 | - * set parameters. | |
849 | - * @param sql the SQL containing bind parameters | |
850 | - * @param pss helper that sets bind parameters. If this is {@code null} | |
851 | - * we run an update with static SQL. | |
852 | - * @return the number of rows affected | |
853 | - * @throws DataAccessException if there is any problem issuing the update | |
854 | - */ | |
855 | - int update(String sql, @Nullable PreparedStatementSetter pss, String[] broadcastTableNames) throws DataAccessException; | |
856 | - | |
857 | - /** | |
858 | - * Issue a single SQL update operation (such as an insert, update or delete statement) | |
859 | - * via a prepared statement, binding the given arguments. | |
860 | - * @param sql the SQL containing bind parameters | |
861 | - * @param args arguments to bind to the query | |
862 | - * @param argTypes the SQL types of the arguments | |
863 | - * (constants from {@code java.sql.Types}) | |
864 | - * @return the number of rows affected | |
865 | - * @throws DataAccessException if there is any problem issuing the update | |
866 | - * @see java.sql.Types | |
867 | - */ | |
868 | - int update(String sql, Object[] args, int[] argTypes, String[] broadcastTableNames) throws DataAccessException; | |
869 | - | |
870 | - /** | |
871 | - * Issue a single SQL update operation (such as an insert, update or delete statement) | |
872 | - * via a prepared statement, binding the given arguments. | |
873 | - * @param sql the SQL containing bind parameters | |
874 | - * @param args arguments to bind to the query | |
875 | - * (leaving it to the PreparedStatement to guess the corresponding SQL type); | |
876 | - * may also contain {@link SqlParameterValue} objects which indicate not | |
877 | - * only the argument value but also the SQL type and optionally the scale | |
878 | - * @return the number of rows affected | |
879 | - * @throws DataAccessException if there is any problem issuing the update | |
880 | - */ | |
881 | - int update(String sql, String[] broadcastTableNames, @Nullable Object... args) throws DataAccessException; | |
882 | - | |
883 | - /** | |
884 | - * Issue multiple update statements on a single PreparedStatement, | |
885 | - * using batch updates and a BatchPreparedStatementSetter to set values. | |
886 | - * <p>Will fall back to separate updates on a single PreparedStatement | |
887 | - * if the JDBC driver does not support batch updates. | |
888 | - * @param sql defining PreparedStatement that will be reused. | |
889 | - * All statements in the batch will use the same SQL. | |
890 | - * @param pss object to set parameters on the PreparedStatement | |
891 | - * created by this method | |
892 | - * @return an array of the number of rows affected by each statement | |
893 | - * @throws DataAccessException if there is any problem issuing the update | |
894 | - */ | |
895 | - int[] batchUpdate(String sql, BatchPreparedStatementSetter pss, String[] broadcastTableNames) throws DataAccessException; | |
896 | - | |
897 | - /** | |
898 | - * Execute a batch using the supplied SQL statement with the batch of supplied arguments. | |
899 | - * @param sql the SQL statement to execute | |
900 | - * @param batchArgs the List of Object arrays containing the batch of arguments for the query | |
901 | - * @return an array containing the numbers of rows affected by each update in the batch | |
902 | - */ | |
903 | - int[] batchUpdate(String sql, List<Object[]> batchArgs, String[] broadcastTableNames) throws DataAccessException; | |
904 | - | |
905 | - /** | |
906 | - * Execute a batch using the supplied SQL statement with the batch of supplied arguments. | |
907 | - * @param sql the SQL statement to execute. | |
908 | - * @param batchArgs the List of Object arrays containing the batch of arguments for the query | |
909 | - * @param argTypes the SQL types of the arguments | |
910 | - * (constants from {@code java.sql.Types}) | |
911 | - * @return an array containing the numbers of rows affected by each update in the batch | |
912 | - */ | |
913 | - int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes, String[] broadcastTableNames) throws DataAccessException; | |
914 | - | |
915 | - /** | |
916 | - * Execute multiple batches using the supplied SQL statement with the collect of supplied arguments. | |
917 | - * The arguments' values will be set using the ParameterizedPreparedStatementSetter. | |
918 | - * Each batch should be of size indicated in 'batchSize'. | |
919 | - * @param sql the SQL statement to execute. | |
920 | - * @param batchArgs the List of Object arrays containing the batch of arguments for the query | |
921 | - * @param batchSize batch size | |
922 | - * @param pss the ParameterizedPreparedStatementSetter to use | |
923 | - * @return an array containing for each batch another array containing the numbers of rows affected | |
924 | - * by each update in the batch | |
925 | - * @since 3.1 | |
926 | - */ | |
927 | - <T> int[][] batchUpdate(String sql, Collection<T> batchArgs, int batchSize, | |
928 | - ParameterizedPreparedStatementSetter<T> pss, String[] broadcastTableNames) throws DataAccessException; | |
929 | - | |
930 | - | |
931 | - //------------------------------------------------------------------------- | |
932 | - // Methods dealing with callable statements | |
933 | - //------------------------------------------------------------------------- | |
934 | - | |
935 | - /** | |
936 | - * Execute a JDBC data access operation, implemented as callback action | |
937 | - * working on a JDBC CallableStatement. This allows for implementing arbitrary | |
938 | - * data access operations on a single Statement, within Spring's managed JDBC | |
939 | - * environment: that is, participating in Spring-managed transactions and | |
940 | - * converting JDBC SQLExceptions into Spring's DataAccessException hierarchy. | |
941 | - * <p>The callback action can return a result object, for example a domain | |
942 | - * object or a collection of domain objects. | |
943 | - * @param csc a callback that creates a CallableStatement given a Connection | |
944 | - * @param action a callback that specifies the action | |
945 | - * @return a result object returned by the action, or {@code null} if none | |
946 | - * @throws DataAccessException if there is any problem | |
947 | - */ | |
948 | - @Nullable | |
949 | - <T> T execute(CallableStatementCreator csc, CallableStatementCallback<T> action, String[] broadcastTableNames) throws DataAccessException; | |
950 | - | |
951 | - /** | |
952 | - * Execute a JDBC data access operation, implemented as callback action | |
953 | - * working on a JDBC CallableStatement. This allows for implementing arbitrary | |
954 | - * data access operations on a single Statement, within Spring's managed JDBC | |
955 | - * environment: that is, participating in Spring-managed transactions and | |
956 | - * converting JDBC SQLExceptions into Spring's DataAccessException hierarchy. | |
957 | - * <p>The callback action can return a result object, for example a domain | |
958 | - * object or a collection of domain objects. | |
959 | - * @param callString the SQL call string to execute | |
960 | - * @param action a callback that specifies the action | |
961 | - * @return a result object returned by the action, or {@code null} if none | |
962 | - * @throws DataAccessException if there is any problem | |
963 | - */ | |
964 | - @Nullable | |
965 | - <T> T execute(String callString, CallableStatementCallback<T> action, String[] broadcastTableNames) throws DataAccessException; | |
966 | - | |
967 | - /** | |
968 | - * Execute a SQL call using a CallableStatementCreator to provide SQL and | |
969 | - * any required parameters. | |
970 | - * @param csc a callback that provides SQL and any necessary parameters | |
971 | - * @param declaredParameters list of declared SqlParameter objects | |
972 | - * @return a Map of extracted out parameters | |
973 | - * @throws DataAccessException if there is any problem issuing the update | |
974 | - */ | |
975 | - Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters, String[] broadcastTableNames) throws DataAccessException; | |
976 | -} |
src/main/java/com/taover/repository/jdbctemplate/JdbcTemplateBroadcastImpl.java
... | ... | @@ -1,667 +0,0 @@ |
1 | -package com.taover.repository.jdbctemplate; | |
2 | - | |
3 | -import java.util.Collection; | |
4 | -import java.util.List; | |
5 | -import java.util.Map; | |
6 | - | |
7 | -import org.apache.shardingsphere.infra.hint.HintManager; | |
8 | -import org.springframework.dao.DataAccessException; | |
9 | -import org.springframework.jdbc.core.BatchPreparedStatementSetter; | |
10 | -import org.springframework.jdbc.core.CallableStatementCallback; | |
11 | -import org.springframework.jdbc.core.CallableStatementCreator; | |
12 | -import org.springframework.jdbc.core.ConnectionCallback; | |
13 | -import org.springframework.jdbc.core.JdbcTemplate; | |
14 | -import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter; | |
15 | -import org.springframework.jdbc.core.PreparedStatementCallback; | |
16 | -import org.springframework.jdbc.core.PreparedStatementCreator; | |
17 | -import org.springframework.jdbc.core.PreparedStatementSetter; | |
18 | -import org.springframework.jdbc.core.ResultSetExtractor; | |
19 | -import org.springframework.jdbc.core.RowCallbackHandler; | |
20 | -import org.springframework.jdbc.core.RowMapper; | |
21 | -import org.springframework.jdbc.core.SqlParameter; | |
22 | -import org.springframework.jdbc.core.StatementCallback; | |
23 | -import org.springframework.jdbc.support.KeyHolder; | |
24 | -import org.springframework.jdbc.support.rowset.SqlRowSet; | |
25 | - | |
26 | -import com.taover.repository.shardingsphere.ShardingInfoEntity; | |
27 | -import com.taover.repository.shardingsphere.ShardingSphereService; | |
28 | - | |
29 | -public class JdbcTemplateBroadcastImpl implements JdbcTemplateBroadcast { | |
30 | - private JdbcTemplate jdbcTemplate; | |
31 | - private ShardingSphereService shardingSphereService; | |
32 | - | |
33 | - public JdbcTemplateBroadcastImpl(JdbcTemplate jdbcTemplate, ShardingSphereService shardingSphereService) { | |
34 | - this.jdbcTemplate = jdbcTemplate; | |
35 | - this.shardingSphereService = shardingSphereService; | |
36 | - } | |
37 | - | |
38 | - @Override | |
39 | - public void loadShardingInfo(String[] broadcastTableNames) { | |
40 | - List<ShardingInfoEntity> shardingInfo = this.shardingSphereService.getShardingInfoByTableNames(broadcastTableNames); | |
41 | - if(shardingInfo == null || shardingInfo.isEmpty()) { | |
42 | - return; | |
43 | - } | |
44 | - HintManager.clear(); | |
45 | - HintManager instance = HintManager.getInstance(); | |
46 | - for(ShardingInfoEntity item: shardingInfo) { | |
47 | - instance.addTableShardingValue(item.getTableName(), item.getTableSuffix()); | |
48 | - instance.addDatabaseShardingValue(item.getTableName(), item.getDsName()); | |
49 | - } | |
50 | - } | |
51 | - | |
52 | - @Override | |
53 | - public void clearShardingInfo() { | |
54 | - HintManager.clear(); | |
55 | - } | |
56 | - | |
57 | - @Override | |
58 | - public <E> E execute(ConnectionCallback<E> action, String[] broadcastTableNames) throws DataAccessException { | |
59 | - try { | |
60 | - this.loadShardingInfo(broadcastTableNames); | |
61 | - return this.jdbcTemplate.execute(action); | |
62 | - }finally { | |
63 | - this.clearShardingInfo(); | |
64 | - } | |
65 | - } | |
66 | - | |
67 | - @Override | |
68 | - public <E> E execute(StatementCallback<E> action, String[] broadcastTableNames) throws DataAccessException { | |
69 | - try { | |
70 | - this.loadShardingInfo(broadcastTableNames); | |
71 | - return this.jdbcTemplate.execute(action); | |
72 | - }finally { | |
73 | - this.clearShardingInfo(); | |
74 | - } | |
75 | - } | |
76 | - | |
77 | - @Override | |
78 | - public void execute(String sql, String[] broadcastTableNames) throws DataAccessException { | |
79 | - try { | |
80 | - this.loadShardingInfo(broadcastTableNames); | |
81 | - this.jdbcTemplate.execute(sql); | |
82 | - }finally { | |
83 | - this.clearShardingInfo(); | |
84 | - } | |
85 | - } | |
86 | - | |
87 | - @Override | |
88 | - public <E> E query(String sql, ResultSetExtractor<E> rse, String[] broadcastTableNames) throws DataAccessException { | |
89 | - try { | |
90 | - this.loadShardingInfo(broadcastTableNames); | |
91 | - return this.jdbcTemplate.query(sql, rse); | |
92 | - }finally { | |
93 | - this.clearShardingInfo(); | |
94 | - } | |
95 | - } | |
96 | - | |
97 | - @Override | |
98 | - public void query(String sql, RowCallbackHandler rch, String[] broadcastTableNames) throws DataAccessException { | |
99 | - try { | |
100 | - this.loadShardingInfo(broadcastTableNames); | |
101 | - this.jdbcTemplate.query(sql, rch); | |
102 | - }finally { | |
103 | - this.clearShardingInfo(); | |
104 | - } | |
105 | - } | |
106 | - | |
107 | - @Override | |
108 | - public <E> List<E> query(String sql, RowMapper<E> rowMapper, String[] broadcastTableNames) throws DataAccessException { | |
109 | - try { | |
110 | - this.loadShardingInfo(broadcastTableNames); | |
111 | - return this.jdbcTemplate.query(sql, rowMapper); | |
112 | - }finally { | |
113 | - this.clearShardingInfo(); | |
114 | - } | |
115 | - } | |
116 | - | |
117 | - @Override | |
118 | - public <E> E queryForObject(String sql, RowMapper<E> rowMapper, String[] broadcastTableNames) throws DataAccessException { | |
119 | - try { | |
120 | - this.loadShardingInfo(broadcastTableNames); | |
121 | - return this.jdbcTemplate.queryForObject(sql, rowMapper); | |
122 | - }finally { | |
123 | - this.clearShardingInfo(); | |
124 | - } | |
125 | - } | |
126 | - | |
127 | - @Override | |
128 | - public <E> E queryForObject(String sql, Class<E> requiredType, String[] broadcastTableNames) throws DataAccessException { | |
129 | - try { | |
130 | - this.loadShardingInfo(broadcastTableNames); | |
131 | - return this.jdbcTemplate.queryForObject(sql, requiredType); | |
132 | - }finally { | |
133 | - this.clearShardingInfo(); | |
134 | - } | |
135 | - } | |
136 | - | |
137 | - @Override | |
138 | - public Map<String, Object> queryForMap(String sql, String[] broadcastTableNames) throws DataAccessException { | |
139 | - try { | |
140 | - this.loadShardingInfo(broadcastTableNames); | |
141 | - return this.jdbcTemplate.queryForMap(sql); | |
142 | - }finally { | |
143 | - this.clearShardingInfo(); | |
144 | - } | |
145 | - } | |
146 | - | |
147 | - @Override | |
148 | - public <E> List<E> queryForList(String sql, Class<E> elementType, String[] broadcastTableNames) throws DataAccessException { | |
149 | - try { | |
150 | - this.loadShardingInfo(broadcastTableNames); | |
151 | - return this.jdbcTemplate.queryForList(sql, elementType); | |
152 | - }finally { | |
153 | - this.clearShardingInfo(); | |
154 | - } | |
155 | - } | |
156 | - | |
157 | - @Override | |
158 | - public List<Map<String, Object>> queryForList(String sql, String[] broadcastTableNames) throws DataAccessException { | |
159 | - try { | |
160 | - this.loadShardingInfo(broadcastTableNames); | |
161 | - return this.jdbcTemplate.queryForList(sql); | |
162 | - }finally { | |
163 | - this.clearShardingInfo(); | |
164 | - } | |
165 | - } | |
166 | - | |
167 | - @Override | |
168 | - public SqlRowSet queryForRowSet(String sql, String[] broadcastTableNames) throws DataAccessException { | |
169 | - try { | |
170 | - this.loadShardingInfo(broadcastTableNames); | |
171 | - return this.jdbcTemplate.queryForRowSet(sql); | |
172 | - }finally { | |
173 | - this.clearShardingInfo(); | |
174 | - } | |
175 | - } | |
176 | - | |
177 | - @Override | |
178 | - public int update(String sql, String[] broadcastTableNames) throws DataAccessException { | |
179 | - try { | |
180 | - this.loadShardingInfo(broadcastTableNames); | |
181 | - return this.jdbcTemplate.update(sql); | |
182 | - }finally { | |
183 | - this.clearShardingInfo(); | |
184 | - } | |
185 | - } | |
186 | - | |
187 | - @Override | |
188 | - public int[] batchUpdate(String[] broadcastTableNames, String... sql) throws DataAccessException { | |
189 | - try { | |
190 | - this.loadShardingInfo(broadcastTableNames); | |
191 | - return this.jdbcTemplate.batchUpdate(sql); | |
192 | - }finally { | |
193 | - this.clearShardingInfo(); | |
194 | - } | |
195 | - } | |
196 | - | |
197 | - @Override | |
198 | - public <E> E execute(PreparedStatementCreator psc, PreparedStatementCallback<E> action, String[] broadcastTableNames) | |
199 | - throws DataAccessException { | |
200 | - try { | |
201 | - this.loadShardingInfo(broadcastTableNames); | |
202 | - return this.jdbcTemplate.execute(psc, action); | |
203 | - }finally { | |
204 | - this.clearShardingInfo(); | |
205 | - } | |
206 | - } | |
207 | - | |
208 | - @Override | |
209 | - public <E> E execute(String sql, PreparedStatementCallback<E> action, String[] broadcastTableNames) throws DataAccessException { | |
210 | - try { | |
211 | - this.loadShardingInfo(broadcastTableNames); | |
212 | - return this.jdbcTemplate.execute(sql, action); | |
213 | - }finally { | |
214 | - this.clearShardingInfo(); | |
215 | - } | |
216 | - } | |
217 | - | |
218 | - @Override | |
219 | - public <E> E query(PreparedStatementCreator psc, ResultSetExtractor<E> rse, String[] broadcastTableNames) | |
220 | - throws DataAccessException { | |
221 | - try { | |
222 | - this.loadShardingInfo(broadcastTableNames); | |
223 | - return this.jdbcTemplate.query(psc, rse); | |
224 | - }finally { | |
225 | - this.clearShardingInfo(); | |
226 | - } | |
227 | - } | |
228 | - | |
229 | - @Override | |
230 | - public <E> E query(String sql, PreparedStatementSetter pss, ResultSetExtractor<E> rse, String[] broadcastTableNames) | |
231 | - throws DataAccessException { | |
232 | - try { | |
233 | - this.loadShardingInfo(broadcastTableNames); | |
234 | - return this.jdbcTemplate.query(sql, pss, rse); | |
235 | - }finally { | |
236 | - this.clearShardingInfo(); | |
237 | - } | |
238 | - } | |
239 | - | |
240 | - @Override | |
241 | - public <E> E query(String sql, Object[] args, int[] argTypes, ResultSetExtractor<E> rse, String[] broadcastTableNames) | |
242 | - throws DataAccessException { | |
243 | - try { | |
244 | - this.loadShardingInfo(broadcastTableNames); | |
245 | - return this.jdbcTemplate.query(sql, args, argTypes, rse); | |
246 | - }finally { | |
247 | - this.clearShardingInfo(); | |
248 | - } | |
249 | - } | |
250 | - | |
251 | - @Override | |
252 | - public <E> E query(String sql, Object[] args, ResultSetExtractor<E> rse, String[] broadcastTableNames) throws DataAccessException { | |
253 | - try { | |
254 | - this.loadShardingInfo(broadcastTableNames); | |
255 | - return this.jdbcTemplate.query(sql, args, rse); | |
256 | - }finally { | |
257 | - this.clearShardingInfo(); | |
258 | - } | |
259 | - } | |
260 | - | |
261 | - @Override | |
262 | - public <E> E query(String sql, ResultSetExtractor<E> rse, String[] broadcastTableNames, Object... args) | |
263 | - throws DataAccessException { | |
264 | - try { | |
265 | - this.loadShardingInfo(broadcastTableNames); | |
266 | - return this.jdbcTemplate.query(sql, rse, args); | |
267 | - }finally { | |
268 | - this.clearShardingInfo(); | |
269 | - } | |
270 | - } | |
271 | - | |
272 | - @Override | |
273 | - public void query(PreparedStatementCreator psc, RowCallbackHandler rch, String[] broadcastTableNames) throws DataAccessException { | |
274 | - try { | |
275 | - this.loadShardingInfo(broadcastTableNames); | |
276 | - this.jdbcTemplate.query(psc, rch); | |
277 | - }finally { | |
278 | - this.clearShardingInfo(); | |
279 | - } | |
280 | - } | |
281 | - | |
282 | - @Override | |
283 | - public void query(String sql, PreparedStatementSetter pss, RowCallbackHandler rch, String[] broadcastTableNames) | |
284 | - throws DataAccessException { | |
285 | - try { | |
286 | - this.loadShardingInfo(broadcastTableNames); | |
287 | - this.jdbcTemplate.query(sql, pss, rch); | |
288 | - }finally { | |
289 | - this.clearShardingInfo(); | |
290 | - } | |
291 | - } | |
292 | - | |
293 | - @Override | |
294 | - public void query(String sql, Object[] args, int[] argTypes, RowCallbackHandler rch, String[] broadcastTableNames) | |
295 | - throws DataAccessException { | |
296 | - try { | |
297 | - this.loadShardingInfo(broadcastTableNames); | |
298 | - this.jdbcTemplate.query(sql, args, argTypes, rch); | |
299 | - }finally { | |
300 | - this.clearShardingInfo(); | |
301 | - } | |
302 | - } | |
303 | - | |
304 | - @Override | |
305 | - public void query(String sql, Object[] args, RowCallbackHandler rch, String[] broadcastTableNames) throws DataAccessException { | |
306 | - try { | |
307 | - this.loadShardingInfo(broadcastTableNames); | |
308 | - this.jdbcTemplate.query(sql, args, rch); | |
309 | - }finally { | |
310 | - this.clearShardingInfo(); | |
311 | - } | |
312 | - } | |
313 | - | |
314 | - @Override | |
315 | - public void query(String sql, RowCallbackHandler rch, String[] broadcastTableNames, Object... args) throws DataAccessException { | |
316 | - try { | |
317 | - this.loadShardingInfo(broadcastTableNames); | |
318 | - this.jdbcTemplate.query(sql, rch, args); | |
319 | - }finally { | |
320 | - this.clearShardingInfo(); | |
321 | - } | |
322 | - } | |
323 | - | |
324 | - @Override | |
325 | - public <E> List<E> query(PreparedStatementCreator psc, RowMapper<E> rowMapper, String[] broadcastTableNames) | |
326 | - throws DataAccessException { | |
327 | - try { | |
328 | - this.loadShardingInfo(broadcastTableNames); | |
329 | - return this.jdbcTemplate.query(psc, rowMapper); | |
330 | - }finally { | |
331 | - this.clearShardingInfo(); | |
332 | - } | |
333 | - } | |
334 | - | |
335 | - @Override | |
336 | - public <E> List<E> query(String sql, PreparedStatementSetter pss, RowMapper<E> rowMapper, String[] broadcastTableNames) | |
337 | - throws DataAccessException { | |
338 | - try { | |
339 | - this.loadShardingInfo(broadcastTableNames); | |
340 | - return this.jdbcTemplate.query(sql, pss, rowMapper); | |
341 | - }finally { | |
342 | - this.clearShardingInfo(); | |
343 | - } | |
344 | - } | |
345 | - | |
346 | - @Override | |
347 | - public <E> List<E> query(String sql, Object[] args, int[] argTypes, RowMapper<E> rowMapper, String[] broadcastTableNames) | |
348 | - throws DataAccessException { | |
349 | - try { | |
350 | - this.loadShardingInfo(broadcastTableNames); | |
351 | - return this.jdbcTemplate.query(sql, args, argTypes, rowMapper); | |
352 | - }finally { | |
353 | - this.clearShardingInfo(); | |
354 | - } | |
355 | - } | |
356 | - | |
357 | - @Override | |
358 | - public <E> List<E> query(String sql, Object[] args, RowMapper<E> rowMapper, String[] broadcastTableNames) | |
359 | - throws DataAccessException { | |
360 | - try { | |
361 | - this.loadShardingInfo(broadcastTableNames); | |
362 | - return this.jdbcTemplate.query(sql, args, rowMapper); | |
363 | - }finally { | |
364 | - this.clearShardingInfo(); | |
365 | - } | |
366 | - } | |
367 | - | |
368 | - @Override | |
369 | - public <E> List<E> query(String sql, RowMapper<E> rowMapper, String[] broadcastTableNames, Object... args) | |
370 | - throws DataAccessException { | |
371 | - try { | |
372 | - this.loadShardingInfo(broadcastTableNames); | |
373 | - return this.jdbcTemplate.query(sql, rowMapper, args); | |
374 | - }finally { | |
375 | - this.clearShardingInfo(); | |
376 | - } | |
377 | - } | |
378 | - | |
379 | - @Override | |
380 | - public <E> E queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<E> rowMapper, String[] broadcastTableNames) | |
381 | - throws DataAccessException { | |
382 | - try { | |
383 | - this.loadShardingInfo(broadcastTableNames); | |
384 | - return this.jdbcTemplate.queryForObject(sql, args, argTypes, rowMapper); | |
385 | - }finally { | |
386 | - this.clearShardingInfo(); | |
387 | - } | |
388 | - } | |
389 | - | |
390 | - @Override | |
391 | - public <E> E queryForObject(String sql, Object[] args, RowMapper<E> rowMapper, String[] broadcastTableNames) | |
392 | - throws DataAccessException { | |
393 | - try { | |
394 | - this.loadShardingInfo(broadcastTableNames); | |
395 | - return this.jdbcTemplate.queryForObject(sql, args, rowMapper); | |
396 | - }finally { | |
397 | - this.clearShardingInfo(); | |
398 | - } | |
399 | - } | |
400 | - | |
401 | - @Override | |
402 | - public <E> E queryForObject(String sql, RowMapper<E> rowMapper, String[] broadcastTableNames, Object... args) | |
403 | - throws DataAccessException { | |
404 | - try { | |
405 | - this.loadShardingInfo(broadcastTableNames); | |
406 | - return this.jdbcTemplate.queryForObject(sql, rowMapper, args); | |
407 | - }finally { | |
408 | - this.clearShardingInfo(); | |
409 | - } | |
410 | - } | |
411 | - | |
412 | - @Override | |
413 | - public <E> E queryForObject(String sql, Object[] args, int[] argTypes, Class<E> requiredType, String[] broadcastTableNames) | |
414 | - throws DataAccessException { | |
415 | - try { | |
416 | - this.loadShardingInfo(broadcastTableNames); | |
417 | - return this.jdbcTemplate.queryForObject(sql, args, argTypes, requiredType); | |
418 | - }finally { | |
419 | - this.clearShardingInfo(); | |
420 | - } | |
421 | - } | |
422 | - | |
423 | - @Override | |
424 | - public <E> E queryForObject(String sql, Object[] args, Class<E> requiredType, String[] broadcastTableNames) | |
425 | - throws DataAccessException { | |
426 | - try { | |
427 | - this.loadShardingInfo(broadcastTableNames); | |
428 | - return this.jdbcTemplate.queryForObject(sql, args, requiredType); | |
429 | - }finally { | |
430 | - this.clearShardingInfo(); | |
431 | - } | |
432 | - } | |
433 | - | |
434 | - @Override | |
435 | - public <E> E queryForObject(String sql, Class<E> requiredType, String[] broadcastTableNames, Object... args) | |
436 | - throws DataAccessException { | |
437 | - try { | |
438 | - this.loadShardingInfo(broadcastTableNames); | |
439 | - return this.jdbcTemplate.queryForObject(sql, requiredType, args); | |
440 | - }finally { | |
441 | - this.clearShardingInfo(); | |
442 | - } | |
443 | - } | |
444 | - | |
445 | - @Override | |
446 | - public Map<String, Object> queryForMap(String sql, Object[] args, int[] argTypes, String[] broadcastTableNames) | |
447 | - throws DataAccessException { | |
448 | - try { | |
449 | - this.loadShardingInfo(broadcastTableNames); | |
450 | - return this.jdbcTemplate.queryForMap(sql, args, argTypes); | |
451 | - }finally { | |
452 | - this.clearShardingInfo(); | |
453 | - } | |
454 | - } | |
455 | - | |
456 | - @Override | |
457 | - public Map<String, Object> queryForMap(String sql, String[] broadcastTableNames, Object... args) throws DataAccessException { | |
458 | - try { | |
459 | - this.loadShardingInfo(broadcastTableNames); | |
460 | - return this.jdbcTemplate.queryForMap(sql, args); | |
461 | - }finally { | |
462 | - this.clearShardingInfo(); | |
463 | - } | |
464 | - } | |
465 | - | |
466 | - @Override | |
467 | - public <E> List<E> queryForList(String sql, Object[] args, int[] argTypes, Class<E> elementType, String[] broadcastTableNames) | |
468 | - throws DataAccessException { | |
469 | - try { | |
470 | - this.loadShardingInfo(broadcastTableNames); | |
471 | - return this.jdbcTemplate.queryForList(sql, args, argTypes, elementType); | |
472 | - }finally { | |
473 | - this.clearShardingInfo(); | |
474 | - } | |
475 | - } | |
476 | - | |
477 | - @Override | |
478 | - public <E> List<E> queryForList(String sql, Object[] args, Class<E> elementType, String[] broadcastTableNames) | |
479 | - throws DataAccessException { | |
480 | - try { | |
481 | - this.loadShardingInfo(broadcastTableNames); | |
482 | - return this.jdbcTemplate.queryForList(sql, args, elementType); | |
483 | - }finally { | |
484 | - this.clearShardingInfo(); | |
485 | - } | |
486 | - } | |
487 | - | |
488 | - @Override | |
489 | - public <E> List<E> queryForList(String sql, Class<E> elementType, String[] broadcastTableNames, Object... args) | |
490 | - throws DataAccessException { | |
491 | - try { | |
492 | - this.loadShardingInfo(broadcastTableNames); | |
493 | - return this.jdbcTemplate.queryForList(sql, elementType, args); | |
494 | - }finally { | |
495 | - this.clearShardingInfo(); | |
496 | - } | |
497 | - } | |
498 | - | |
499 | - @Override | |
500 | - public List<Map<String, Object>> queryForList(String sql, Object[] args, int[] argTypes, String[] broadcastTableNames) | |
501 | - throws DataAccessException { | |
502 | - try { | |
503 | - this.loadShardingInfo(broadcastTableNames); | |
504 | - return this.jdbcTemplate.queryForList(sql, args, argTypes); | |
505 | - }finally { | |
506 | - this.clearShardingInfo(); | |
507 | - } | |
508 | - } | |
509 | - | |
510 | - @Override | |
511 | - public List<Map<String, Object>> queryForList(String sql, String[] broadcastTableNames, Object... args) | |
512 | - throws DataAccessException { | |
513 | - try { | |
514 | - this.loadShardingInfo(broadcastTableNames); | |
515 | - return this.jdbcTemplate.queryForList(sql, args); | |
516 | - }finally { | |
517 | - this.clearShardingInfo(); | |
518 | - } | |
519 | - } | |
520 | - | |
521 | - @Override | |
522 | - public SqlRowSet queryForRowSet(String sql, Object[] args, int[] argTypes, String[] broadcastTableNames) | |
523 | - throws DataAccessException { | |
524 | - try { | |
525 | - this.loadShardingInfo(broadcastTableNames); | |
526 | - return this.jdbcTemplate.queryForRowSet(sql, args, argTypes); | |
527 | - }finally { | |
528 | - this.clearShardingInfo(); | |
529 | - } | |
530 | - } | |
531 | - | |
532 | - @Override | |
533 | - public SqlRowSet queryForRowSet(String sql, String[] broadcastTableNames, Object... args) throws DataAccessException { | |
534 | - try { | |
535 | - this.loadShardingInfo(broadcastTableNames); | |
536 | - return this.jdbcTemplate.queryForRowSet(sql, args); | |
537 | - }finally { | |
538 | - this.clearShardingInfo(); | |
539 | - } | |
540 | - } | |
541 | - | |
542 | - @Override | |
543 | - public int update(PreparedStatementCreator psc, String[] broadcastTableNames) throws DataAccessException { | |
544 | - try { | |
545 | - this.loadShardingInfo(broadcastTableNames); | |
546 | - return this.jdbcTemplate.update(psc); | |
547 | - }finally { | |
548 | - this.clearShardingInfo(); | |
549 | - } | |
550 | - } | |
551 | - | |
552 | - @Override | |
553 | - public int update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder, String[] broadcastTableNames) | |
554 | - throws DataAccessException { | |
555 | - try { | |
556 | - this.loadShardingInfo(broadcastTableNames); | |
557 | - return this.jdbcTemplate.update(psc, generatedKeyHolder); | |
558 | - }finally { | |
559 | - this.clearShardingInfo(); | |
560 | - } | |
561 | - } | |
562 | - | |
563 | - @Override | |
564 | - public int update(String sql, PreparedStatementSetter pss, String[] broadcastTableNames) throws DataAccessException { | |
565 | - try { | |
566 | - this.loadShardingInfo(broadcastTableNames); | |
567 | - return this.jdbcTemplate.update(sql, pss); | |
568 | - }finally { | |
569 | - this.clearShardingInfo(); | |
570 | - } | |
571 | - } | |
572 | - | |
573 | - @Override | |
574 | - public int update(String sql, Object[] args, int[] argTypes, String[] broadcastTableNames) throws DataAccessException { | |
575 | - try { | |
576 | - this.loadShardingInfo(broadcastTableNames); | |
577 | - return this.jdbcTemplate.update(sql, args, argTypes); | |
578 | - }finally { | |
579 | - this.clearShardingInfo(); | |
580 | - } | |
581 | - } | |
582 | - | |
583 | - @Override | |
584 | - public int update(String sql, String[] broadcastTableNames, Object... args) throws DataAccessException { | |
585 | - try { | |
586 | - this.loadShardingInfo(broadcastTableNames); | |
587 | - return this.jdbcTemplate.update(sql, args); | |
588 | - }finally { | |
589 | - this.clearShardingInfo(); | |
590 | - } | |
591 | - } | |
592 | - | |
593 | - @Override | |
594 | - public int[] batchUpdate(String sql, BatchPreparedStatementSetter pss, String[] broadcastTableNames) throws DataAccessException { | |
595 | - try { | |
596 | - this.loadShardingInfo(broadcastTableNames); | |
597 | - return this.jdbcTemplate.batchUpdate(sql, pss); | |
598 | - }finally { | |
599 | - this.clearShardingInfo(); | |
600 | - } | |
601 | - } | |
602 | - | |
603 | - @Override | |
604 | - public int[] batchUpdate(String sql, List<Object[]> batchArgs, String[] broadcastTableNames) throws DataAccessException { | |
605 | - try { | |
606 | - this.loadShardingInfo(broadcastTableNames); | |
607 | - return this.jdbcTemplate.batchUpdate(sql, batchArgs); | |
608 | - }finally { | |
609 | - this.clearShardingInfo(); | |
610 | - } | |
611 | - } | |
612 | - | |
613 | - @Override | |
614 | - public int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes, String[] broadcastTableNames) | |
615 | - throws DataAccessException { | |
616 | - try { | |
617 | - this.loadShardingInfo(broadcastTableNames); | |
618 | - return this.jdbcTemplate.batchUpdate(sql, batchArgs); | |
619 | - }finally { | |
620 | - this.clearShardingInfo(); | |
621 | - } | |
622 | - } | |
623 | - | |
624 | - @Override | |
625 | - public <E> int[][] batchUpdate(String sql, Collection<E> batchArgs, int batchSize, | |
626 | - ParameterizedPreparedStatementSetter<E> pss, String[] broadcastTableNames) throws DataAccessException { | |
627 | - try { | |
628 | - this.loadShardingInfo(broadcastTableNames); | |
629 | - return this.jdbcTemplate.batchUpdate(sql, batchArgs, batchSize, pss); | |
630 | - }finally { | |
631 | - this.clearShardingInfo(); | |
632 | - } | |
633 | - } | |
634 | - | |
635 | - @Override | |
636 | - public <E> E execute(CallableStatementCreator csc, CallableStatementCallback<E> action, String[] broadcastTableNames) | |
637 | - throws DataAccessException { | |
638 | - try { | |
639 | - this.loadShardingInfo(broadcastTableNames); | |
640 | - return this.jdbcTemplate.execute(csc, action); | |
641 | - }finally { | |
642 | - this.clearShardingInfo(); | |
643 | - } | |
644 | - } | |
645 | - | |
646 | - @Override | |
647 | - public <E> E execute(String callString, CallableStatementCallback<E> action, String[] broadcastTableNames) | |
648 | - throws DataAccessException { | |
649 | - try { | |
650 | - this.loadShardingInfo(broadcastTableNames); | |
651 | - return this.jdbcTemplate.execute(callString, action); | |
652 | - }finally { | |
653 | - this.clearShardingInfo(); | |
654 | - } | |
655 | - } | |
656 | - | |
657 | - @Override | |
658 | - public Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters, String[] broadcastTableNames) | |
659 | - throws DataAccessException { | |
660 | - try { | |
661 | - this.loadShardingInfo(broadcastTableNames); | |
662 | - return this.jdbcTemplate.call(csc, declaredParameters); | |
663 | - }finally { | |
664 | - this.clearShardingInfo(); | |
665 | - } | |
666 | - } | |
667 | -} |
src/main/java/com/taover/repository/jdbctemplate/JdbcTemplateWrapperTenant.java
1 | 1 | package com.taover.repository.jdbctemplate; |
2 | 2 | |
3 | +import java.io.Serializable; | |
3 | 4 | import java.util.Collection; |
4 | 5 | import java.util.List; |
5 | 6 | import java.util.Map; |
... | ... | @@ -7,84 +8,43 @@ import java.util.Map; |
7 | 8 | import org.springframework.dao.DataAccessException; |
8 | 9 | import org.springframework.dao.IncorrectResultSizeDataAccessException; |
9 | 10 | import org.springframework.jdbc.core.BatchPreparedStatementSetter; |
10 | -import org.springframework.jdbc.core.CallableStatementCallback; | |
11 | -import org.springframework.jdbc.core.CallableStatementCreator; | |
12 | 11 | import org.springframework.jdbc.core.ColumnMapRowMapper; |
13 | -import org.springframework.jdbc.core.ConnectionCallback; | |
12 | +import org.springframework.jdbc.core.JdbcTemplate; | |
14 | 13 | import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter; |
15 | 14 | import org.springframework.jdbc.core.PreparedStatementCallback; |
16 | -import org.springframework.jdbc.core.PreparedStatementCreator; | |
17 | -import org.springframework.jdbc.core.PreparedStatementCreatorFactory; | |
18 | 15 | import org.springframework.jdbc.core.PreparedStatementSetter; |
19 | 16 | import org.springframework.jdbc.core.ResultSetExtractor; |
20 | 17 | import org.springframework.jdbc.core.RowCallbackHandler; |
21 | 18 | import org.springframework.jdbc.core.RowMapper; |
22 | 19 | import org.springframework.jdbc.core.SingleColumnRowMapper; |
23 | -import org.springframework.jdbc.core.SqlParameter; | |
24 | 20 | import org.springframework.jdbc.core.SqlParameterValue; |
25 | 21 | import org.springframework.jdbc.core.SqlRowSetResultSetExtractor; |
26 | -import org.springframework.jdbc.core.StatementCallback; | |
27 | -import org.springframework.jdbc.support.KeyHolder; | |
28 | 22 | import org.springframework.jdbc.support.rowset.SqlRowSet; |
29 | 23 | import org.springframework.lang.Nullable; |
30 | 24 | |
31 | -public interface JdbcTemplateWrapperTenant { | |
25 | +import com.taover.repository.exception.NoContainTenantException; | |
26 | + | |
27 | +public interface JdbcTemplateWrapperTenant<ID extends Serializable> { | |
32 | 28 | /** |
33 | - * 加载分片信息 | |
34 | - * @param tenantId | |
29 | + * 获取JdbcTemplate | |
30 | + * @return | |
35 | 31 | */ |
36 | - public void loadShardingInfo(Long tenantId); | |
32 | + JdbcTemplate getJdbcTemplate(); | |
37 | 33 | |
38 | 34 | /** |
39 | - * 清空分片信息 | |
35 | + * 检查租户ID | |
36 | + * @param sql | |
37 | + * @param tenantId | |
38 | + * @throws NoContainTenantException | |
40 | 39 | */ |
41 | - public void clearShardingInfo(); | |
40 | + void doCheckTenantId(String sql, ID tenantId) throws NoContainTenantException; | |
42 | 41 | |
43 | - //------------------------------------------------------------------------- | |
44 | - // Methods dealing with a plain java.sql.Connection | |
45 | - //------------------------------------------------------------------------- | |
46 | - | |
47 | - /** | |
48 | - * Execute a JDBC data access operation, implemented as callback action | |
49 | - * working on a JDBC Connection. This allows for implementing arbitrary | |
50 | - * data access operations, within Spring's managed JDBC environment: | |
51 | - * that is, participating in Spring-managed transactions and converting | |
52 | - * JDBC SQLExceptions into Spring's DataAccessException hierarchy. | |
53 | - * <p>The callback action can return a result object, for example a domain | |
54 | - * object or a collection of domain objects. | |
55 | - * @param action a callback object that specifies the action | |
56 | - * @return a result object returned by the action, or {@code null} if none | |
57 | - * @throws DataAccessException if there is any problem | |
58 | - */ | |
59 | - @Nullable | |
60 | - <T> T execute(ConnectionCallback<T> action, Long tenantId) throws DataAccessException; | |
61 | - | |
62 | - | |
63 | - //------------------------------------------------------------------------- | |
64 | - // Methods dealing with static SQL (java.sql.Statement) | |
65 | - //------------------------------------------------------------------------- | |
66 | - | |
67 | - /** | |
68 | - * Execute a JDBC data access operation, implemented as callback action | |
69 | - * working on a JDBC Statement. This allows for implementing arbitrary data | |
70 | - * access operations on a single Statement, within Spring's managed JDBC | |
71 | - * environment: that is, participating in Spring-managed transactions and | |
72 | - * converting JDBC SQLExceptions into Spring's DataAccessException hierarchy. | |
73 | - * <p>The callback action can return a result object, for example a domain | |
74 | - * object or a collection of domain objects. | |
75 | - * @param action a callback that specifies the action | |
76 | - * @return a result object returned by the action, or {@code null} if none | |
77 | - * @throws DataAccessException if there is any problem | |
78 | - */ | |
79 | - @Nullable | |
80 | - <T> T execute(StatementCallback<T> action, Long tenantId) throws DataAccessException; | |
81 | - | |
82 | 42 | /** |
83 | 43 | * Issue a single SQL execute, typically a DDL statement. |
84 | 44 | * @param sql static SQL to execute |
85 | 45 | * @throws DataAccessException if there is any problem |
86 | 46 | */ |
87 | - void execute(String sql, Long tenantId) throws DataAccessException; | |
47 | + void execute(String sql, ID tenantId) throws DataAccessException; | |
88 | 48 | |
89 | 49 | /** |
90 | 50 | * Execute a query given static SQL, reading the ResultSet with a |
... | ... | @@ -99,7 +59,7 @@ public interface JdbcTemplateWrapperTenant { |
99 | 59 | * @see #query(String, Object[], ResultSetExtractor) |
100 | 60 | */ |
101 | 61 | @Nullable |
102 | - <T> T query(String sql, ResultSetExtractor<T> rse, Long tenantId) throws DataAccessException; | |
62 | + <T> T query(String sql, ResultSetExtractor<T> rse, ID tenantId) throws DataAccessException; | |
103 | 63 | |
104 | 64 | /** |
105 | 65 | * Execute a query given static SQL, reading the ResultSet on a per-row |
... | ... | @@ -112,7 +72,7 @@ public interface JdbcTemplateWrapperTenant { |
112 | 72 | * @throws DataAccessException if there is any problem executing the query |
113 | 73 | * @see #query(String, Object[], RowCallbackHandler) |
114 | 74 | */ |
115 | - void query(String sql, RowCallbackHandler rch, Long tenantId) throws DataAccessException; | |
75 | + void query(String sql, RowCallbackHandler rch, ID tenantId) throws DataAccessException; | |
116 | 76 | |
117 | 77 | /** |
118 | 78 | * Execute a query given static SQL, mapping each row to a result object |
... | ... | @@ -126,7 +86,7 @@ public interface JdbcTemplateWrapperTenant { |
126 | 86 | * @throws DataAccessException if there is any problem executing the query |
127 | 87 | * @see #query(String, Object[], RowMapper) |
128 | 88 | */ |
129 | - <T> List<T> query(String sql, RowMapper<T> rowMapper, Long tenantId) throws DataAccessException; | |
89 | + <T> List<T> query(String sql, RowMapper<T> rowMapper, ID tenantId) throws DataAccessException; | |
130 | 90 | |
131 | 91 | /** |
132 | 92 | * Execute a query given static SQL, mapping a single result row to a |
... | ... | @@ -145,7 +105,7 @@ public interface JdbcTemplateWrapperTenant { |
145 | 105 | * @see #queryForObject(String, Object[], RowMapper) |
146 | 106 | */ |
147 | 107 | @Nullable |
148 | - <T> T queryForObject(String sql, RowMapper<T> rowMapper, Long tenantId) throws DataAccessException; | |
108 | + <T> T queryForObject(String sql, RowMapper<T> rowMapper, ID tenantId) throws DataAccessException; | |
149 | 109 | |
150 | 110 | /** |
151 | 111 | * Execute a query for a result object, given static SQL. |
... | ... | @@ -165,7 +125,7 @@ public interface JdbcTemplateWrapperTenant { |
165 | 125 | * @see #queryForObject(String, Object[], Class) |
166 | 126 | */ |
167 | 127 | @Nullable |
168 | - <T> T queryForObject(String sql, Class<T> requiredType, Long tenantId) throws DataAccessException; | |
128 | + <T> T queryForObject(String sql, Class<T> requiredType, ID tenantId) throws DataAccessException; | |
169 | 129 | |
170 | 130 | /** |
171 | 131 | * Execute a query for a result map, given static SQL. |
... | ... | @@ -183,7 +143,7 @@ public interface JdbcTemplateWrapperTenant { |
183 | 143 | * @see #queryForMap(String, Object[]) |
184 | 144 | * @see ColumnMapRowMapper |
185 | 145 | */ |
186 | - Map<String, Object> queryForMap(String sql, Long tenantId) throws DataAccessException; | |
146 | + Map<String, Object> queryForMap(String sql, ID tenantId) throws DataAccessException; | |
187 | 147 | |
188 | 148 | /** |
189 | 149 | * Execute a query for a result list, given static SQL. |
... | ... | @@ -200,7 +160,7 @@ public interface JdbcTemplateWrapperTenant { |
200 | 160 | * @see #queryForList(String, Object[], Class) |
201 | 161 | * @see SingleColumnRowMapper |
202 | 162 | */ |
203 | - <T> List<T> queryForList(String sql, Class<T> elementType, Long tenantId) throws DataAccessException; | |
163 | + <T> List<T> queryForList(String sql, Class<T> elementType, ID tenantId) throws DataAccessException; | |
204 | 164 | |
205 | 165 | /** |
206 | 166 | * Execute a query for a result list, given static SQL. |
... | ... | @@ -216,7 +176,7 @@ public interface JdbcTemplateWrapperTenant { |
216 | 176 | * @throws DataAccessException if there is any problem executing the query |
217 | 177 | * @see #queryForList(String, Object[]) |
218 | 178 | */ |
219 | - List<Map<String, Object>> queryForList(String sql, Long tenantId) throws DataAccessException; | |
179 | + List<Map<String, Object>> queryForList(String sql, ID tenantId) throws DataAccessException; | |
220 | 180 | |
221 | 181 | /** |
222 | 182 | * Execute a query for a SqlRowSet, given static SQL. |
... | ... | @@ -237,7 +197,7 @@ public interface JdbcTemplateWrapperTenant { |
237 | 197 | * @see SqlRowSetResultSetExtractor |
238 | 198 | * @see javax.sql.rowset.CachedRowSet |
239 | 199 | */ |
240 | - SqlRowSet queryForRowSet(String sql, Long tenantId) throws DataAccessException; | |
200 | + SqlRowSet queryForRowSet(String sql, ID tenantId) throws DataAccessException; | |
241 | 201 | |
242 | 202 | /** |
243 | 203 | * Issue a single SQL update operation (such as an insert, update or delete statement). |
... | ... | @@ -245,7 +205,7 @@ public interface JdbcTemplateWrapperTenant { |
245 | 205 | * @return the number of rows affected |
246 | 206 | * @throws DataAccessException if there is any problem. |
247 | 207 | */ |
248 | - int update(String sql, Long tenantId) throws DataAccessException; | |
208 | + int update(String sql, ID tenantId) throws DataAccessException; | |
249 | 209 | |
250 | 210 | /** |
251 | 211 | * Issue multiple SQL updates on a single JDBC Statement using batching. |
... | ... | @@ -255,28 +215,7 @@ public interface JdbcTemplateWrapperTenant { |
255 | 215 | * @return an array of the number of rows affected by each statement |
256 | 216 | * @throws DataAccessException if there is any problem executing the batch |
257 | 217 | */ |
258 | - int[] batchUpdate(Long tenantId, String... sql) throws DataAccessException; | |
259 | - | |
260 | - | |
261 | - //------------------------------------------------------------------------- | |
262 | - // Methods dealing with prepared statements | |
263 | - //------------------------------------------------------------------------- | |
264 | - | |
265 | - /** | |
266 | - * Execute a JDBC data access operation, implemented as callback action | |
267 | - * working on a JDBC PreparedStatement. This allows for implementing arbitrary | |
268 | - * data access operations on a single Statement, within Spring's managed JDBC | |
269 | - * environment: that is, participating in Spring-managed transactions and | |
270 | - * converting JDBC SQLExceptions into Spring's DataAccessException hierarchy. | |
271 | - * <p>The callback action can return a result object, for example a domain | |
272 | - * object or a collection of domain objects. | |
273 | - * @param psc a callback that creates a PreparedStatement given a Connection | |
274 | - * @param action a callback that specifies the action | |
275 | - * @return a result object returned by the action, or {@code null} if none | |
276 | - * @throws DataAccessException if there is any problem | |
277 | - */ | |
278 | - @Nullable | |
279 | - <T> T execute(PreparedStatementCreator psc, PreparedStatementCallback<T> action, Long tenantId) throws DataAccessException; | |
218 | + int[] batchUpdate(ID tenantId, String... sql) throws DataAccessException; | |
280 | 219 | |
281 | 220 | /** |
282 | 221 | * Execute a JDBC data access operation, implemented as callback action |
... | ... | @@ -292,20 +231,7 @@ public interface JdbcTemplateWrapperTenant { |
292 | 231 | * @throws DataAccessException if there is any problem |
293 | 232 | */ |
294 | 233 | @Nullable |
295 | - <T> T execute(String sql, PreparedStatementCallback<T> action, Long tenantId) throws DataAccessException; | |
296 | - | |
297 | - /** | |
298 | - * Query using a prepared statement, reading the ResultSet with a ResultSetExtractor. | |
299 | - * <p>A PreparedStatementCreator can either be implemented directly or | |
300 | - * configured through a PreparedStatementCreatorFactory. | |
301 | - * @param psc a callback that creates a PreparedStatement given a Connection | |
302 | - * @param rse a callback that will extract results | |
303 | - * @return an arbitrary result object, as returned by the ResultSetExtractor | |
304 | - * @throws DataAccessException if there is any problem | |
305 | - * @see PreparedStatementCreatorFactory | |
306 | - */ | |
307 | - @Nullable | |
308 | - <T> T query(PreparedStatementCreator psc, ResultSetExtractor<T> rse, Long tenantId) throws DataAccessException; | |
234 | + <T> T execute(String sql, PreparedStatementCallback<T> action, ID tenantId) throws DataAccessException; | |
309 | 235 | |
310 | 236 | /** |
311 | 237 | * Query using a prepared statement, reading the ResultSet with a ResultSetExtractor. |
... | ... | @@ -319,7 +245,7 @@ public interface JdbcTemplateWrapperTenant { |
319 | 245 | * @throws DataAccessException if there is any problem |
320 | 246 | */ |
321 | 247 | @Nullable |
322 | - <T> T query(String sql, @Nullable PreparedStatementSetter pss, ResultSetExtractor<T> rse, Long tenantId) throws DataAccessException; | |
248 | + <T> T query(String sql, @Nullable PreparedStatementSetter pss, ResultSetExtractor<T> rse, ID tenantId) throws DataAccessException; | |
323 | 249 | |
324 | 250 | /** |
325 | 251 | * Query given SQL to create a prepared statement from SQL and a list of arguments |
... | ... | @@ -334,7 +260,7 @@ public interface JdbcTemplateWrapperTenant { |
334 | 260 | * @see java.sql.Types |
335 | 261 | */ |
336 | 262 | @Nullable |
337 | - <T> T query(String sql, Object[] args, int[] argTypes, ResultSetExtractor<T> rse, Long tenantId) throws DataAccessException; | |
263 | + <T> T query(String sql, Object[] args, int[] argTypes, ResultSetExtractor<T> rse, ID tenantId) throws DataAccessException; | |
338 | 264 | |
339 | 265 | /** |
340 | 266 | * Query given SQL to create a prepared statement from SQL and a list of arguments |
... | ... | @@ -349,7 +275,7 @@ public interface JdbcTemplateWrapperTenant { |
349 | 275 | * @throws DataAccessException if the query fails |
350 | 276 | */ |
351 | 277 | @Nullable |
352 | - <T> T query(String sql, Object[] args, ResultSetExtractor<T> rse, Long tenantId) throws DataAccessException; | |
278 | + <T> T query(String sql, Object[] args, ResultSetExtractor<T> rse, ID tenantId) throws DataAccessException; | |
353 | 279 | |
354 | 280 | /** |
355 | 281 | * Query given SQL to create a prepared statement from SQL and a list of arguments |
... | ... | @@ -365,19 +291,7 @@ public interface JdbcTemplateWrapperTenant { |
365 | 291 | * @since 3.0.1 |
366 | 292 | */ |
367 | 293 | @Nullable |
368 | - <T> T query(String sql, ResultSetExtractor<T> rse, Long tenantId, @Nullable Object... args) throws DataAccessException; | |
369 | - | |
370 | - /** | |
371 | - * Query using a prepared statement, reading the ResultSet on a per-row basis | |
372 | - * with a RowCallbackHandler. | |
373 | - * <p>A PreparedStatementCreator can either be implemented directly or | |
374 | - * configured through a PreparedStatementCreatorFactory. | |
375 | - * @param psc a callback that creates a PreparedStatement given a Connection | |
376 | - * @param rch a callback that will extract results, one row at a time | |
377 | - * @throws DataAccessException if there is any problem | |
378 | - * @see PreparedStatementCreatorFactory | |
379 | - */ | |
380 | - void query(PreparedStatementCreator psc, RowCallbackHandler rch, Long tenantId) throws DataAccessException; | |
294 | + <T> T query(String sql, ResultSetExtractor<T> rse, ID tenantId, @Nullable Object... args) throws DataAccessException; | |
381 | 295 | |
382 | 296 | /** |
383 | 297 | * Query given SQL to create a prepared statement from SQL and a |
... | ... | @@ -391,7 +305,7 @@ public interface JdbcTemplateWrapperTenant { |
391 | 305 | * @param rch a callback that will extract results, one row at a time |
392 | 306 | * @throws DataAccessException if the query fails |
393 | 307 | */ |
394 | - void query(String sql, @Nullable PreparedStatementSetter pss, RowCallbackHandler rch, Long tenantId) throws DataAccessException; | |
308 | + void query(String sql, @Nullable PreparedStatementSetter pss, RowCallbackHandler rch, ID tenantId) throws DataAccessException; | |
395 | 309 | |
396 | 310 | /** |
397 | 311 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -405,7 +319,7 @@ public interface JdbcTemplateWrapperTenant { |
405 | 319 | * @throws DataAccessException if the query fails |
406 | 320 | * @see java.sql.Types |
407 | 321 | */ |
408 | - void query(String sql, Object[] args, int[] argTypes, RowCallbackHandler rch, Long tenantId) throws DataAccessException; | |
322 | + void query(String sql, Object[] args, int[] argTypes, RowCallbackHandler rch, ID tenantId) throws DataAccessException; | |
409 | 323 | |
410 | 324 | /** |
411 | 325 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -419,7 +333,7 @@ public interface JdbcTemplateWrapperTenant { |
419 | 333 | * @param rch a callback that will extract results, one row at a time |
420 | 334 | * @throws DataAccessException if the query fails |
421 | 335 | */ |
422 | - void query(String sql, Object[] args, RowCallbackHandler rch, Long tenantId) throws DataAccessException; | |
336 | + void query(String sql, Object[] args, RowCallbackHandler rch, ID tenantId) throws DataAccessException; | |
423 | 337 | |
424 | 338 | /** |
425 | 339 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -434,20 +348,7 @@ public interface JdbcTemplateWrapperTenant { |
434 | 348 | * @throws DataAccessException if the query fails |
435 | 349 | * @since 3.0.1 |
436 | 350 | */ |
437 | - void query(String sql, RowCallbackHandler rch, Long tenantId, @Nullable Object... args) throws DataAccessException; | |
438 | - | |
439 | - /** | |
440 | - * Query using a prepared statement, mapping each row to a result object | |
441 | - * via a RowMapper. | |
442 | - * <p>A PreparedStatementCreator can either be implemented directly or | |
443 | - * configured through a PreparedStatementCreatorFactory. | |
444 | - * @param psc a callback that creates a PreparedStatement given a Connection | |
445 | - * @param rowMapper a callback that will map one object per row | |
446 | - * @return the result List, containing mapped objects | |
447 | - * @throws DataAccessException if there is any problem | |
448 | - * @see PreparedStatementCreatorFactory | |
449 | - */ | |
450 | - <T> List<T> query(PreparedStatementCreator psc, RowMapper<T> rowMapper, Long tenantId) throws DataAccessException; | |
351 | + void query(String sql, RowCallbackHandler rch, ID tenantId, @Nullable Object... args) throws DataAccessException; | |
451 | 352 | |
452 | 353 | /** |
453 | 354 | * Query given SQL to create a prepared statement from SQL and a |
... | ... | @@ -462,7 +363,7 @@ public interface JdbcTemplateWrapperTenant { |
462 | 363 | * @return the result List, containing mapped objects |
463 | 364 | * @throws DataAccessException if the query fails |
464 | 365 | */ |
465 | - <T> List<T> query(String sql, @Nullable PreparedStatementSetter pss, RowMapper<T> rowMapper, Long tenantId) throws DataAccessException; | |
366 | + <T> List<T> query(String sql, @Nullable PreparedStatementSetter pss, RowMapper<T> rowMapper, ID tenantId) throws DataAccessException; | |
466 | 367 | |
467 | 368 | /** |
468 | 369 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -477,7 +378,7 @@ public interface JdbcTemplateWrapperTenant { |
477 | 378 | * @throws DataAccessException if the query fails |
478 | 379 | * @see java.sql.Types |
479 | 380 | */ |
480 | - <T> List<T> query(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper, Long tenantId) throws DataAccessException; | |
381 | + <T> List<T> query(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper, ID tenantId) throws DataAccessException; | |
481 | 382 | |
482 | 383 | /** |
483 | 384 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -492,7 +393,7 @@ public interface JdbcTemplateWrapperTenant { |
492 | 393 | * @return the result List, containing mapped objects |
493 | 394 | * @throws DataAccessException if the query fails |
494 | 395 | */ |
495 | - <T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper, Long tenantId) throws DataAccessException; | |
396 | + <T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper, ID tenantId) throws DataAccessException; | |
496 | 397 | |
497 | 398 | /** |
498 | 399 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -508,7 +409,7 @@ public interface JdbcTemplateWrapperTenant { |
508 | 409 | * @throws DataAccessException if the query fails |
509 | 410 | * @since 3.0.1 |
510 | 411 | */ |
511 | - <T> List<T> query(String sql, RowMapper<T> rowMapper, Long tenantId, @Nullable Object... args) throws DataAccessException; | |
412 | + <T> List<T> query(String sql, RowMapper<T> rowMapper, ID tenantId, @Nullable Object... args) throws DataAccessException; | |
512 | 413 | |
513 | 414 | /** |
514 | 415 | * Query given SQL to create a prepared statement from SQL and a list |
... | ... | @@ -527,7 +428,7 @@ public interface JdbcTemplateWrapperTenant { |
527 | 428 | * @throws DataAccessException if the query fails |
528 | 429 | */ |
529 | 430 | @Nullable |
530 | - <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper, Long tenantId) | |
431 | + <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper, ID tenantId) | |
531 | 432 | throws DataAccessException; |
532 | 433 | |
533 | 434 | /** |
... | ... | @@ -547,7 +448,7 @@ public interface JdbcTemplateWrapperTenant { |
547 | 448 | * @throws DataAccessException if the query fails |
548 | 449 | */ |
549 | 450 | @Nullable |
550 | - <T> T queryForObject(String sql, Object[] args, RowMapper<T> rowMapper, Long tenantId) throws DataAccessException; | |
451 | + <T> T queryForObject(String sql, Object[] args, RowMapper<T> rowMapper, ID tenantId) throws DataAccessException; | |
551 | 452 | |
552 | 453 | /** |
553 | 454 | * Query given SQL to create a prepared statement from SQL and a list |
... | ... | @@ -567,7 +468,7 @@ public interface JdbcTemplateWrapperTenant { |
567 | 468 | * @since 3.0.1 |
568 | 469 | */ |
569 | 470 | @Nullable |
570 | - <T> T queryForObject(String sql, RowMapper<T> rowMapper, Long tenantId, @Nullable Object... args) throws DataAccessException; | |
471 | + <T> T queryForObject(String sql, RowMapper<T> rowMapper, ID tenantId, @Nullable Object... args) throws DataAccessException; | |
571 | 472 | |
572 | 473 | /** |
573 | 474 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -587,7 +488,7 @@ public interface JdbcTemplateWrapperTenant { |
587 | 488 | * @see java.sql.Types |
588 | 489 | */ |
589 | 490 | @Nullable |
590 | - <T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T> requiredType, Long tenantId) | |
491 | + <T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T> requiredType, ID tenantId) | |
591 | 492 | throws DataAccessException; |
592 | 493 | |
593 | 494 | /** |
... | ... | @@ -608,7 +509,7 @@ public interface JdbcTemplateWrapperTenant { |
608 | 509 | * @see #queryForObject(String, Class) |
609 | 510 | */ |
610 | 511 | @Nullable |
611 | - <T> T queryForObject(String sql, Object[] args, Class<T> requiredType, Long tenantId) throws DataAccessException; | |
512 | + <T> T queryForObject(String sql, Object[] args, Class<T> requiredType, ID tenantId) throws DataAccessException; | |
612 | 513 | |
613 | 514 | /** |
614 | 515 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -629,7 +530,7 @@ public interface JdbcTemplateWrapperTenant { |
629 | 530 | * @see #queryForObject(String, Class) |
630 | 531 | */ |
631 | 532 | @Nullable |
632 | - <T> T queryForObject(String sql, Class<T> requiredType, Long tenantId, @Nullable Object... args) throws DataAccessException; | |
533 | + <T> T queryForObject(String sql, Class<T> requiredType, ID tenantId, @Nullable Object... args) throws DataAccessException; | |
633 | 534 | |
634 | 535 | /** |
635 | 536 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -648,7 +549,7 @@ public interface JdbcTemplateWrapperTenant { |
648 | 549 | * @see ColumnMapRowMapper |
649 | 550 | * @see java.sql.Types |
650 | 551 | */ |
651 | - Map<String, Object> queryForMap(String sql, Object[] args, int[] argTypes, Long tenantId) throws DataAccessException; | |
552 | + Map<String, Object> queryForMap(String sql, Object[] args, int[] argTypes, ID tenantId) throws DataAccessException; | |
652 | 553 | |
653 | 554 | /** |
654 | 555 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -671,7 +572,7 @@ public interface JdbcTemplateWrapperTenant { |
671 | 572 | * @see #queryForMap(String) |
672 | 573 | * @see ColumnMapRowMapper |
673 | 574 | */ |
674 | - Map<String, Object> queryForMap(String sql, Long tenantId, @Nullable Object... args) throws DataAccessException; | |
575 | + Map<String, Object> queryForMap(String sql, ID tenantId, @Nullable Object... args) throws DataAccessException; | |
675 | 576 | |
676 | 577 | /** |
677 | 578 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -689,7 +590,7 @@ public interface JdbcTemplateWrapperTenant { |
689 | 590 | * @see #queryForList(String, Class) |
690 | 591 | * @see SingleColumnRowMapper |
691 | 592 | */ |
692 | - <T> List<T> queryForList(String sql, Object[] args, int[] argTypes, Class<T> elementType, Long tenantId) | |
593 | + <T> List<T> queryForList(String sql, Object[] args, int[] argTypes, Class<T> elementType, ID tenantId) | |
693 | 594 | throws DataAccessException; |
694 | 595 | |
695 | 596 | /** |
... | ... | @@ -709,7 +610,7 @@ public interface JdbcTemplateWrapperTenant { |
709 | 610 | * @see #queryForList(String, Class) |
710 | 611 | * @see SingleColumnRowMapper |
711 | 612 | */ |
712 | - <T> List<T> queryForList(String sql, Object[] args, Class<T> elementType, Long tenantId) throws DataAccessException; | |
613 | + <T> List<T> queryForList(String sql, Object[] args, Class<T> elementType, ID tenantId) throws DataAccessException; | |
713 | 614 | |
714 | 615 | /** |
715 | 616 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -729,7 +630,7 @@ public interface JdbcTemplateWrapperTenant { |
729 | 630 | * @see #queryForList(String, Class) |
730 | 631 | * @see SingleColumnRowMapper |
731 | 632 | */ |
732 | - <T> List<T> queryForList(String sql, Class<T> elementType, Long tenantId, @Nullable Object... args) throws DataAccessException; | |
633 | + <T> List<T> queryForList(String sql, Class<T> elementType, ID tenantId, @Nullable Object... args) throws DataAccessException; | |
733 | 634 | |
734 | 635 | /** |
735 | 636 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -747,7 +648,7 @@ public interface JdbcTemplateWrapperTenant { |
747 | 648 | * @see #queryForList(String) |
748 | 649 | * @see java.sql.Types |
749 | 650 | */ |
750 | - List<Map<String, Object>> queryForList(String sql, Object[] args, int[] argTypes, Long tenantId) throws DataAccessException; | |
651 | + List<Map<String, Object>> queryForList(String sql, Object[] args, int[] argTypes, ID tenantId) throws DataAccessException; | |
751 | 652 | |
752 | 653 | /** |
753 | 654 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -765,7 +666,7 @@ public interface JdbcTemplateWrapperTenant { |
765 | 666 | * @throws DataAccessException if the query fails |
766 | 667 | * @see #queryForList(String) |
767 | 668 | */ |
768 | - List<Map<String, Object>> queryForList(String sql, Long tenantId, @Nullable Object... args) throws DataAccessException; | |
669 | + List<Map<String, Object>> queryForList(String sql, ID tenantId, @Nullable Object... args) throws DataAccessException; | |
769 | 670 | |
770 | 671 | /** |
771 | 672 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -788,7 +689,7 @@ public interface JdbcTemplateWrapperTenant { |
788 | 689 | * @see javax.sql.rowset.CachedRowSet |
789 | 690 | * @see java.sql.Types |
790 | 691 | */ |
791 | - SqlRowSet queryForRowSet(String sql, Object[] args, int[] argTypes, Long tenantId) throws DataAccessException; | |
692 | + SqlRowSet queryForRowSet(String sql, Object[] args, int[] argTypes, ID tenantId) throws DataAccessException; | |
792 | 693 | |
793 | 694 | /** |
794 | 695 | * Query given SQL to create a prepared statement from SQL and a list of |
... | ... | @@ -811,35 +712,7 @@ public interface JdbcTemplateWrapperTenant { |
811 | 712 | * @see SqlRowSetResultSetExtractor |
812 | 713 | * @see javax.sql.rowset.CachedRowSet |
813 | 714 | */ |
814 | - SqlRowSet queryForRowSet(String sql, Long tenantId, @Nullable Object... args) throws DataAccessException; | |
815 | - | |
816 | - /** | |
817 | - * Issue a single SQL update operation (such as an insert, update or delete | |
818 | - * statement) using a PreparedStatementCreator to provide SQL and any | |
819 | - * required parameters. | |
820 | - * <p>A PreparedStatementCreator can either be implemented directly or | |
821 | - * configured through a PreparedStatementCreatorFactory. | |
822 | - * @param psc a callback that provides SQL and any necessary parameters | |
823 | - * @return the number of rows affected | |
824 | - * @throws DataAccessException if there is any problem issuing the update | |
825 | - * @see PreparedStatementCreatorFactory | |
826 | - */ | |
827 | - int update(PreparedStatementCreator psc, Long tenantId) throws DataAccessException; | |
828 | - | |
829 | - /** | |
830 | - * Issue an update statement using a PreparedStatementCreator to provide SQL and | |
831 | - * any required parameters. Generated keys will be put into the given KeyHolder. | |
832 | - * <p>Note that the given PreparedStatementCreator has to create a statement | |
833 | - * with activated extraction of generated keys (a JDBC 3.0 feature). This can | |
834 | - * either be done directly or through using a PreparedStatementCreatorFactory. | |
835 | - * @param psc a callback that provides SQL and any necessary parameters | |
836 | - * @param generatedKeyHolder a KeyHolder that will hold the generated keys | |
837 | - * @return the number of rows affected | |
838 | - * @throws DataAccessException if there is any problem issuing the update | |
839 | - * @see PreparedStatementCreatorFactory | |
840 | - * @see org.springframework.jdbc.support.GeneratedKeyHolder | |
841 | - */ | |
842 | - int update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder, Long tenantId) throws DataAccessException; | |
715 | + SqlRowSet queryForRowSet(String sql, ID tenantId, @Nullable Object... args) throws DataAccessException; | |
843 | 716 | |
844 | 717 | /** |
845 | 718 | * Issue an update statement using a PreparedStatementSetter to set bind parameters, |
... | ... | @@ -852,7 +725,7 @@ public interface JdbcTemplateWrapperTenant { |
852 | 725 | * @return the number of rows affected |
853 | 726 | * @throws DataAccessException if there is any problem issuing the update |
854 | 727 | */ |
855 | - int update(String sql, @Nullable PreparedStatementSetter pss, Long tenantId) throws DataAccessException; | |
728 | + int update(String sql, @Nullable PreparedStatementSetter pss, ID tenantId) throws DataAccessException; | |
856 | 729 | |
857 | 730 | /** |
858 | 731 | * Issue a single SQL update operation (such as an insert, update or delete statement) |
... | ... | @@ -865,7 +738,7 @@ public interface JdbcTemplateWrapperTenant { |
865 | 738 | * @throws DataAccessException if there is any problem issuing the update |
866 | 739 | * @see java.sql.Types |
867 | 740 | */ |
868 | - int update(String sql, Object[] args, int[] argTypes, Long tenantId) throws DataAccessException; | |
741 | + int update(String sql, Object[] args, int[] argTypes, ID tenantId) throws DataAccessException; | |
869 | 742 | |
870 | 743 | /** |
871 | 744 | * Issue a single SQL update operation (such as an insert, update or delete statement) |
... | ... | @@ -878,7 +751,7 @@ public interface JdbcTemplateWrapperTenant { |
878 | 751 | * @return the number of rows affected |
879 | 752 | * @throws DataAccessException if there is any problem issuing the update |
880 | 753 | */ |
881 | - int update(String sql, Long tenantId, @Nullable Object... args) throws DataAccessException; | |
754 | + int update(String sql, ID tenantId, @Nullable Object... args) throws DataAccessException; | |
882 | 755 | |
883 | 756 | /** |
884 | 757 | * Issue multiple update statements on a single PreparedStatement, |
... | ... | @@ -892,7 +765,7 @@ public interface JdbcTemplateWrapperTenant { |
892 | 765 | * @return an array of the number of rows affected by each statement |
893 | 766 | * @throws DataAccessException if there is any problem issuing the update |
894 | 767 | */ |
895 | - int[] batchUpdate(String sql, BatchPreparedStatementSetter pss, Long tenantId) throws DataAccessException; | |
768 | + int[] batchUpdate(String sql, BatchPreparedStatementSetter pss, ID tenantId) throws DataAccessException; | |
896 | 769 | |
897 | 770 | /** |
898 | 771 | * Execute a batch using the supplied SQL statement with the batch of supplied arguments. |
... | ... | @@ -900,7 +773,7 @@ public interface JdbcTemplateWrapperTenant { |
900 | 773 | * @param batchArgs the List of Object arrays containing the batch of arguments for the query |
901 | 774 | * @return an array containing the numbers of rows affected by each update in the batch |
902 | 775 | */ |
903 | - int[] batchUpdate(String sql, List<Object[]> batchArgs, Long tenantId) throws DataAccessException; | |
776 | + int[] batchUpdate(String sql, List<Object[]> batchArgs, ID tenantId) throws DataAccessException; | |
904 | 777 | |
905 | 778 | /** |
906 | 779 | * Execute a batch using the supplied SQL statement with the batch of supplied arguments. |
... | ... | @@ -910,7 +783,7 @@ public interface JdbcTemplateWrapperTenant { |
910 | 783 | * (constants from {@code java.sql.Types}) |
911 | 784 | * @return an array containing the numbers of rows affected by each update in the batch |
912 | 785 | */ |
913 | - int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes, Long tenantId) throws DataAccessException; | |
786 | + int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes, ID tenantId) throws DataAccessException; | |
914 | 787 | |
915 | 788 | /** |
916 | 789 | * Execute multiple batches using the supplied SQL statement with the collect of supplied arguments. |
... | ... | @@ -925,52 +798,6 @@ public interface JdbcTemplateWrapperTenant { |
925 | 798 | * @since 3.1 |
926 | 799 | */ |
927 | 800 | <T> int[][] batchUpdate(String sql, Collection<T> batchArgs, int batchSize, |
928 | - ParameterizedPreparedStatementSetter<T> pss, Long tenantId) throws DataAccessException; | |
929 | - | |
930 | - | |
931 | - //------------------------------------------------------------------------- | |
932 | - // Methods dealing with callable statements | |
933 | - //------------------------------------------------------------------------- | |
934 | - | |
935 | - /** | |
936 | - * Execute a JDBC data access operation, implemented as callback action | |
937 | - * working on a JDBC CallableStatement. This allows for implementing arbitrary | |
938 | - * data access operations on a single Statement, within Spring's managed JDBC | |
939 | - * environment: that is, participating in Spring-managed transactions and | |
940 | - * converting JDBC SQLExceptions into Spring's DataAccessException hierarchy. | |
941 | - * <p>The callback action can return a result object, for example a domain | |
942 | - * object or a collection of domain objects. | |
943 | - * @param csc a callback that creates a CallableStatement given a Connection | |
944 | - * @param action a callback that specifies the action | |
945 | - * @return a result object returned by the action, or {@code null} if none | |
946 | - * @throws DataAccessException if there is any problem | |
947 | - */ | |
948 | - @Nullable | |
949 | - <T> T execute(CallableStatementCreator csc, CallableStatementCallback<T> action, Long tenantId) throws DataAccessException; | |
950 | - | |
951 | - /** | |
952 | - * Execute a JDBC data access operation, implemented as callback action | |
953 | - * working on a JDBC CallableStatement. This allows for implementing arbitrary | |
954 | - * data access operations on a single Statement, within Spring's managed JDBC | |
955 | - * environment: that is, participating in Spring-managed transactions and | |
956 | - * converting JDBC SQLExceptions into Spring's DataAccessException hierarchy. | |
957 | - * <p>The callback action can return a result object, for example a domain | |
958 | - * object or a collection of domain objects. | |
959 | - * @param callString the SQL call string to execute | |
960 | - * @param action a callback that specifies the action | |
961 | - * @return a result object returned by the action, or {@code null} if none | |
962 | - * @throws DataAccessException if there is any problem | |
963 | - */ | |
964 | - @Nullable | |
965 | - <T> T execute(String callString, CallableStatementCallback<T> action, Long tenantId) throws DataAccessException; | |
801 | + ParameterizedPreparedStatementSetter<T> pss, ID tenantId) throws DataAccessException; | |
966 | 802 | |
967 | - /** | |
968 | - * Execute a SQL call using a CallableStatementCreator to provide SQL and | |
969 | - * any required parameters. | |
970 | - * @param csc a callback that provides SQL and any necessary parameters | |
971 | - * @param declaredParameters list of declared SqlParameter objects | |
972 | - * @return a Map of extracted out parameters | |
973 | - * @throws DataAccessException if there is any problem issuing the update | |
974 | - */ | |
975 | - Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters, Long tenantId) throws DataAccessException; | |
976 | 803 | } | ... | ... |
src/main/java/com/taover/repository/jdbctemplate/JdbcTemplateWrapperTenantImpl.java
1 | 1 | package com.taover.repository.jdbctemplate; |
2 | 2 | |
3 | +import java.io.Serializable; | |
3 | 4 | import java.util.Collection; |
4 | 5 | import java.util.List; |
5 | 6 | import java.util.Map; |
6 | 7 | |
7 | -import org.apache.shardingsphere.infra.hint.HintManager; | |
8 | 8 | import org.springframework.dao.DataAccessException; |
9 | 9 | import org.springframework.jdbc.core.BatchPreparedStatementSetter; |
10 | -import org.springframework.jdbc.core.CallableStatementCallback; | |
11 | -import org.springframework.jdbc.core.CallableStatementCreator; | |
12 | -import org.springframework.jdbc.core.ConnectionCallback; | |
13 | 10 | import org.springframework.jdbc.core.JdbcTemplate; |
14 | 11 | import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter; |
15 | 12 | import org.springframework.jdbc.core.PreparedStatementCallback; |
16 | -import org.springframework.jdbc.core.PreparedStatementCreator; | |
17 | 13 | import org.springframework.jdbc.core.PreparedStatementSetter; |
18 | 14 | import org.springframework.jdbc.core.ResultSetExtractor; |
19 | 15 | import org.springframework.jdbc.core.RowCallbackHandler; |
20 | 16 | import org.springframework.jdbc.core.RowMapper; |
21 | -import org.springframework.jdbc.core.SqlParameter; | |
22 | -import org.springframework.jdbc.core.StatementCallback; | |
23 | -import org.springframework.jdbc.support.KeyHolder; | |
24 | 17 | import org.springframework.jdbc.support.rowset.SqlRowSet; |
25 | 18 | |
26 | -import com.taover.repository.shardingsphere.ShardingInfoEntity; | |
27 | -import com.taover.repository.shardingsphere.ShardingSphereService; | |
19 | +import com.taover.repository.exception.NoContainTenantException; | |
28 | 20 | |
29 | -public class JdbcTemplateWrapperTenantImpl implements JdbcTemplateWrapperTenant { | |
21 | +public class JdbcTemplateWrapperTenantImpl<ID extends Serializable> implements JdbcTemplateWrapperTenant<ID> { | |
30 | 22 | private JdbcTemplate jdbcTemplate; |
31 | - private ShardingSphereService shardingSphereService; | |
32 | - | |
33 | - public JdbcTemplateWrapperTenantImpl(JdbcTemplate jdbcTemplate, ShardingSphereService shardingSphereService) { | |
23 | + public JdbcTemplateWrapperTenantImpl(JdbcTemplate jdbcTemplate) { | |
34 | 24 | this.jdbcTemplate = jdbcTemplate; |
35 | - this.shardingSphereService = shardingSphereService; | |
36 | 25 | } |
37 | - | |
26 | + | |
38 | 27 | @Override |
39 | - public void loadShardingInfo(Long tenantId) { | |
40 | - List<ShardingInfoEntity> shardingInfo = this.shardingSphereService.getShardingInfoByTenantId(tenantId); | |
41 | - if(shardingInfo == null || shardingInfo.isEmpty()) { | |
42 | - return; | |
43 | - } | |
44 | - HintManager.clear(); | |
45 | - HintManager instance = HintManager.getInstance(); | |
46 | - for(ShardingInfoEntity item: shardingInfo) { | |
47 | - instance.addTableShardingValue(item.getTableName(), item.getTableSuffix()); | |
48 | - instance.addDatabaseShardingValue(item.getTableName(), item.getDsName()); | |
49 | - } | |
28 | + public void doCheckTenantId(String sql, ID tenantId) throws NoContainTenantException { | |
29 | + | |
50 | 30 | } |
51 | 31 | |
52 | 32 | @Override |
53 | - public void clearShardingInfo() { | |
54 | - HintManager.clear(); | |
33 | + public JdbcTemplate getJdbcTemplate() { | |
34 | + return this.jdbcTemplate; | |
55 | 35 | } |
56 | 36 | |
57 | 37 | @Override |
58 | - public <E> E execute(ConnectionCallback<E> action, Long tenantId) throws DataAccessException { | |
59 | - try { | |
60 | - this.loadShardingInfo(tenantId); | |
61 | - return this.jdbcTemplate.execute(action); | |
62 | - }finally { | |
63 | - this.clearShardingInfo(); | |
64 | - } | |
38 | + public void execute(String sql, ID tenantId) throws DataAccessException { | |
39 | + this.doCheckTenantId(sql, tenantId); | |
40 | + this.jdbcTemplate.execute(sql); | |
65 | 41 | } |
66 | 42 | |
67 | 43 | @Override |
68 | - public <E> E execute(StatementCallback<E> action, Long tenantId) throws DataAccessException { | |
69 | - try { | |
70 | - this.loadShardingInfo(tenantId); | |
71 | - return this.jdbcTemplate.execute(action); | |
72 | - }finally { | |
73 | - this.clearShardingInfo(); | |
74 | - } | |
44 | + public <E> E query(String sql, ResultSetExtractor<E> rse, ID tenantId) throws DataAccessException { | |
45 | + this.doCheckTenantId(sql, tenantId); | |
46 | + return this.jdbcTemplate.query(sql, rse); | |
75 | 47 | } |
76 | 48 | |
77 | 49 | @Override |
78 | - public void execute(String sql, Long tenantId) throws DataAccessException { | |
79 | - try { | |
80 | - this.loadShardingInfo(tenantId); | |
81 | - this.jdbcTemplate.execute(sql); | |
82 | - }finally { | |
83 | - this.clearShardingInfo(); | |
84 | - } | |
50 | + public void query(String sql, RowCallbackHandler rch, ID tenantId) throws DataAccessException { | |
51 | + this.doCheckTenantId(sql, tenantId); | |
52 | + this.jdbcTemplate.query(sql, rch); | |
85 | 53 | } |
86 | 54 | |
87 | 55 | @Override |
88 | - public <E> E query(String sql, ResultSetExtractor<E> rse, Long tenantId) throws DataAccessException { | |
89 | - try { | |
90 | - this.loadShardingInfo(tenantId); | |
91 | - return this.jdbcTemplate.query(sql, rse); | |
92 | - }finally { | |
93 | - this.clearShardingInfo(); | |
94 | - } | |
56 | + public <E> List<E> query(String sql, RowMapper<E> rowMapper, ID tenantId) throws DataAccessException { | |
57 | + this.doCheckTenantId(sql, tenantId); | |
58 | + return this.jdbcTemplate.query(sql, rowMapper); | |
95 | 59 | } |
96 | 60 | |
97 | 61 | @Override |
98 | - public void query(String sql, RowCallbackHandler rch, Long tenantId) throws DataAccessException { | |
99 | - try { | |
100 | - this.loadShardingInfo(tenantId); | |
101 | - this.jdbcTemplate.query(sql, rch); | |
102 | - }finally { | |
103 | - this.clearShardingInfo(); | |
104 | - } | |
62 | + public <E> E queryForObject(String sql, RowMapper<E> rowMapper, ID tenantId) throws DataAccessException { | |
63 | + this.doCheckTenantId(sql, tenantId); | |
64 | + return this.jdbcTemplate.queryForObject(sql, rowMapper); | |
105 | 65 | } |
106 | 66 | |
107 | 67 | @Override |
108 | - public <E> List<E> query(String sql, RowMapper<E> rowMapper, Long tenantId) throws DataAccessException { | |
109 | - try { | |
110 | - this.loadShardingInfo(tenantId); | |
111 | - return this.jdbcTemplate.query(sql, rowMapper); | |
112 | - }finally { | |
113 | - this.clearShardingInfo(); | |
114 | - } | |
68 | + public <E> E queryForObject(String sql, Class<E> requiredType, ID tenantId) throws DataAccessException { | |
69 | + this.doCheckTenantId(sql, tenantId); | |
70 | + return this.jdbcTemplate.queryForObject(sql, requiredType); | |
115 | 71 | } |
116 | 72 | |
117 | 73 | @Override |
118 | - public <E> E queryForObject(String sql, RowMapper<E> rowMapper, Long tenantId) throws DataAccessException { | |
119 | - try { | |
120 | - this.loadShardingInfo(tenantId); | |
121 | - return this.jdbcTemplate.queryForObject(sql, rowMapper); | |
122 | - }finally { | |
123 | - this.clearShardingInfo(); | |
124 | - } | |
74 | + public Map<String, Object> queryForMap(String sql, ID tenantId) throws DataAccessException { | |
75 | + this.doCheckTenantId(sql, tenantId); | |
76 | + return this.jdbcTemplate.queryForMap(sql); | |
125 | 77 | } |
126 | 78 | |
127 | 79 | @Override |
128 | - public <E> E queryForObject(String sql, Class<E> requiredType, Long tenantId) throws DataAccessException { | |
129 | - try { | |
130 | - this.loadShardingInfo(tenantId); | |
131 | - return this.jdbcTemplate.queryForObject(sql, requiredType); | |
132 | - }finally { | |
133 | - this.clearShardingInfo(); | |
134 | - } | |
80 | + public <E> List<E> queryForList(String sql, Class<E> elementType, ID tenantId) throws DataAccessException { | |
81 | + this.doCheckTenantId(sql, tenantId); | |
82 | + return this.jdbcTemplate.queryForList(sql, elementType); | |
135 | 83 | } |
136 | 84 | |
137 | 85 | @Override |
138 | - public Map<String, Object> queryForMap(String sql, Long tenantId) throws DataAccessException { | |
139 | - try { | |
140 | - this.loadShardingInfo(tenantId); | |
141 | - return this.jdbcTemplate.queryForMap(sql); | |
142 | - }finally { | |
143 | - this.clearShardingInfo(); | |
144 | - } | |
86 | + public List<Map<String, Object>> queryForList(String sql, ID tenantId) throws DataAccessException { | |
87 | + return this.jdbcTemplate.queryForList(sql); | |
145 | 88 | } |
146 | 89 | |
147 | 90 | @Override |
148 | - public <E> List<E> queryForList(String sql, Class<E> elementType, Long tenantId) throws DataAccessException { | |
149 | - try { | |
150 | - this.loadShardingInfo(tenantId); | |
151 | - return this.jdbcTemplate.queryForList(sql, elementType); | |
152 | - }finally { | |
153 | - this.clearShardingInfo(); | |
154 | - } | |
91 | + public SqlRowSet queryForRowSet(String sql, ID tenantId) throws DataAccessException { | |
92 | + this.doCheckTenantId(sql, tenantId); | |
93 | + return this.jdbcTemplate.queryForRowSet(sql); | |
155 | 94 | } |
156 | 95 | |
157 | 96 | @Override |
158 | - public List<Map<String, Object>> queryForList(String sql, Long tenantId) throws DataAccessException { | |
159 | - try { | |
160 | - this.loadShardingInfo(tenantId); | |
161 | - return this.jdbcTemplate.queryForList(sql); | |
162 | - }finally { | |
163 | - this.clearShardingInfo(); | |
164 | - } | |
97 | + public int update(String sql, ID tenantId) throws DataAccessException { | |
98 | + return this.jdbcTemplate.update(sql); | |
165 | 99 | } |
166 | 100 | |
167 | 101 | @Override |
168 | - public SqlRowSet queryForRowSet(String sql, Long tenantId) throws DataAccessException { | |
169 | - try { | |
170 | - this.loadShardingInfo(tenantId); | |
171 | - return this.jdbcTemplate.queryForRowSet(sql); | |
172 | - }finally { | |
173 | - this.clearShardingInfo(); | |
102 | + public int[] batchUpdate(ID tenantId, String... sql) throws DataAccessException { | |
103 | + for(String item: sql) { | |
104 | + this.doCheckTenantId(item, tenantId); | |
174 | 105 | } |
106 | + return this.jdbcTemplate.batchUpdate(sql); | |
175 | 107 | } |
176 | 108 | |
177 | 109 | @Override |
178 | - public int update(String sql, Long tenantId) throws DataAccessException { | |
179 | - try { | |
180 | - this.loadShardingInfo(tenantId); | |
181 | - return this.jdbcTemplate.update(sql); | |
182 | - }finally { | |
183 | - this.clearShardingInfo(); | |
184 | - } | |
110 | + public <E> E execute(String sql, PreparedStatementCallback<E> action, ID tenantId) throws DataAccessException { | |
111 | + this.doCheckTenantId(sql, tenantId); | |
112 | + return this.jdbcTemplate.execute(sql, action); | |
185 | 113 | } |
186 | 114 | |
187 | 115 | @Override |
188 | - public int[] batchUpdate(Long tenantId, String... sql) throws DataAccessException { | |
189 | - try { | |
190 | - this.loadShardingInfo(tenantId); | |
191 | - return this.jdbcTemplate.batchUpdate(sql); | |
192 | - }finally { | |
193 | - this.clearShardingInfo(); | |
194 | - } | |
195 | - } | |
196 | - | |
197 | - @Override | |
198 | - public <E> E execute(PreparedStatementCreator psc, PreparedStatementCallback<E> action, Long tenantId) | |
116 | + public <E> E query(String sql, PreparedStatementSetter pss, ResultSetExtractor<E> rse, ID tenantId) | |
199 | 117 | throws DataAccessException { |
200 | - try { | |
201 | - this.loadShardingInfo(tenantId); | |
202 | - return this.jdbcTemplate.execute(psc, action); | |
203 | - }finally { | |
204 | - this.clearShardingInfo(); | |
205 | - } | |
118 | + this.doCheckTenantId(sql, tenantId); | |
119 | + return this.jdbcTemplate.query(sql, pss, rse); | |
206 | 120 | } |
207 | 121 | |
208 | 122 | @Override |
209 | - public <E> E execute(String sql, PreparedStatementCallback<E> action, Long tenantId) throws DataAccessException { | |
210 | - try { | |
211 | - this.loadShardingInfo(tenantId); | |
212 | - return this.jdbcTemplate.execute(sql, action); | |
213 | - }finally { | |
214 | - this.clearShardingInfo(); | |
215 | - } | |
216 | - } | |
217 | - | |
218 | - @Override | |
219 | - public <E> E query(PreparedStatementCreator psc, ResultSetExtractor<E> rse, Long tenantId) | |
123 | + public <E> E query(String sql, Object[] args, int[] argTypes, ResultSetExtractor<E> rse, ID tenantId) | |
220 | 124 | throws DataAccessException { |
221 | - try { | |
222 | - this.loadShardingInfo(tenantId); | |
223 | - return this.jdbcTemplate.query(psc, rse); | |
224 | - }finally { | |
225 | - this.clearShardingInfo(); | |
226 | - } | |
125 | + this.doCheckTenantId(sql, tenantId); | |
126 | + return this.jdbcTemplate.query(sql, args, argTypes, rse); | |
227 | 127 | } |
228 | 128 | |
229 | 129 | @Override |
230 | - public <E> E query(String sql, PreparedStatementSetter pss, ResultSetExtractor<E> rse, Long tenantId) | |
231 | - throws DataAccessException { | |
232 | - try { | |
233 | - this.loadShardingInfo(tenantId); | |
234 | - return this.jdbcTemplate.query(sql, pss, rse); | |
235 | - }finally { | |
236 | - this.clearShardingInfo(); | |
237 | - } | |
130 | + public <E> E query(String sql, Object[] args, ResultSetExtractor<E> rse, ID tenantId) throws DataAccessException { | |
131 | + this.doCheckTenantId(sql, tenantId); | |
132 | + return this.jdbcTemplate.query(sql, args, rse); | |
238 | 133 | } |
239 | 134 | |
240 | 135 | @Override |
241 | - public <E> E query(String sql, Object[] args, int[] argTypes, ResultSetExtractor<E> rse, Long tenantId) | |
136 | + public <E> E query(String sql, ResultSetExtractor<E> rse, ID tenantId, Object... args) | |
242 | 137 | throws DataAccessException { |
243 | - try { | |
244 | - this.loadShardingInfo(tenantId); | |
245 | - return this.jdbcTemplate.query(sql, args, argTypes, rse); | |
246 | - }finally { | |
247 | - this.clearShardingInfo(); | |
248 | - } | |
249 | - } | |
250 | - | |
251 | - @Override | |
252 | - public <E> E query(String sql, Object[] args, ResultSetExtractor<E> rse, Long tenantId) throws DataAccessException { | |
253 | - try { | |
254 | - this.loadShardingInfo(tenantId); | |
255 | - return this.jdbcTemplate.query(sql, args, rse); | |
256 | - }finally { | |
257 | - this.clearShardingInfo(); | |
258 | - } | |
259 | - } | |
260 | - | |
261 | - @Override | |
262 | - public <E> E query(String sql, ResultSetExtractor<E> rse, Long tenantId, Object... args) | |
263 | - throws DataAccessException { | |
264 | - try { | |
265 | - this.loadShardingInfo(tenantId); | |
138 | + | |
139 | + this.doCheckTenantId(sql, tenantId); | |
266 | 140 | return this.jdbcTemplate.query(sql, rse, args); |
267 | - }finally { | |
268 | - this.clearShardingInfo(); | |
269 | - } | |
270 | - } | |
271 | - | |
272 | - @Override | |
273 | - public void query(PreparedStatementCreator psc, RowCallbackHandler rch, Long tenantId) throws DataAccessException { | |
274 | - try { | |
275 | - this.loadShardingInfo(tenantId); | |
276 | - this.jdbcTemplate.query(psc, rch); | |
277 | - }finally { | |
278 | - this.clearShardingInfo(); | |
279 | - } | |
280 | 141 | } |
281 | 142 | |
282 | 143 | @Override |
283 | - public void query(String sql, PreparedStatementSetter pss, RowCallbackHandler rch, Long tenantId) | |
144 | + public void query(String sql, PreparedStatementSetter pss, RowCallbackHandler rch, ID tenantId) | |
284 | 145 | throws DataAccessException { |
285 | - try { | |
286 | - this.loadShardingInfo(tenantId); | |
287 | - this.jdbcTemplate.query(sql, pss, rch); | |
288 | - }finally { | |
289 | - this.clearShardingInfo(); | |
290 | - } | |
146 | + this.doCheckTenantId(sql, tenantId); | |
147 | + this.jdbcTemplate.query(sql, pss, rch); | |
291 | 148 | } |
292 | 149 | |
293 | 150 | @Override |
294 | - public void query(String sql, Object[] args, int[] argTypes, RowCallbackHandler rch, Long tenantId) | |
295 | - throws DataAccessException { | |
296 | - try { | |
297 | - this.loadShardingInfo(tenantId); | |
298 | - this.jdbcTemplate.query(sql, args, argTypes, rch); | |
299 | - }finally { | |
300 | - this.clearShardingInfo(); | |
301 | - } | |
151 | + public void query(String sql, Object[] args, int[] argTypes, RowCallbackHandler rch, ID tenantId) | |
152 | + throws DataAccessException { | |
153 | + this.doCheckTenantId(sql, tenantId); | |
154 | + this.jdbcTemplate.query(sql, args, argTypes, rch); | |
302 | 155 | } |
303 | 156 | |
304 | 157 | @Override |
305 | - public void query(String sql, Object[] args, RowCallbackHandler rch, Long tenantId) throws DataAccessException { | |
306 | - try { | |
307 | - this.loadShardingInfo(tenantId); | |
308 | - this.jdbcTemplate.query(sql, args, rch); | |
309 | - }finally { | |
310 | - this.clearShardingInfo(); | |
311 | - } | |
158 | + public void query(String sql, Object[] args, RowCallbackHandler rch, ID tenantId) throws DataAccessException { | |
159 | + this.doCheckTenantId(sql, tenantId); | |
160 | + this.jdbcTemplate.query(sql, args, rch); | |
312 | 161 | } |
313 | 162 | |
314 | 163 | @Override |
315 | - public void query(String sql, RowCallbackHandler rch, Long tenantId, Object... args) throws DataAccessException { | |
316 | - try { | |
317 | - this.loadShardingInfo(tenantId); | |
318 | - this.jdbcTemplate.query(sql, rch, args); | |
319 | - }finally { | |
320 | - this.clearShardingInfo(); | |
321 | - } | |
164 | + public void query(String sql, RowCallbackHandler rch, ID tenantId, Object... args) throws DataAccessException { | |
165 | + this.doCheckTenantId(sql, tenantId); | |
166 | + this.jdbcTemplate.query(sql, rch, args); | |
322 | 167 | } |
323 | 168 | |
324 | 169 | @Override |
325 | - public <E> List<E> query(PreparedStatementCreator psc, RowMapper<E> rowMapper, Long tenantId) | |
170 | + public <E> List<E> query(String sql, PreparedStatementSetter pss, RowMapper<E> rowMapper, ID tenantId) | |
326 | 171 | throws DataAccessException { |
327 | - try { | |
328 | - this.loadShardingInfo(tenantId); | |
329 | - return this.jdbcTemplate.query(psc, rowMapper); | |
330 | - }finally { | |
331 | - this.clearShardingInfo(); | |
332 | - } | |
172 | + this.doCheckTenantId(sql, tenantId); | |
173 | + return this.jdbcTemplate.query(sql, pss, rowMapper); | |
333 | 174 | } |
334 | 175 | |
335 | 176 | @Override |
336 | - public <E> List<E> query(String sql, PreparedStatementSetter pss, RowMapper<E> rowMapper, Long tenantId) | |
177 | + public <E> List<E> query(String sql, Object[] args, int[] argTypes, RowMapper<E> rowMapper, ID tenantId) | |
337 | 178 | throws DataAccessException { |
338 | - try { | |
339 | - this.loadShardingInfo(tenantId); | |
340 | - return this.jdbcTemplate.query(sql, pss, rowMapper); | |
341 | - }finally { | |
342 | - this.clearShardingInfo(); | |
343 | - } | |
179 | + this.doCheckTenantId(sql, tenantId); | |
180 | + return this.jdbcTemplate.query(sql, args, argTypes, rowMapper); | |
344 | 181 | } |
345 | 182 | |
346 | 183 | @Override |
347 | - public <E> List<E> query(String sql, Object[] args, int[] argTypes, RowMapper<E> rowMapper, Long tenantId) | |
184 | + public <E> List<E> query(String sql, Object[] args, RowMapper<E> rowMapper, ID tenantId) | |
348 | 185 | throws DataAccessException { |
349 | - try { | |
350 | - this.loadShardingInfo(tenantId); | |
351 | - return this.jdbcTemplate.query(sql, args, argTypes, rowMapper); | |
352 | - }finally { | |
353 | - this.clearShardingInfo(); | |
354 | - } | |
186 | + this.doCheckTenantId(sql, tenantId); | |
187 | + return this.jdbcTemplate.query(sql, args, rowMapper); | |
355 | 188 | } |
356 | 189 | |
357 | 190 | @Override |
358 | - public <E> List<E> query(String sql, Object[] args, RowMapper<E> rowMapper, Long tenantId) | |
359 | - throws DataAccessException { | |
360 | - try { | |
361 | - this.loadShardingInfo(tenantId); | |
362 | - return this.jdbcTemplate.query(sql, args, rowMapper); | |
363 | - }finally { | |
364 | - this.clearShardingInfo(); | |
365 | - } | |
191 | + public <E> List<E> query(String sql, RowMapper<E> rowMapper, ID tenantId, Object... args) | |
192 | + throws DataAccessException { | |
193 | + this.doCheckTenantId(sql, tenantId); | |
194 | + return this.jdbcTemplate.query(sql, rowMapper, args); | |
366 | 195 | } |
367 | 196 | |
368 | 197 | @Override |
369 | - public <E> List<E> query(String sql, RowMapper<E> rowMapper, Long tenantId, Object... args) | |
370 | - throws DataAccessException { | |
371 | - try { | |
372 | - this.loadShardingInfo(tenantId); | |
373 | - return this.jdbcTemplate.query(sql, rowMapper, args); | |
374 | - }finally { | |
375 | - this.clearShardingInfo(); | |
376 | - } | |
198 | + public <E> E queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<E> rowMapper, ID tenantId) | |
199 | + throws DataAccessException { | |
200 | + this.doCheckTenantId(sql, tenantId); | |
201 | + return this.jdbcTemplate.queryForObject(sql, args, argTypes, rowMapper); | |
377 | 202 | } |
378 | 203 | |
379 | 204 | @Override |
380 | - public <E> E queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<E> rowMapper, Long tenantId) | |
205 | + public <E> E queryForObject(String sql, Object[] args, RowMapper<E> rowMapper, ID tenantId) | |
381 | 206 | throws DataAccessException { |
382 | - try { | |
383 | - this.loadShardingInfo(tenantId); | |
384 | - return this.jdbcTemplate.queryForObject(sql, args, argTypes, rowMapper); | |
385 | - }finally { | |
386 | - this.clearShardingInfo(); | |
387 | - } | |
207 | + this.doCheckTenantId(sql, tenantId); | |
208 | + return this.jdbcTemplate.queryForObject(sql, args, rowMapper); | |
388 | 209 | } |
389 | 210 | |
390 | 211 | @Override |
391 | - public <E> E queryForObject(String sql, Object[] args, RowMapper<E> rowMapper, Long tenantId) | |
212 | + public <E> E queryForObject(String sql, RowMapper<E> rowMapper, ID tenantId, Object... args) | |
392 | 213 | throws DataAccessException { |
393 | - try { | |
394 | - this.loadShardingInfo(tenantId); | |
395 | - return this.jdbcTemplate.queryForObject(sql, args, rowMapper); | |
396 | - }finally { | |
397 | - this.clearShardingInfo(); | |
398 | - } | |
214 | + this.doCheckTenantId(sql, tenantId); | |
215 | + return this.jdbcTemplate.queryForObject(sql, rowMapper, args); | |
399 | 216 | } |
400 | 217 | |
401 | 218 | @Override |
402 | - public <E> E queryForObject(String sql, RowMapper<E> rowMapper, Long tenantId, Object... args) | |
403 | - throws DataAccessException { | |
404 | - try { | |
405 | - this.loadShardingInfo(tenantId); | |
406 | - return this.jdbcTemplate.queryForObject(sql, rowMapper, args); | |
407 | - }finally { | |
408 | - this.clearShardingInfo(); | |
409 | - } | |
410 | - } | |
411 | - | |
412 | - @Override | |
413 | - public <E> E queryForObject(String sql, Object[] args, int[] argTypes, Class<E> requiredType, Long tenantId) | |
414 | - throws DataAccessException { | |
415 | - try { | |
416 | - this.loadShardingInfo(tenantId); | |
417 | - return this.jdbcTemplate.queryForObject(sql, args, argTypes, requiredType); | |
418 | - }finally { | |
419 | - this.clearShardingInfo(); | |
420 | - } | |
421 | - } | |
422 | - | |
423 | - @Override | |
424 | - public <E> E queryForObject(String sql, Object[] args, Class<E> requiredType, Long tenantId) | |
425 | - throws DataAccessException { | |
426 | - try { | |
427 | - this.loadShardingInfo(tenantId); | |
428 | - return this.jdbcTemplate.queryForObject(sql, args, requiredType); | |
429 | - }finally { | |
430 | - this.clearShardingInfo(); | |
431 | - } | |
432 | - } | |
433 | - | |
434 | - @Override | |
435 | - public <E> E queryForObject(String sql, Class<E> requiredType, Long tenantId, Object... args) | |
436 | - throws DataAccessException { | |
437 | - try { | |
438 | - this.loadShardingInfo(tenantId); | |
439 | - return this.jdbcTemplate.queryForObject(sql, requiredType, args); | |
440 | - }finally { | |
441 | - this.clearShardingInfo(); | |
442 | - } | |
219 | + public <E> E queryForObject(String sql, Object[] args, int[] argTypes, Class<E> requiredType, ID tenantId) | |
220 | + throws DataAccessException { | |
221 | + this.doCheckTenantId(sql, tenantId); | |
222 | + return this.jdbcTemplate.queryForObject(sql, args, argTypes, requiredType); | |
443 | 223 | } |
444 | 224 | |
445 | 225 | @Override |
446 | - public Map<String, Object> queryForMap(String sql, Object[] args, int[] argTypes, Long tenantId) | |
447 | - throws DataAccessException { | |
448 | - try { | |
449 | - this.loadShardingInfo(tenantId); | |
450 | - return this.jdbcTemplate.queryForMap(sql, args, argTypes); | |
451 | - }finally { | |
452 | - this.clearShardingInfo(); | |
453 | - } | |
226 | + public <E> E queryForObject(String sql, Object[] args, Class<E> requiredType, ID tenantId) | |
227 | + throws DataAccessException { | |
228 | + this.doCheckTenantId(sql, tenantId); | |
229 | + return this.jdbcTemplate.queryForObject(sql, args, requiredType); | |
454 | 230 | } |
455 | 231 | |
456 | 232 | @Override |
457 | - public Map<String, Object> queryForMap(String sql, Long tenantId, Object... args) throws DataAccessException { | |
458 | - try { | |
459 | - this.loadShardingInfo(tenantId); | |
460 | - return this.jdbcTemplate.queryForMap(sql, args); | |
461 | - }finally { | |
462 | - this.clearShardingInfo(); | |
463 | - } | |
233 | + public <E> E queryForObject(String sql, Class<E> requiredType, ID tenantId, Object... args) | |
234 | + throws DataAccessException { | |
235 | + this.doCheckTenantId(sql, tenantId); | |
236 | + return this.jdbcTemplate.queryForObject(sql, requiredType, args); | |
464 | 237 | } |
465 | 238 | |
466 | 239 | @Override |
467 | - public <E> List<E> queryForList(String sql, Object[] args, int[] argTypes, Class<E> elementType, Long tenantId) | |
240 | + public Map<String, Object> queryForMap(String sql, Object[] args, int[] argTypes, ID tenantId) | |
468 | 241 | throws DataAccessException { |
469 | - try { | |
470 | - this.loadShardingInfo(tenantId); | |
471 | - return this.jdbcTemplate.queryForList(sql, args, argTypes, elementType); | |
472 | - }finally { | |
473 | - this.clearShardingInfo(); | |
474 | - } | |
242 | + this.doCheckTenantId(sql, tenantId); | |
243 | + return this.jdbcTemplate.queryForMap(sql, args, argTypes); | |
475 | 244 | } |
476 | 245 | |
477 | 246 | @Override |
478 | - public <E> List<E> queryForList(String sql, Object[] args, Class<E> elementType, Long tenantId) | |
479 | - throws DataAccessException { | |
480 | - try { | |
481 | - this.loadShardingInfo(tenantId); | |
482 | - return this.jdbcTemplate.queryForList(sql, args, elementType); | |
483 | - }finally { | |
484 | - this.clearShardingInfo(); | |
485 | - } | |
247 | + public Map<String, Object> queryForMap(String sql, ID tenantId, Object... args) throws DataAccessException { | |
248 | + this.doCheckTenantId(sql, tenantId); | |
249 | + return this.jdbcTemplate.queryForMap(sql, args); | |
486 | 250 | } |
487 | 251 | |
488 | 252 | @Override |
489 | - public <E> List<E> queryForList(String sql, Class<E> elementType, Long tenantId, Object... args) | |
490 | - throws DataAccessException { | |
491 | - try { | |
492 | - this.loadShardingInfo(tenantId); | |
493 | - return this.jdbcTemplate.queryForList(sql, elementType, args); | |
494 | - }finally { | |
495 | - this.clearShardingInfo(); | |
496 | - } | |
253 | + public <E> List<E> queryForList(String sql, Object[] args, int[] argTypes, Class<E> elementType, ID tenantId) | |
254 | + throws DataAccessException { | |
255 | + this.doCheckTenantId(sql, tenantId); | |
256 | + return this.jdbcTemplate.queryForList(sql, args, argTypes, elementType); | |
497 | 257 | } |
498 | 258 | |
499 | 259 | @Override |
500 | - public List<Map<String, Object>> queryForList(String sql, Object[] args, int[] argTypes, Long tenantId) | |
501 | - throws DataAccessException { | |
502 | - try { | |
503 | - this.loadShardingInfo(tenantId); | |
504 | - return this.jdbcTemplate.queryForList(sql, args, argTypes); | |
505 | - }finally { | |
506 | - this.clearShardingInfo(); | |
507 | - } | |
260 | + public <E> List<E> queryForList(String sql, Object[] args, Class<E> elementType, ID tenantId) | |
261 | + throws DataAccessException { | |
262 | + this.doCheckTenantId(sql, tenantId); | |
263 | + return this.jdbcTemplate.queryForList(sql, args, elementType); | |
508 | 264 | } |
509 | 265 | |
510 | 266 | @Override |
511 | - public List<Map<String, Object>> queryForList(String sql, Long tenantId, Object... args) | |
267 | + public <E> List<E> queryForList(String sql, Class<E> elementType, ID tenantId, Object... args) | |
512 | 268 | throws DataAccessException { |
513 | - try { | |
514 | - this.loadShardingInfo(tenantId); | |
515 | - return this.jdbcTemplate.queryForList(sql, args); | |
516 | - }finally { | |
517 | - this.clearShardingInfo(); | |
518 | - } | |
269 | + this.doCheckTenantId(sql, tenantId); | |
270 | + return this.jdbcTemplate.queryForList(sql, elementType, args); | |
519 | 271 | } |
520 | 272 | |
521 | 273 | @Override |
522 | - public SqlRowSet queryForRowSet(String sql, Object[] args, int[] argTypes, Long tenantId) | |
274 | + public List<Map<String, Object>> queryForList(String sql, Object[] args, int[] argTypes, ID tenantId) | |
523 | 275 | throws DataAccessException { |
524 | - try { | |
525 | - this.loadShardingInfo(tenantId); | |
526 | - return this.jdbcTemplate.queryForRowSet(sql, args, argTypes); | |
527 | - }finally { | |
528 | - this.clearShardingInfo(); | |
529 | - } | |
276 | + this.doCheckTenantId(sql, tenantId); | |
277 | + return this.jdbcTemplate.queryForList(sql, args, argTypes); | |
530 | 278 | } |
531 | 279 | |
532 | 280 | @Override |
533 | - public SqlRowSet queryForRowSet(String sql, Long tenantId, Object... args) throws DataAccessException { | |
534 | - try { | |
535 | - this.loadShardingInfo(tenantId); | |
536 | - return this.jdbcTemplate.queryForRowSet(sql, args); | |
537 | - }finally { | |
538 | - this.clearShardingInfo(); | |
539 | - } | |
281 | + public List<Map<String, Object>> queryForList(String sql, ID tenantId, Object... args) | |
282 | + throws DataAccessException { | |
283 | + this.doCheckTenantId(sql, tenantId); | |
284 | + return this.jdbcTemplate.queryForList(sql, args); | |
540 | 285 | } |
541 | 286 | |
542 | 287 | @Override |
543 | - public int update(PreparedStatementCreator psc, Long tenantId) throws DataAccessException { | |
544 | - try { | |
545 | - this.loadShardingInfo(tenantId); | |
546 | - return this.jdbcTemplate.update(psc); | |
547 | - }finally { | |
548 | - this.clearShardingInfo(); | |
549 | - } | |
288 | + public SqlRowSet queryForRowSet(String sql, Object[] args, int[] argTypes, ID tenantId) | |
289 | + throws DataAccessException { | |
290 | + this.doCheckTenantId(sql, tenantId); | |
291 | + return this.jdbcTemplate.queryForRowSet(sql, args, argTypes); | |
550 | 292 | } |
551 | 293 | |
552 | 294 | @Override |
553 | - public int update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder, Long tenantId) | |
554 | - throws DataAccessException { | |
555 | - try { | |
556 | - this.loadShardingInfo(tenantId); | |
557 | - return this.jdbcTemplate.update(psc, generatedKeyHolder); | |
558 | - }finally { | |
559 | - this.clearShardingInfo(); | |
560 | - } | |
295 | + public SqlRowSet queryForRowSet(String sql, ID tenantId, Object... args) throws DataAccessException { | |
296 | + this.doCheckTenantId(sql, tenantId); | |
297 | + return this.jdbcTemplate.queryForRowSet(sql, args); | |
561 | 298 | } |
562 | 299 | |
563 | 300 | @Override |
564 | - public int update(String sql, PreparedStatementSetter pss, Long tenantId) throws DataAccessException { | |
565 | - try { | |
566 | - this.loadShardingInfo(tenantId); | |
567 | - return this.jdbcTemplate.update(sql, pss); | |
568 | - }finally { | |
569 | - this.clearShardingInfo(); | |
570 | - } | |
301 | + public int update(String sql, PreparedStatementSetter pss, ID tenantId) throws DataAccessException { | |
302 | + this.doCheckTenantId(sql, tenantId); | |
303 | + return this.jdbcTemplate.update(sql, pss); | |
571 | 304 | } |
572 | 305 | |
573 | 306 | @Override |
574 | - public int update(String sql, Object[] args, int[] argTypes, Long tenantId) throws DataAccessException { | |
575 | - try { | |
576 | - this.loadShardingInfo(tenantId); | |
577 | - return this.jdbcTemplate.update(sql, args, argTypes); | |
578 | - }finally { | |
579 | - this.clearShardingInfo(); | |
580 | - } | |
307 | + public int update(String sql, Object[] args, int[] argTypes, ID tenantId) throws DataAccessException { | |
308 | + this.doCheckTenantId(sql, tenantId); | |
309 | + return this.jdbcTemplate.update(sql, args, argTypes); | |
581 | 310 | } |
582 | 311 | |
583 | 312 | @Override |
584 | - public int update(String sql, Long tenantId, Object... args) throws DataAccessException { | |
585 | - try { | |
586 | - this.loadShardingInfo(tenantId); | |
587 | - return this.jdbcTemplate.update(sql, args); | |
588 | - }finally { | |
589 | - this.clearShardingInfo(); | |
590 | - } | |
313 | + public int update(String sql, ID tenantId, Object... args) throws DataAccessException { | |
314 | + this.doCheckTenantId(sql, tenantId); | |
315 | + return this.jdbcTemplate.update(sql, args); | |
591 | 316 | } |
592 | 317 | |
593 | 318 | @Override |
594 | - public int[] batchUpdate(String sql, BatchPreparedStatementSetter pss, Long tenantId) throws DataAccessException { | |
595 | - try { | |
596 | - this.loadShardingInfo(tenantId); | |
597 | - return this.jdbcTemplate.batchUpdate(sql, pss); | |
598 | - }finally { | |
599 | - this.clearShardingInfo(); | |
600 | - } | |
319 | + public int[] batchUpdate(String sql, BatchPreparedStatementSetter pss, ID tenantId) throws DataAccessException { | |
320 | + this.doCheckTenantId(sql, tenantId); | |
321 | + return this.jdbcTemplate.batchUpdate(sql, pss); | |
601 | 322 | } |
602 | 323 | |
603 | 324 | @Override |
604 | - public int[] batchUpdate(String sql, List<Object[]> batchArgs, Long tenantId) throws DataAccessException { | |
605 | - try { | |
606 | - this.loadShardingInfo(tenantId); | |
607 | - return this.jdbcTemplate.batchUpdate(sql, batchArgs); | |
608 | - }finally { | |
609 | - this.clearShardingInfo(); | |
610 | - } | |
325 | + public int[] batchUpdate(String sql, List<Object[]> batchArgs, ID tenantId) throws DataAccessException { | |
326 | + this.doCheckTenantId(sql, tenantId); | |
327 | + return this.jdbcTemplate.batchUpdate(sql, batchArgs); | |
611 | 328 | } |
612 | 329 | |
613 | 330 | @Override |
614 | - public int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes, Long tenantId) | |
615 | - throws DataAccessException { | |
616 | - try { | |
617 | - this.loadShardingInfo(tenantId); | |
618 | - return this.jdbcTemplate.batchUpdate(sql, batchArgs); | |
619 | - }finally { | |
620 | - this.clearShardingInfo(); | |
621 | - } | |
331 | + public int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes, ID tenantId) | |
332 | + throws DataAccessException { | |
333 | + this.doCheckTenantId(sql, tenantId); | |
334 | + return this.jdbcTemplate.batchUpdate(sql, batchArgs); | |
622 | 335 | } |
623 | 336 | |
624 | 337 | @Override |
625 | 338 | public <E> int[][] batchUpdate(String sql, Collection<E> batchArgs, int batchSize, |
626 | - ParameterizedPreparedStatementSetter<E> pss, Long tenantId) throws DataAccessException { | |
627 | - try { | |
628 | - this.loadShardingInfo(tenantId); | |
629 | - return this.jdbcTemplate.batchUpdate(sql, batchArgs, batchSize, pss); | |
630 | - }finally { | |
631 | - this.clearShardingInfo(); | |
632 | - } | |
633 | - } | |
634 | - | |
635 | - @Override | |
636 | - public <E> E execute(CallableStatementCreator csc, CallableStatementCallback<E> action, Long tenantId) | |
637 | - throws DataAccessException { | |
638 | - try { | |
639 | - this.loadShardingInfo(tenantId); | |
640 | - return this.jdbcTemplate.execute(csc, action); | |
641 | - }finally { | |
642 | - this.clearShardingInfo(); | |
643 | - } | |
644 | - } | |
645 | - | |
646 | - @Override | |
647 | - public <E> E execute(String callString, CallableStatementCallback<E> action, Long tenantId) | |
648 | - throws DataAccessException { | |
649 | - try { | |
650 | - this.loadShardingInfo(tenantId); | |
651 | - return this.jdbcTemplate.execute(callString, action); | |
652 | - }finally { | |
653 | - this.clearShardingInfo(); | |
654 | - } | |
655 | - } | |
656 | - | |
657 | - @Override | |
658 | - public Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters, Long tenantId) | |
659 | - throws DataAccessException { | |
660 | - try { | |
661 | - this.loadShardingInfo(tenantId); | |
662 | - return this.jdbcTemplate.call(csc, declaredParameters); | |
663 | - }finally { | |
664 | - this.clearShardingInfo(); | |
665 | - } | |
339 | + ParameterizedPreparedStatementSetter<E> pss, ID tenantId) throws DataAccessException { | |
340 | + this.doCheckTenantId(sql, tenantId); | |
341 | + return this.jdbcTemplate.batchUpdate(sql, batchArgs, batchSize, pss); | |
666 | 342 | } |
667 | 343 | } | ... | ... |
src/main/java/com/taover/repository/mapper/CustomJdbcTemplateRowMapper.java
1 | 1 | package com.taover.repository.mapper; |
2 | 2 | |
3 | 3 | import java.lang.reflect.Field; |
4 | -import java.lang.reflect.Method; | |
5 | 4 | import java.sql.ResultSet; |
6 | 5 | import java.sql.SQLException; |
7 | 6 | import java.util.HashMap; |
... | ... | @@ -15,10 +14,7 @@ import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSetMetaDat |
15 | 14 | public class CustomJdbcTemplateRowMapper <E> implements RowMapper<E>{ |
16 | 15 | Log log = LogFactory.getLog(this.getClass()); |
17 | 16 | |
18 | - private Map<String, Field> beanFielNameToField = new HashMap<String, Field>(); | |
19 | - private Method beforeMethod = null; | |
20 | - private Method afterMethod = null; | |
21 | - | |
17 | + private Map<String, Field> beanFielNameToField = new HashMap<String, Field>(); | |
22 | 18 | private Class<E> classInfo; |
23 | 19 | private Map<String, String> tableToBeanMap; |
24 | 20 | |
... | ... | @@ -30,36 +26,11 @@ public class CustomJdbcTemplateRowMapper <E> implements RowMapper<E>{ |
30 | 26 | for(Field item: fields) { |
31 | 27 | try { |
32 | 28 | item.setAccessible(true); |
33 | - this.beanFielNameToField.put(item.getName(), item); | |
29 | + this.beanFielNameToField.put(item.getName(), item); | |
34 | 30 | }catch (Exception e) { |
35 | 31 | log.error("set field accessible:"+e.getMessage()); |
36 | 32 | } |
37 | 33 | } |
38 | - | |
39 | - boolean hasImplementPointCut = false; | |
40 | - Class[] interfaceArr = this.classInfo.getInterfaces(); | |
41 | - for(int i=0; i<interfaceArr.length; ++i){ | |
42 | - if(interfaceArr[i].getName().equals("com.taover.repository.advice.EntityPointCut")){ | |
43 | - hasImplementPointCut = true; | |
44 | - break; | |
45 | - } | |
46 | - } | |
47 | - | |
48 | - if(hasImplementPointCut){ | |
49 | - try{ | |
50 | - beforeMethod = this.classInfo.getDeclaredMethod("before"); | |
51 | - beforeMethod.setAccessible(true); | |
52 | - }catch(Exception e){ | |
53 | - log.error("set before method exception:"+e.getMessage()); | |
54 | - } | |
55 | - | |
56 | - try{ | |
57 | - afterMethod = this.classInfo.getDeclaredMethod("after"); | |
58 | - afterMethod.setAccessible(true); | |
59 | - }catch(Exception e){ | |
60 | - log.error("set after method exception:"+e.getMessage()); | |
61 | - } | |
62 | - } | |
63 | 34 | } |
64 | 35 | |
65 | 36 | @Override |
... | ... | @@ -71,14 +42,6 @@ public class CustomJdbcTemplateRowMapper <E> implements RowMapper<E>{ |
71 | 42 | throw new RuntimeException(e); |
72 | 43 | } |
73 | 44 | |
74 | - if(this.beforeMethod != null){ | |
75 | - try{ | |
76 | - beforeMethod.invoke(targetObj); | |
77 | - }catch(Exception e){ | |
78 | - log.error("invoke before exception:"+e.getMessage()); | |
79 | - } | |
80 | - } | |
81 | - | |
82 | 45 | ResultSetWrappingSqlRowSetMetaData wapping = new ResultSetWrappingSqlRowSetMetaData(rs.getMetaData()); |
83 | 46 | int columnCount = wapping.getColumnCount(); |
84 | 47 | for (int i = 1; i<=columnCount; i++) { |
... | ... | @@ -112,14 +75,6 @@ public class CustomJdbcTemplateRowMapper <E> implements RowMapper<E>{ |
112 | 75 | } |
113 | 76 | } |
114 | 77 | |
115 | - if(this.afterMethod != null){ | |
116 | - try { | |
117 | - afterMethod.invoke(targetObj); | |
118 | - }catch (Exception e) { | |
119 | - log.error("invoke after exception:"+e.getMessage()); | |
120 | - } | |
121 | - } | |
122 | - | |
123 | 78 | return targetObj; |
124 | 79 | } |
125 | 80 | } | ... | ... |
src/main/java/com/taover/repository/shardingsphere/ShardingDatabaseAlgorithmHint.java
... | ... | @@ -1,53 +0,0 @@ |
1 | -package com.taover.repository.shardingsphere; | |
2 | - | |
3 | -import java.util.Collection; | |
4 | -import java.util.HashSet; | |
5 | -import java.util.Properties; | |
6 | -import java.util.Set; | |
7 | - | |
8 | -import org.apache.commons.logging.Log; | |
9 | -import org.apache.commons.logging.LogFactory; | |
10 | -import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingAlgorithm; | |
11 | -import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingValue; | |
12 | - | |
13 | -public class ShardingDatabaseAlgorithmHint implements HintShardingAlgorithm<String> { | |
14 | - Log log = LogFactory.getLog(ShardingDatabaseAlgorithmHint.class); | |
15 | - Properties prop = null; | |
16 | - | |
17 | - public ShardingDatabaseAlgorithmHint() { } | |
18 | - | |
19 | - @Override | |
20 | - public Collection<String> doSharding(Collection<String> availableTargetNames, HintShardingValue<String> shardingValue) { | |
21 | - Collection<String> values = shardingValue.getValues(); | |
22 | - if(values == null || values.isEmpty()) { | |
23 | - String message = shardingValue.getLogicTableName()+"已启用分库分表,SQL语句请指定tenant_id 或 使用封装好的DAO层方法"; | |
24 | - log.error(message); | |
25 | - throw new RuntimeException(message); | |
26 | - } | |
27 | - Set<String> result = new HashSet<String>(1); | |
28 | - for(String item: shardingValue.getValues()) { | |
29 | - result.add(item); | |
30 | - } | |
31 | - return result; | |
32 | - } | |
33 | - | |
34 | - @Override | |
35 | - public String getType() { | |
36 | - return "DATABASE-HINT"; | |
37 | - } | |
38 | - | |
39 | - @Override | |
40 | - public Properties getProps() { | |
41 | - return this.prop; | |
42 | - } | |
43 | - | |
44 | - @Override | |
45 | - public void setProps(Properties props) { | |
46 | - this.prop = props; | |
47 | - } | |
48 | - | |
49 | - @Override | |
50 | - public void init() { | |
51 | - System.out.print("ShardingDatabaseAlgorithmHint:init"); | |
52 | - } | |
53 | -} |
src/main/java/com/taover/repository/shardingsphere/ShardingInfoEntity.java
... | ... | @@ -1,119 +0,0 @@ |
1 | -package com.taover.repository.shardingsphere; | |
2 | - | |
3 | -import java.io.Serializable; | |
4 | -import java.math.BigDecimal; | |
5 | -import java.sql.Timestamp; | |
6 | -import java.util.Date; | |
7 | - | |
8 | -import javax.persistence.Entity; | |
9 | -import javax.persistence.Table; | |
10 | -import javax.persistence.Id; | |
11 | -import javax.persistence.Column; | |
12 | - | |
13 | -/** | |
14 | - * @version 1.0.0 | |
15 | - */ | |
16 | -@Entity | |
17 | -@Table(name="sharding_info", catalog="") | |
18 | -public class ShardingInfoEntity implements Serializable { | |
19 | - | |
20 | - private static final long serialVersionUID = 1L; | |
21 | - | |
22 | - | |
23 | - /** | |
24 | - * | |
25 | - */ | |
26 | - @Id | |
27 | - @Column(name="id") | |
28 | - private java.lang.Long id; | |
29 | - | |
30 | - public java.lang.Long getId(){ | |
31 | - return id; | |
32 | - } | |
33 | - public void setId(java.lang.Long id){ | |
34 | - this.id = id; | |
35 | - } | |
36 | - | |
37 | - /** | |
38 | - * 表名 | |
39 | - */ | |
40 | - @Column(name="table_name") | |
41 | - private java.lang.String tableName; | |
42 | - | |
43 | - public java.lang.String getTableName(){ | |
44 | - return tableName; | |
45 | - } | |
46 | - public void setTableName(java.lang.String tableName){ | |
47 | - this.tableName = tableName; | |
48 | - } | |
49 | - | |
50 | - /** | |
51 | - * 租户ID | |
52 | - */ | |
53 | - @Column(name="tenant_id") | |
54 | - private java.lang.Long tenantId; | |
55 | - | |
56 | - public java.lang.Long getTenantId(){ | |
57 | - return tenantId; | |
58 | - } | |
59 | - public void setTenantId(java.lang.Long tenantId){ | |
60 | - this.tenantId = tenantId; | |
61 | - } | |
62 | - | |
63 | - /** | |
64 | - * 表后缀 | |
65 | - */ | |
66 | - @Column(name="table_suffix") | |
67 | - private java.lang.String tableSuffix; | |
68 | - | |
69 | - public java.lang.String getTableSuffix(){ | |
70 | - return tableSuffix; | |
71 | - } | |
72 | - public void setTableSuffix(java.lang.String tableSuffix){ | |
73 | - this.tableSuffix = tableSuffix; | |
74 | - } | |
75 | - | |
76 | - /** | |
77 | - * | |
78 | - */ | |
79 | - @Column(name="create_time") | |
80 | - private java.sql.Timestamp createTime; | |
81 | - | |
82 | - public java.sql.Timestamp getCreateTime(){ | |
83 | - return createTime; | |
84 | - } | |
85 | - public void setCreateTime(java.sql.Timestamp createTime){ | |
86 | - this.createTime = createTime; | |
87 | - } | |
88 | - | |
89 | - /** | |
90 | - * | |
91 | - */ | |
92 | - @Column(name="update_time") | |
93 | - private java.sql.Timestamp updateTime; | |
94 | - | |
95 | - public java.sql.Timestamp getUpdateTime(){ | |
96 | - return updateTime; | |
97 | - } | |
98 | - public void setUpdateTime(java.sql.Timestamp updateTime){ | |
99 | - this.updateTime = updateTime; | |
100 | - } | |
101 | - | |
102 | - /** | |
103 | - * 数据库 后缀 | |
104 | - */ | |
105 | - @Column(name="ds_name") | |
106 | - private java.lang.String dsName; | |
107 | - | |
108 | - public java.lang.String getDsName(){ | |
109 | - return dsName; | |
110 | - } | |
111 | - public void setDsName(java.lang.String dsName){ | |
112 | - this.dsName = dsName; | |
113 | - } | |
114 | - | |
115 | - @Override | |
116 | - public String toString() { | |
117 | - return "ShardingInfoEntity: [id="+id+",tableName="+tableName+",tenantId="+tenantId+",tableSuffix="+tableSuffix+",createTime="+createTime+",updateTime="+updateTime+",dsName="+dsName+"]"; | |
118 | - } | |
119 | - } |
src/main/java/com/taover/repository/shardingsphere/ShardingInfoRepository.java
... | ... | @@ -1,11 +0,0 @@ |
1 | -package com.taover.repository.shardingsphere; | |
2 | - | |
3 | -import com.taover.repository.CustomJdbcTemplate; | |
4 | - | |
5 | -public class ShardingInfoRepository extends CustomJdbcTemplate<ShardingInfoEntity, Long>{ | |
6 | - | |
7 | - public ShardingInfoRepository() throws Exception { | |
8 | - super(); | |
9 | - } | |
10 | - | |
11 | -} |
src/main/java/com/taover/repository/shardingsphere/ShardingKeyGeneratorExt.java
... | ... | @@ -1,21 +0,0 @@ |
1 | -package com.taover.repository.shardingsphere; | |
2 | - | |
3 | -import java.util.List; | |
4 | - | |
5 | -import com.taover.repository.autoconfigure.ShardingSphereKeyGeneratorConfiguration; | |
6 | - | |
7 | -public interface ShardingKeyGeneratorExt { | |
8 | - /** | |
9 | - * 生成ID List | |
10 | - * @param shardingOffset 分片索引 | |
11 | - * @param genNum 生成数量 | |
12 | - * @return | |
13 | - */ | |
14 | - List<Long> generateKeyList(int genNum); | |
15 | - | |
16 | - /** | |
17 | - * 设置属性值 | |
18 | - * @param config | |
19 | - */ | |
20 | - void loalConfig(ShardingSphereKeyGeneratorConfiguration config); | |
21 | -} |
src/main/java/com/taover/repository/shardingsphere/ShardingKeyGeneratorImpl.java
... | ... | @@ -1,217 +0,0 @@ |
1 | -package com.taover.repository.shardingsphere; | |
2 | -/* | |
3 | - * Licensed to the Apache Software Foundation (ASF) under one or more | |
4 | - * contributor license agreements. See the NOTICE file distributed with | |
5 | - * this work for additional information regarding copyright ownership. | |
6 | - * The ASF licenses this file to You under the Apache License, Version 2.0 | |
7 | - * (the "License"); you may not use this file except in compliance with | |
8 | - * the License. You may obtain a copy of the License at | |
9 | - * | |
10 | - * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | - * | |
12 | - * Unless required by applicable law or agreed to in writing, software | |
13 | - * distributed under the License is distributed on an "AS IS" BASIS, | |
14 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 | - * See the License for the specific language governing permissions and | |
16 | - * limitations under the License. | |
17 | - */ | |
18 | - | |
19 | -import java.util.ArrayList; | |
20 | -import java.util.Calendar; | |
21 | -import java.util.List; | |
22 | -import java.util.Properties; | |
23 | - | |
24 | -import org.apache.shardingsphere.sharding.algorithm.keygen.TimeService; | |
25 | -import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm; | |
26 | - | |
27 | -import com.google.common.base.Preconditions; | |
28 | -import com.taover.repository.autoconfigure.ShardingSphereKeyGeneratorConfiguration; | |
29 | - | |
30 | -/** | |
31 | - * Snowflake distributed primary key generator. | |
32 | - * | |
33 | - * <p> | |
34 | - * Use snowflake algorithm. Length is 64 bit. | |
35 | - * </p> | |
36 | - * | |
37 | - * <pre> | |
38 | - * 1bit sign bit. | |
39 | - * 41bits timestamp offset from 2016.11.01(ShardingSphere distributed primary key published data) to now. | |
40 | - * 10bits worker process id. | |
41 | - * 12bits auto increment offset in one mills | |
42 | - * </pre> | |
43 | - * | |
44 | - * <p> | |
45 | - * Call @{@code SnowflakeShardingKeyGenerator.setWorkerId} to set worker id, default value is 0. | |
46 | - * </p> | |
47 | - * | |
48 | - * <p> | |
49 | - * Call @{@code SnowflakeShardingKeyGenerator.setMaxTolerateTimeDifferenceMilliseconds} to set max tolerate time difference milliseconds, default value is 0. | |
50 | - * </p> | |
51 | - */ | |
52 | -public final class ShardingKeyGeneratorImpl implements KeyGenerateAlgorithm, ShardingKeyGeneratorExt { | |
53 | - | |
54 | - public static final long EPOCH; | |
55 | - | |
56 | - private static final long SEQUENCE_BITS = 11L; | |
57 | - | |
58 | - private static final long BATCH_FLAG_BITS = 1L; | |
59 | - | |
60 | - private static final long WORKER_ID_BITS = 10L; | |
61 | - | |
62 | - private static final long SEQUENCE_MASK = (1 << SEQUENCE_BITS) - 1; | |
63 | - | |
64 | - private static final long BATCH_FLAG_LEFT_SHIFT_BITS = SEQUENCE_BITS; | |
65 | - | |
66 | - private static final long WORKER_ID_LEFT_SHIFT_BITS = BATCH_FLAG_LEFT_SHIFT_BITS + BATCH_FLAG_BITS; | |
67 | - | |
68 | - private static final long TIMESTAMP_LEFT_SHIFT_BITS = WORKER_ID_LEFT_SHIFT_BITS + WORKER_ID_BITS; | |
69 | - | |
70 | - private static final long WORKER_ID_MAX_VALUE = 1L << WORKER_ID_BITS; | |
71 | - | |
72 | - private static final long WORKER_ID = 0; | |
73 | - | |
74 | - private static final int DEFAULT_VIBRATION_VALUE = 1; | |
75 | - | |
76 | - private static final int MAX_TOLERATE_TIME_DIFFERENCE_MILLISECONDS = 10; | |
77 | - | |
78 | - private static TimeService timeService = new TimeService(); | |
79 | - | |
80 | - private Properties properties = new Properties(); | |
81 | - | |
82 | - private int sequenceOffset = -1; | |
83 | - | |
84 | - private long sequence; | |
85 | - | |
86 | - private long lastMilliseconds; | |
87 | - | |
88 | - static { | |
89 | - Calendar calendar = Calendar.getInstance(); | |
90 | - calendar.set(2016, Calendar.NOVEMBER, 1); | |
91 | - calendar.set(Calendar.HOUR_OF_DAY, 0); | |
92 | - calendar.set(Calendar.MINUTE, 0); | |
93 | - calendar.set(Calendar.SECOND, 0); | |
94 | - calendar.set(Calendar.MILLISECOND, 0); | |
95 | - EPOCH = calendar.getTimeInMillis(); | |
96 | - } | |
97 | - | |
98 | - @Override | |
99 | - public String getType() { | |
100 | - return "SNOWFLAKE-SELF"; | |
101 | - } | |
102 | - | |
103 | - @Override | |
104 | - public synchronized Comparable<?> generateKey() { | |
105 | - long currentMilliseconds = timeService.getCurrentMillis(); | |
106 | - if (waitTolerateTimeDifferenceIfNeed(currentMilliseconds)) { | |
107 | - currentMilliseconds = timeService.getCurrentMillis(); | |
108 | - } | |
109 | - if (lastMilliseconds == currentMilliseconds) { | |
110 | - if (0L == (sequence = (sequence + 1) & SEQUENCE_MASK)) { | |
111 | - currentMilliseconds = waitUntilNextTime(currentMilliseconds); | |
112 | - } | |
113 | - } else { | |
114 | - vibrateSequenceOffset(); | |
115 | - sequence = sequenceOffset; | |
116 | - } | |
117 | - lastMilliseconds = currentMilliseconds; | |
118 | - return ((currentMilliseconds - EPOCH) << TIMESTAMP_LEFT_SHIFT_BITS) | (getWorkerId() << WORKER_ID_LEFT_SHIFT_BITS) | (0 << BATCH_FLAG_LEFT_SHIFT_BITS) | sequence; | |
119 | - } | |
120 | - | |
121 | - private boolean waitTolerateTimeDifferenceIfNeed(final long currentMilliseconds) { | |
122 | - if (lastMilliseconds <= currentMilliseconds) { | |
123 | - return false; | |
124 | - } | |
125 | - long timeDifferenceMilliseconds = lastMilliseconds - currentMilliseconds; | |
126 | - Preconditions.checkState(timeDifferenceMilliseconds < getMaxTolerateTimeDifferenceMilliseconds(), | |
127 | - "Clock is moving backwards, last time is %d milliseconds, current time is %d milliseconds", lastMilliseconds, currentMilliseconds); | |
128 | - try { | |
129 | - Thread.sleep(timeDifferenceMilliseconds); | |
130 | - } catch (InterruptedException e) { | |
131 | - e.printStackTrace(); | |
132 | - } | |
133 | - return true; | |
134 | - } | |
135 | - | |
136 | - private long getWorkerId() { | |
137 | - long result = Long.valueOf(properties.getProperty("worker.id", String.valueOf(WORKER_ID))); | |
138 | - Preconditions.checkArgument(result >= 0L && result < WORKER_ID_MAX_VALUE); | |
139 | - return result; | |
140 | - } | |
141 | - | |
142 | - private int getMaxVibrationOffset() { | |
143 | - int result = Integer.parseInt(properties.getProperty("max.vibration.offset", String.valueOf(DEFAULT_VIBRATION_VALUE))); | |
144 | - Preconditions.checkArgument(result >= 0 && result <= SEQUENCE_MASK, "Illegal max vibration offset"); | |
145 | - return result; | |
146 | - } | |
147 | - | |
148 | - private int getMaxTolerateTimeDifferenceMilliseconds() { | |
149 | - return Integer.valueOf(properties.getProperty("max.tolerate.time.difference.milliseconds", String.valueOf(MAX_TOLERATE_TIME_DIFFERENCE_MILLISECONDS))); | |
150 | - } | |
151 | - | |
152 | - private long waitUntilNextTime(final long lastTime) { | |
153 | - long result = timeService.getCurrentMillis(); | |
154 | - while (result <= lastTime) { | |
155 | - result = timeService.getCurrentMillis(); | |
156 | - } | |
157 | - return result; | |
158 | - } | |
159 | - | |
160 | - private void vibrateSequenceOffset() { | |
161 | - sequenceOffset = sequenceOffset >= getMaxVibrationOffset() ? 0 : sequenceOffset + 1; | |
162 | - } | |
163 | - | |
164 | - @Override | |
165 | - public List<Long> generateKeyList(int genNum) { | |
166 | - long currentMilliseconds = timeService.getCurrentMillis(); | |
167 | - if (waitTolerateTimeDifferenceIfNeed(currentMilliseconds)) { | |
168 | - currentMilliseconds = timeService.getCurrentMillis(); | |
169 | - } | |
170 | - List<Long> data = new ArrayList<Long>(genNum); | |
171 | - for(int i=0; i<genNum; ++i) { | |
172 | - if (lastMilliseconds == currentMilliseconds) { | |
173 | - if (0L == (sequence = (sequence + 1) & SEQUENCE_MASK)) { | |
174 | - currentMilliseconds = waitUntilNextTime(currentMilliseconds); | |
175 | - } | |
176 | - } else { | |
177 | - vibrateSequenceOffset(); | |
178 | - sequence = sequenceOffset; | |
179 | - } | |
180 | - data.add(((currentMilliseconds - EPOCH) << TIMESTAMP_LEFT_SHIFT_BITS) | (getWorkerId() << WORKER_ID_LEFT_SHIFT_BITS) | (1 << BATCH_FLAG_LEFT_SHIFT_BITS) | sequence); | |
181 | - } | |
182 | - lastMilliseconds = currentMilliseconds; | |
183 | - return data; | |
184 | - } | |
185 | - | |
186 | - public static void setTimeService(TimeService timeService) { | |
187 | - ShardingKeyGeneratorImpl.timeService = timeService; | |
188 | - } | |
189 | - | |
190 | - @Override | |
191 | - public void loalConfig(ShardingSphereKeyGeneratorConfiguration config) { | |
192 | - if(config.getMaxTolerateTimeDifferenceMilliseconds() != null) { | |
193 | - properties.setProperty("max.tolerate.time.difference.milliseconds", config.getMaxTolerateTimeDifferenceMilliseconds()); | |
194 | - } | |
195 | - if(config.getWorkerId() != null) { | |
196 | - properties.setProperty("worker.id", config.getWorkerId()); | |
197 | - } | |
198 | - if(config.getMaxVibrationOffset() != null) { | |
199 | - properties.setProperty("max.vibration.offset", config.getMaxVibrationOffset()); | |
200 | - } | |
201 | - } | |
202 | - | |
203 | - @Override | |
204 | - public Properties getProps() { | |
205 | - return this.properties; | |
206 | - } | |
207 | - | |
208 | - @Override | |
209 | - public void setProps(Properties props) { | |
210 | - this.properties = props; | |
211 | - } | |
212 | - | |
213 | - @Override | |
214 | - public void init() { | |
215 | - System.out.println("ShardingKeyGeneratorImpl:init"); | |
216 | - } | |
217 | -} |
src/main/java/com/taover/repository/shardingsphere/ShardingSphereService.java
... | ... | @@ -1,87 +0,0 @@ |
1 | -package com.taover.repository.shardingsphere; | |
2 | - | |
3 | -import java.util.ArrayList; | |
4 | -import java.util.HashMap; | |
5 | -import java.util.List; | |
6 | -import java.util.Map; | |
7 | - | |
8 | -import javax.annotation.Resource; | |
9 | - | |
10 | -import org.springframework.stereotype.Service; | |
11 | - | |
12 | -import com.taover.repository.autoconfigure.ShardingSphereKeyGeneratorConfiguration; | |
13 | - | |
14 | -@Service | |
15 | -public class ShardingSphereService { | |
16 | - private Map<Long, List<ShardingInfoEntity>> CACHED_TABLE_SUFFIX_BY_TENANT = null; | |
17 | - private Map<String, List<ShardingInfoEntity>> CACHED_TABLE_SUFFIX_BY_TABLE_NAME = null; | |
18 | - private Map<String, ShardingKeyGeneratorExt> GENERATOR_HOLDER = new HashMap<String, ShardingKeyGeneratorExt>(); | |
19 | - | |
20 | - @Resource | |
21 | - private ShardingInfoRepository shardingInfoRepository; | |
22 | - private ShardingSphereKeyGeneratorConfiguration config; | |
23 | - | |
24 | - public ShardingSphereService(ShardingSphereKeyGeneratorConfiguration config) { | |
25 | - this.config = config; | |
26 | - } | |
27 | - | |
28 | - public List<Long> generateKeyList(String tableName, int number){ | |
29 | - if(!GENERATOR_HOLDER.containsKey(tableName)) { | |
30 | - loadShardingKeyGenerator(tableName); | |
31 | - } | |
32 | - return GENERATOR_HOLDER.get(tableName).generateKeyList(number); | |
33 | - } | |
34 | - | |
35 | - public List<ShardingInfoEntity> getShardingInfoByTenantId(Long tenantId) { | |
36 | - if(CACHED_TABLE_SUFFIX_BY_TENANT == null) { | |
37 | - loadCacheTableShardingInfo(); | |
38 | - } | |
39 | - return CACHED_TABLE_SUFFIX_BY_TENANT.get(tenantId); | |
40 | - } | |
41 | - | |
42 | - private synchronized void loadCacheTableShardingInfo() { | |
43 | - if(CACHED_TABLE_SUFFIX_BY_TENANT != null && CACHED_TABLE_SUFFIX_BY_TABLE_NAME != null) { | |
44 | - return; | |
45 | - } | |
46 | - List<ShardingInfoEntity> dataList = this.shardingInfoRepository.findListBySql("1=1"); | |
47 | - Map<Long, List<ShardingInfoEntity>> tempData = new HashMap<Long, List<ShardingInfoEntity>>(); | |
48 | - Map<String, List<ShardingInfoEntity>> tempDataTableName = new HashMap<String, List<ShardingInfoEntity>>(); | |
49 | - for(ShardingInfoEntity item: dataList) { | |
50 | - List<ShardingInfoEntity> tempItem = tempData.getOrDefault(item.getTenantId(), new ArrayList<ShardingInfoEntity>()); | |
51 | - tempItem.add(item); | |
52 | - tempData.put(item.getTenantId(), tempItem); | |
53 | - | |
54 | - List<ShardingInfoEntity> tempItemTableName = tempDataTableName.getOrDefault(item.getTableName(), new ArrayList<ShardingInfoEntity>()); | |
55 | - tempItemTableName.add(item); | |
56 | - tempDataTableName.put(item.getTableName(), tempItemTableName); | |
57 | - } | |
58 | - CACHED_TABLE_SUFFIX_BY_TENANT = tempData; | |
59 | - CACHED_TABLE_SUFFIX_BY_TABLE_NAME = tempDataTableName; | |
60 | - } | |
61 | - | |
62 | - private synchronized void loadShardingKeyGenerator(String tableName) { | |
63 | - if(GENERATOR_HOLDER.containsKey(tableName)) { | |
64 | - return; | |
65 | - } | |
66 | - ShardingKeyGeneratorExt generator = new ShardingKeyGeneratorImpl(); | |
67 | - generator.loalConfig(config); | |
68 | - GENERATOR_HOLDER.put(tableName, generator); | |
69 | - } | |
70 | - | |
71 | - public List<ShardingInfoEntity> getShardingInfoByTableNames(String[] broadcastTableNames) { | |
72 | - if(broadcastTableNames == null || broadcastTableNames.length == 0) { | |
73 | - return null; | |
74 | - } | |
75 | - if(CACHED_TABLE_SUFFIX_BY_TABLE_NAME == null) { | |
76 | - loadCacheTableShardingInfo(); | |
77 | - } | |
78 | - List<ShardingInfoEntity> dataList = new ArrayList<ShardingInfoEntity>(); | |
79 | - for(String item: broadcastTableNames) { | |
80 | - List<ShardingInfoEntity> tempData = CACHED_TABLE_SUFFIX_BY_TABLE_NAME.get(item); | |
81 | - if(tempData != null && !tempData.isEmpty()) { | |
82 | - dataList.addAll(tempData); | |
83 | - } | |
84 | - } | |
85 | - return dataList; | |
86 | - } | |
87 | -} |
src/main/java/com/taover/repository/shardingsphere/ShardingTableAlgorithmHint.java
... | ... | @@ -1,54 +0,0 @@ |
1 | -package com.taover.repository.shardingsphere; | |
2 | - | |
3 | -import java.util.Collection; | |
4 | -import java.util.HashSet; | |
5 | -import java.util.Properties; | |
6 | -import java.util.Set; | |
7 | - | |
8 | -import org.apache.commons.logging.Log; | |
9 | -import org.apache.commons.logging.LogFactory; | |
10 | -import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingAlgorithm; | |
11 | -import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingValue; | |
12 | - | |
13 | -public class ShardingTableAlgorithmHint implements HintShardingAlgorithm<String> { | |
14 | - Log log = LogFactory.getLog(ShardingTableAlgorithmHint.class); | |
15 | - | |
16 | - Properties prop = null; | |
17 | - | |
18 | - public ShardingTableAlgorithmHint() { } | |
19 | - | |
20 | - @Override | |
21 | - public Collection<String> doSharding(Collection<String> availableTargetNames, HintShardingValue<String> shardingValue) { | |
22 | - Collection<String> values = shardingValue.getValues(); | |
23 | - if(values == null || values.isEmpty()) { | |
24 | - String message = shardingValue.getLogicTableName()+"已启用分库分表,SQL语句请指定tenant_id 或 使用封装好的DAO层方法"; | |
25 | - log.error(message); | |
26 | - throw new RuntimeException(message); | |
27 | - } | |
28 | - Set<String> result = new HashSet<String>(availableTargetNames); | |
29 | - for(String item: values) { | |
30 | - result.add(shardingValue.getLogicTableName()+item); | |
31 | - } | |
32 | - return result; | |
33 | - } | |
34 | - | |
35 | - @Override | |
36 | - public String getType() { | |
37 | - return "TABLE-HINT"; | |
38 | - } | |
39 | - | |
40 | - @Override | |
41 | - public Properties getProps() { | |
42 | - return this.prop; | |
43 | - } | |
44 | - | |
45 | - @Override | |
46 | - public void setProps(Properties props) { | |
47 | - this.prop = props; | |
48 | - } | |
49 | - | |
50 | - @Override | |
51 | - public void init() { | |
52 | - System.out.println("ShardingTableAlgorithmHint:init"); | |
53 | - } | |
54 | -} |
src/main/java/com/taover/repository/spring/autoconfigure/TaoverRepositoryAutoConfiguration.java
0 → 100644
... | ... | @@ -0,0 +1,24 @@ |
1 | +package com.taover.repository.spring.autoconfigure; | |
2 | + | |
3 | +import javax.annotation.Resource; | |
4 | + | |
5 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; | |
6 | +import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; | |
7 | +import org.springframework.context.annotation.Bean; | |
8 | +import org.springframework.context.annotation.Configuration; | |
9 | +import org.springframework.jdbc.core.JdbcTemplate; | |
10 | + | |
11 | +import com.taover.repository.jdbctemplate.JdbcTemplateWrapperTenant; | |
12 | +import com.taover.repository.jdbctemplate.JdbcTemplateWrapperTenantImpl; | |
13 | + | |
14 | +@Configuration | |
15 | +@AutoConfigureAfter(JdbcTemplateAutoConfiguration.class) | |
16 | +public class TaoverRepositoryAutoConfiguration { | |
17 | + @Resource | |
18 | + private JdbcTemplate jdbcTemplate; | |
19 | + | |
20 | + @Bean | |
21 | + public JdbcTemplateWrapperTenant jdbcTemplateWrapperTenant() { | |
22 | + return new JdbcTemplateWrapperTenantImpl(this.jdbcTemplate); | |
23 | + } | |
24 | +} | ... | ... |
src/main/resources/META-INF/services/org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator
src/main/resources/META-INF/spring.factories
1 | -org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.taover.repository.autoconfigure.TaoverRepositoryAutoConfiguration | |
2 | 1 | \ No newline at end of file |
2 | +org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.taover.repository.spring.autoconfigure.TaoverRepositoryAutoConfiguration | |
3 | 3 | \ No newline at end of file | ... | ... |
src/test/java/com/taover/repository/test/TestAutoconfigure.java
... | ... | @@ -18,20 +18,16 @@ import org.springframework.jdbc.support.GeneratedKeyHolder; |
18 | 18 | import org.springframework.jdbc.support.KeyHolder; |
19 | 19 | import org.springframework.scheduling.annotation.EnableScheduling; |
20 | 20 | |
21 | -import com.taover.repository.jdbctemplate.JdbcTemplateBroadcast; | |
22 | 21 | import com.taover.repository.jdbctemplate.JdbcTemplateWrapperTenant; |
23 | 22 | |
24 | 23 | @SpringBootApplication |
25 | 24 | @EnableScheduling |
26 | 25 | public class TestAutoconfigure { |
27 | - private static JdbcTemplateBroadcast jdbcTemplateBroadcast; | |
28 | 26 | private static JdbcTemplateWrapperTenant jdbcTemplateWrapperTenant; |
29 | 27 | private static JdbcTemplate jdbcTemplate; |
30 | - | |
31 | - | |
28 | + | |
32 | 29 | public static void main(String args[]) { |
33 | 30 | ConfigurableApplicationContext context = SpringApplication.run(TestAutoconfigure.class, args); |
34 | - jdbcTemplateBroadcast = context.getBean(JdbcTemplateBroadcast.class); | |
35 | 31 | jdbcTemplateWrapperTenant = context.getBean(JdbcTemplateWrapperTenant.class); |
36 | 32 | jdbcTemplate = context.getBean(JdbcTemplate.class); |
37 | 33 | |
... | ... | @@ -67,7 +63,7 @@ public class TestAutoconfigure { |
67 | 63 | |
68 | 64 | private static void testSimpleQuery() { |
69 | 65 | String sql = "select id from demo limit 1; "; |
70 | - System.out.println(jdbcTemplate.queryForObject(sql, BigDecimal.class)); | |
66 | + System.out.println(jdbcTemplateWrapperTenant.queryForObject(sql, BigDecimal.class, null)); | |
71 | 67 | } |
72 | 68 | |
73 | 69 | private static void testExpressSql() { |
... | ... | @@ -502,7 +498,6 @@ public class TestAutoconfigure { |
502 | 498 | } |
503 | 499 | |
504 | 500 | public static void testBroadCast() { |
505 | - System.out.println(jdbcTemplateBroadcast.queryForObject("select id from wxorder_order order by id desc limit 5 ", String.class, new String[]{"wxorder_order"})); | |
506 | 501 | System.out.println(jdbcTemplateWrapperTenant.queryForObject("select id from wxorder_order limit 1", Long.class, 1L)); |
507 | 502 | System.out.println(jdbcTemplateWrapperTenant.queryForList("select * from wxorder_order limit 1", 1L)); |
508 | 503 | System.out.println(jdbcTemplateWrapperTenant.queryForList("select * from wxorder_order limit 1", 2L)); | ... | ... |
src/test/java/com/taover/repository/test/TestMapper.java
... | ... | @@ -7,6 +7,8 @@ import javax.sql.DataSource; |
7 | 7 | import org.springframework.jdbc.core.JdbcTemplate; |
8 | 8 | |
9 | 9 | import com.alibaba.druid.pool.DruidDataSourceFactory; |
10 | +import com.taover.repository.jdbctemplate.JdbcTemplateWrapperTenant; | |
11 | +import com.taover.repository.jdbctemplate.JdbcTemplateWrapperTenantImpl; | |
10 | 12 | import com.taover.repository.test.repository.CommonRegionRepository; |
11 | 13 | |
12 | 14 | public class TestMapper { |
... | ... | @@ -20,10 +22,10 @@ public class TestMapper { |
20 | 22 | DataSource ds = null; |
21 | 23 | try { |
22 | 24 | ds = new DruidDataSourceFactory().createDataSource(properties); |
23 | - JdbcTemplate template = new JdbcTemplate(ds); | |
25 | + JdbcTemplateWrapperTenant template = new JdbcTemplateWrapperTenantImpl(new JdbcTemplate(ds)); | |
24 | 26 | //System.out.println(template.queryForObject("select count(*) from wxorder_order", Integer.class)); |
25 | 27 | CommonRegionRepository repo = new CommonRegionRepository(template); |
26 | - System.out.println(repo.findListBySql("1=1")); | |
28 | + System.out.println(repo.findListBySql("1=1", null)); | |
27 | 29 | } catch (Exception e) { |
28 | 30 | // TODO Auto-generated catch block |
29 | 31 | e.printStackTrace(); | ... | ... |
src/test/java/com/taover/repository/test/repository/CommonRegionRepository.java
1 | 1 | package com.taover.repository.test.repository; |
2 | 2 | |
3 | -import org.springframework.jdbc.core.JdbcTemplate; | |
3 | +import com.taover.repository.CustomJdbcTemplateWrapperTenant; | |
4 | +import com.taover.repository.jdbctemplate.JdbcTemplateWrapperTenant; | |
4 | 5 | |
5 | -import com.taover.repository.CustomJdbcTemplate; | |
6 | - | |
7 | -public class CommonRegionRepository extends CustomJdbcTemplate<CommonRegionEntity, Long>{ | |
6 | +public class CommonRegionRepository extends CustomJdbcTemplateWrapperTenant<CommonRegionEntity, Long>{ | |
8 | 7 | public static int TYPE_PROVINCE = 1; |
9 | 8 | public static int TYPE_CITY = 2; |
10 | 9 | public static int TYPE_DISTRICT = 3; |
11 | 10 | public static Long PROVINCE_PARENT_ID = 1L; |
12 | 11 | |
13 | - public CommonRegionRepository(JdbcTemplate jdbcTemplate) throws Exception { | |
12 | + public CommonRegionRepository(JdbcTemplateWrapperTenant jdbcTemplate) throws Exception { | |
14 | 13 | super(jdbcTemplate); |
15 | 14 | } |
16 | 15 | } | ... | ... |
src/test/resources/META-INF/services/org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator
src/test/resources/META-INF/spring.factories
1 | -org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.taover.repository.autoconfigure.RepositoryAutoConfiguration | |
2 | 1 | \ No newline at end of file |
2 | +org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.taover.repository.spring.autoconfigure.TaoverRepositoryAutoConfiguration | |
3 | 3 | \ No newline at end of file | ... | ... |
src/test/resources/application.properties
1 | 1 | |
2 | -server.port=80 | |
3 | -server.session.timeout=3600 | |
4 | - | |
5 | -spring.servlet.multipart.max-file-size=10MB | |
6 | -spring.servlet.multipart.max-request-size=20MB | |
7 | - | |
8 | -spring.jackson.time-zone=GMT+8 | |
9 | -spring.jackson.date-format=yyyy-MM-dd HH:mm:ss | |
10 | -spring.mvc.dateFormat = yyyy-MM-dd HH:mm:ss | |
11 | -spring.gson.serialize-nulls=true | |
12 | - | |
13 | -spring.mvc.dispatch-options-request=true | |
14 | - | |
15 | -# shardingspere | |
16 | -spring.shardingsphere.datasource.names=ds0 | |
17 | -spring.shardingsphere.datasource.common.type=com.alibaba.druid.pool.DruidDataSource | |
18 | -spring.shardingsphere.datasource.common.driver-class-name=com.mysql.cj.jdbc.Driver | |
19 | -spring.shardingsphere.datasource.ds0.url=jdbc:mysql://rdsifmezqifmezqo.mysql.rds.aliyuncs.com:3306/bzyun_wxorder?characterEncoding=UTF-8 | |
20 | -spring.shardingsphere.datasource.ds0.username=tylife | |
21 | -spring.shardingsphere.datasource.ds0.password=lexi365 | |
22 | -spring.shardingsphere.datasource.ds0.initial-size=10 | |
23 | -spring.shardingsphere.datasource.ds0.max-active=20 | |
24 | -spring.shardingsphere.datasource.ds0.min-idle=5 | |
25 | -spring.shardingsphere.datasource.ds0.max-wait=60000 | |
26 | - | |
27 | -spring.shardingsphere.datasource.ds1.url=jdbc:mysql://121.42.142.102:3306/bzyun_wxorder?characterEncoding=UTF-8 | |
28 | -spring.shardingsphere.datasource.ds1.username=dev | |
29 | -spring.shardingsphere.datasource.ds1.password=taover02 | |
30 | -spring.shardingsphere.datasource.ds1.initial-size=10 | |
31 | -spring.shardingsphere.datasource.ds1.max-active=20 | |
32 | -spring.shardingsphere.datasource.ds1.min-idle=5 | |
33 | -spring.shardingsphere.datasource.ds1.max-wait=60000 | |
34 | - | |
35 | -#spring.shardingsphere.rules.sharding.tables.wxorder_order.actual-data-nodes=ds0.wxorder_order | |
36 | -#spring.shardingsphere.rules.sharding.tables.wxorder_order.database-strategy.hint.database_hint.sharding-algorithm-name=database_hint | |
37 | -#spring.shardingsphere.rules.sharding.tables.wxorder_order.table-strategy.hint.table_hint.sharding-algorithm-name=table_hint | |
38 | -#spring.shardingsphere.rules.sharding.tables.wxorder_order.key-generate-strategy.column=id | |
39 | -#spring.shardingsphere.rules.sharding.tables.wxorder_order.key-generate-strategy.key-generator-name=SNOWFLAKE-SELF | |
40 | - | |
41 | -#spring.shardingsphere.rules.sharding.sharding-algorithms.database_hint.type=HINT | |
42 | -#spring.shardingsphere.rules.sharding.sharding-algorithms.database_hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingDatabaseAlgorithmHint | |
43 | -#spring.shardingsphere.rules.sharding.sharding-algorithms.table_hint.type=HINT | |
44 | -#spring.shardingsphere.rules.sharding.sharding-algorithms.table_hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingTableAlgorithmHint | |
45 | - | |
46 | -taover.sharding.workerId=1 | |
47 | -taover.sharding.maxVibrationOffset=3 | |
48 | -taover.sharding.maxTolerateTimeDifferenceMilliseconds=1000 | |
49 | - | |
50 | -#spring.shardingsphere.sharding.default-key-generator.key-generator.column=id | |
51 | -#spring.shardingsphere.sharding.default-key-generator.type=SNOWFLAKE-SELF | |
52 | -#spring.shardingsphere.sharding.default-key-generator.props.worker.id=1 | |
53 | -#spring.shardingsphere.sharding.default-key-generator.props.max.vibration.offset=3 | |
54 | -#spring.shardingsphere.sharding.default-key-generator.props.max.tolerate.time.difference.milliseconds=1000 | |
2 | +#DB info | |
3 | +spring.datasource.url=jdbc:mysql://rdsifmezqifmezqo.mysql.rds.aliyuncs.com:3306/bzyun_wxorder?characterEncoding=UTF8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=GMT%2B8 | |
4 | +spring.datasource.username=tylife | |
5 | +spring.datasource.password=lexi365 | |
6 | +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | |
7 | + | |
8 | +spring.datasource.druid.initial-size=10 | |
9 | +spring.datasource.druid.max-active=20 | |
10 | +spring.datasource.druid.min-idle=5 | |
11 | +spring.datasource.druid.max-wait=60000 | ... | ... |