Commit 486880e3e2b2221ea7f7b93659f6076e9bf9b547
1 parent
12322a42
Exists in
master
and in
2 other branches
optimized
Showing
3 changed files
with
49 additions
and
79 deletions
Show diff stats
build.gradle
src/main/java/com/taover/repository/CustomJdbcTemplateWrapperTenant.java
... | ... | @@ -296,9 +296,8 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
296 | 296 | } |
297 | 297 | |
298 | 298 | @Override |
299 | - public T findEntityByCondition(List<Object[]> condition) throws NotFoundException, MultiRowException, NoContainTenantException { | |
300 | - this.checkTenantInfoFromCondition(condition); | |
301 | - List<T> tempList = findListByCondition(condition); | |
299 | + public T findEntityByCondition(List<Object[]> condition, Long tenantId) throws NotFoundException, MultiRowException, NoContainTenantException { | |
300 | + List<T> tempList = findListByCondition(condition, tenantId); | |
302 | 301 | if(tempList == null || tempList.size() == 0){ |
303 | 302 | throw new NotFoundException(); |
304 | 303 | } |
... | ... | @@ -309,22 +308,9 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
309 | 308 | } |
310 | 309 | } |
311 | 310 | |
312 | - private void checkTenantInfoFromCondition(List<Object[]> condition) throws NoContainTenantException{ | |
313 | - for(Object[] item: condition) { | |
314 | - if(item == null || item.length == 0) { | |
315 | - continue; | |
316 | - } | |
317 | - if(item[0].toString().contains("tenant_id")) { | |
318 | - return; | |
319 | - } | |
320 | - } | |
321 | - throw new NoContainTenantException(); | |
322 | - } | |
323 | - | |
324 | 311 | @Override |
325 | - public T findEntityBySql(String sqlCondition) throws NotFoundException, MultiRowException, NoContainTenantException { | |
326 | - this.checkTenantInfoFromSql(sqlCondition); | |
327 | - List<T> tempList = findListBySql(sqlCondition); | |
312 | + public T findEntityBySql(String sqlCondition, Long tenantId) throws NotFoundException, MultiRowException { | |
313 | + List<T> tempList = findListBySql(sqlCondition, tenantId); | |
328 | 314 | if(tempList == null || tempList.size() == 0){ |
329 | 315 | throw new NotFoundException(); |
330 | 316 | } |
... | ... | @@ -334,53 +320,41 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
334 | 320 | throw new MultiRowException(); |
335 | 321 | } |
336 | 322 | } |
337 | - | |
338 | - private void checkTenantInfoFromSql(String sqlCondition) throws NoContainTenantException{ | |
339 | - if(sqlCondition.contains("tenant_id")) { | |
340 | - return; | |
341 | - }else { | |
342 | - throw new NoContainTenantException(); | |
343 | - } | |
344 | - } | |
345 | - | |
323 | + | |
346 | 324 | @Override |
347 | - public List<T> findListByCondition(List<Object[]> condition) throws NoContainTenantException { | |
348 | - return findListByCondition(condition, null); | |
325 | + public List<T> findListByCondition(List<Object[]> condition, Long tenantId){ | |
326 | + return findListByCondition(condition, null, tenantId); | |
349 | 327 | } |
350 | 328 | |
351 | 329 | @Override |
352 | - public List<T> findListByCondition(List<Object[]> condition, String sortCondition) throws NoContainTenantException { | |
353 | - this.checkTenantInfoFromCondition(condition); | |
330 | + public List<T> findListByCondition(List<Object[]> condition, String sortCondition, Long tenantId){ | |
354 | 331 | StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); |
355 | - StringBuffer pql = new StringBuffer(sql.toString()); | |
356 | 332 | List<Object> list = new ArrayList<Object>(); |
357 | - this.appendWhereCondition(sql, pql, list, condition); | |
333 | + condition.add(new Object[] {"tenant_id", "=", tenantId}); | |
334 | + this.appendWhereCondition(sql, new StringBuffer(), list, condition); | |
358 | 335 | if(sortCondition != null && !sortCondition.equals("")){ |
359 | 336 | sql.append(" " + sortCondition + " "); |
360 | - pql.append(" " + sortCondition + " "); | |
361 | 337 | } |
362 | 338 | return (List<T>)jdbcTemplateWrite.query(sql.toString(), this.customJdbcTemplateRowMapper, list.toArray()); |
363 | 339 | } |
364 | 340 | |
365 | 341 | @Override |
366 | - public List<T> findListBySql(String sqlCondition) throws NoContainTenantException { | |
367 | - this.checkTenantInfoFromSql(sqlCondition); | |
368 | - StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
342 | + public List<T> findListBySql(String sqlCondition, Long tenantId){ | |
343 | + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE tenant_id="+tenantId + " and " + sqlCondition); | |
369 | 344 | return (List<T>)jdbcTemplateWrite.query(sql.toString(), this.customJdbcTemplateRowMapper); |
370 | 345 | } |
371 | 346 | |
372 | 347 | @Override |
373 | - public Map<String, Object> findPageByCondition(List<Object[]> condition, int page, int pageSize) throws NoContainTenantException { | |
374 | - return findPageByCondition(condition, null , page, pageSize); | |
348 | + public Map<String, Object> findPageByCondition(List<Object[]> condition, int page, int pageSize, Long tenantId){ | |
349 | + return findPageByCondition(condition, null , page, pageSize, tenantId); | |
375 | 350 | } |
376 | 351 | |
377 | 352 | @Override |
378 | - public Map<String, Object> findPageByCondition(List<Object[]> condition, String sortCondition, int page, int pageSize) throws NoContainTenantException { | |
379 | - this.checkTenantInfoFromCondition(condition); | |
353 | + public Map<String, Object> findPageByCondition(List<Object[]> condition, String sortCondition, int page, int pageSize, Long tenantId){ | |
380 | 354 | StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); |
381 | 355 | StringBuffer pql = new StringBuffer(sql.toString()); |
382 | 356 | StringBuffer sqlCount = new StringBuffer("SELECT COUNT(1) rowCount FROM "+this.getTableSql()); |
383 | - | |
357 | + condition.add(new Object[] {"tenant_id", "=", tenantId}); | |
384 | 358 | List<Object> count_list = new ArrayList<Object>(); |
385 | 359 | List<Object> page_list = new ArrayList<Object>(); |
386 | 360 | this.appendWhereConditionForCount(sqlCount, condition); |
... | ... | @@ -404,10 +378,9 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
404 | 378 | } |
405 | 379 | |
406 | 380 | @Override |
407 | - public Map<String, Object> findPageBySql(String sqlCondition, int page, int pageSize) throws NoContainTenantException { | |
408 | - this.checkTenantInfoFromSql(sqlCondition); | |
409 | - StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition ); | |
410 | - StringBuffer sqlCount = new StringBuffer("SELECT count(1) rowCount FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
381 | + public Map<String, Object> findPageBySql(String sqlCondition, int page, int pageSize, Long tenantId){ | |
382 | + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE tenant_id=" + tenantId + " and " + sqlCondition ); | |
383 | + StringBuffer sqlCount = new StringBuffer("SELECT count(1) rowCount FROM "+this.getTableSql()+" WHERE tenant_id=" + tenantId + " and " + sqlCondition); | |
411 | 384 | sql.append(" limit ?, ?"); |
412 | 385 | List<Object> page_list = new ArrayList<Object>(); |
413 | 386 | page_list.add((page - 1) * pageSize); |
... | ... | @@ -498,35 +471,34 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
498 | 471 | } |
499 | 472 | |
500 | 473 | @Override |
501 | - public int deleteEntityByCondition(List<Object[]> condition) throws NoContainTenantException { | |
474 | + public int deleteEntityByCondition(List<Object[]> condition, Long tenantId){ | |
502 | 475 | if (null == condition || condition.size() == 0) { |
503 | 476 | throw new RuntimeException("params[condition] is empty"); |
504 | 477 | } |
505 | - this.checkTenantInfoFromCondition(condition); | |
506 | 478 | |
507 | 479 | List<Object> list = new ArrayList<Object>(); |
508 | 480 | StringBuffer sql = new StringBuffer("DELETE FROM "+this.getTableSql()+""); |
509 | 481 | StringBuffer pql = new StringBuffer(sql.toString()); |
482 | + condition.add(new Object[] {"tenant_id", "=", tenantId}); | |
510 | 483 | this.appendWhereCondition(sql, pql, list, condition); |
511 | 484 | return jdbcTemplateWrite.update( sql.toString(), list.toArray()); |
512 | 485 | } |
513 | 486 | |
514 | 487 | @Override |
515 | - public int deleteEntityBySql(String sqlCondition) throws NoContainTenantException { | |
488 | + public int deleteEntityBySql(String sqlCondition, Long tenantId){ | |
516 | 489 | if("".equals(sqlCondition.trim())) { |
517 | 490 | throw new RuntimeException("params[sqlCondition] is empty"); |
518 | 491 | } |
519 | - this.checkTenantInfoFromSql(sqlCondition); | |
520 | - return jdbcTemplateWrite.update( "DELETE FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
492 | + return jdbcTemplateWrite.update( "DELETE FROM "+this.getTableSql()+" WHERE tenant_id="+tenantId + " and " + sqlCondition); | |
521 | 493 | } |
522 | 494 | |
523 | 495 | @Override |
524 | - public int deleteEntityList(List<ID> idList, Long tenantId) throws NoContainTenantException { | |
496 | + public int deleteEntityList(List<ID> idList, Long tenantId){ | |
525 | 497 | StringBuffer idSb = new StringBuffer(); |
526 | 498 | for(ID id: idList) { |
527 | 499 | idSb.append(id); |
528 | 500 | } |
529 | - return this.deleteEntityBySql(this.idTableFieldName + " in ("+idSb.toString().substring(0, idSb.length()-1)+") and tenant_id="+tenantId); | |
501 | + return this.deleteEntityBySql(this.idTableFieldName + " in ("+idSb.toString().substring(0, idSb.length()-1)+") and tenant_id="+tenantId, tenantId); | |
530 | 502 | } |
531 | 503 | |
532 | 504 | @Override |
... | ... | @@ -550,20 +522,20 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
550 | 522 | } |
551 | 523 | |
552 | 524 | @Override |
553 | - public int updateEntityByCondition(List<Object[]> updateObj, List<Object[]> condition) throws NoContainTenantException { | |
525 | + public int updateEntityByCondition(List<Object[]> updateObj, List<Object[]> condition, Long tenantId){ | |
554 | 526 | if (null == updateObj || updateObj.size() == 0) { |
555 | 527 | throw new RuntimeException("params[updateObj] is empty"); |
556 | 528 | } |
557 | 529 | if (null == condition || condition.size() == 0) { |
558 | 530 | throw new RuntimeException("params[condition] is empty"); |
559 | 531 | } |
560 | - this.checkTenantInfoFromCondition(condition); | |
561 | 532 | |
562 | 533 | StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); |
563 | 534 | StringBuffer pql = new StringBuffer(sql.toString()); |
564 | 535 | List<Object> list = new ArrayList<Object>(); |
565 | 536 | this.appendSetSql(sql, pql, list, updateObj); |
566 | 537 | |
538 | + condition.add(new Object[] {"tenant_id", "=", tenantId}); | |
567 | 539 | StringBuffer where = new StringBuffer(""); |
568 | 540 | StringBuffer pwhere = new StringBuffer(""); |
569 | 541 | this.appendWhereCondition(where, pwhere, list, condition); |
... | ... | @@ -573,38 +545,36 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
573 | 545 | } |
574 | 546 | |
575 | 547 | @Override |
576 | - public int updateEntityBySql(List<Object[]> updateObj, String sqlCondition) throws NoContainTenantException { | |
548 | + public int updateEntityBySql(List<Object[]> updateObj, String sqlCondition, Long tenantId){ | |
577 | 549 | if (null == updateObj || updateObj.size() == 0) { |
578 | 550 | throw new RuntimeException("params[updateObj] is empty"); |
579 | 551 | } |
580 | 552 | if ("".equals(sqlCondition)) { |
581 | 553 | throw new RuntimeException("params[sqlCondition] is empty"); |
582 | 554 | } |
583 | - this.checkTenantInfoFromSql(sqlCondition); | |
584 | 555 | |
585 | 556 | StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); |
586 | 557 | StringBuffer pql = new StringBuffer(sql.toString()); |
587 | 558 | List<Object> list = new ArrayList<Object>(); |
588 | 559 | this.appendSetSql(sql, pql, list, updateObj); |
589 | 560 | |
590 | - String updateSql = sql.toString().substring(0, sql.length()-1) + " WHERE "+sqlCondition; | |
561 | + String updateSql = sql.toString().substring(0, sql.length()-1) + " WHERE tenant_id="+tenantId+" and "+sqlCondition; | |
591 | 562 | return jdbcTemplateWrite.update(updateSql, list.toArray()); |
592 | 563 | } |
593 | 564 | |
594 | 565 | @Override |
595 | - public Map<String, Object> getPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize) throws NoContainTenantException { | |
596 | - this.checkTenantInfoFromSql(coreSql); | |
566 | + public Map<String, Object> getPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize, Long tenantId){ | |
597 | 567 | try { |
598 | 568 | String[] splitedSql = UtilsSql.splitCoreSql(coreSql); |
599 | - return this.getPageData(splitedSql[0], splitedSql[1], orderByPartSql, page, pageSize); | |
569 | + return this.getPageData(splitedSql[0], splitedSql[1], orderByPartSql, page, pageSize, tenantId); | |
600 | 570 | }catch (Exception e) { |
601 | 571 | return UtilsSql.createPage(page, pageSize, 0, new ArrayList<Object>()); |
602 | 572 | } |
603 | 573 | } |
604 | 574 | |
605 | 575 | @Override |
606 | - public Map<String, Object> getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize) throws NoContainTenantException { | |
607 | - this.checkTenantInfoFromSql(fromAndWhereSql); | |
576 | + public Map<String, Object> getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize, Long tenantId){ | |
577 | + fromAndWhereSql = fromAndWhereSql + " and tenant_id="+tenantId; | |
608 | 578 | |
609 | 579 | //构造查询语句 |
610 | 580 | String querySql = selectSql+" "+fromAndWhereSql+" "+orderByPartSql+" "+UtilsSql.getLimitCondition(page, pageSize); | ... | ... |
src/main/java/com/taover/repository/JdbcRepositoryWrapperTenant.java
... | ... | @@ -27,40 +27,40 @@ public interface JdbcRepositoryWrapperTenant<T, ID extends Serializable> { |
27 | 27 | * Object[]数组长度是3 |
28 | 28 | * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 |
29 | 29 | */ |
30 | - public T findEntityByCondition(List<Object[]> condition) throws NotFoundException,MultiRowException,NoContainTenantException; | |
30 | + public T findEntityByCondition(List<Object[]> condition, Long tenantId) throws NotFoundException,MultiRowException,NoContainTenantException; | |
31 | 31 | |
32 | 32 | /** |
33 | 33 | * 根据条件sql查询 |
34 | 34 | * sqlCondition 为where 后面的条件。 |
35 | 35 | */ |
36 | - public T findEntityBySql(String sqlCondition) throws NotFoundException,MultiRowException,NoContainTenantException; | |
36 | + public T findEntityBySql(String sqlCondition, Long tenantId) throws NotFoundException,MultiRowException,NoContainTenantException; | |
37 | 37 | |
38 | 38 | /** |
39 | 39 | * 根据条件List<Object[]>查询 |
40 | 40 | * Object[]数组长度是3 |
41 | 41 | * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 |
42 | 42 | */ |
43 | - public List<T> findListByCondition(List<Object[]> condition) throws NoContainTenantException; | |
43 | + public List<T> findListByCondition(List<Object[]> condition, Long tenantId); | |
44 | 44 | |
45 | 45 | /** |
46 | 46 | * 根据条件List<Object[]>查询 |
47 | 47 | * Object[]数组长度是3 |
48 | 48 | * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 |
49 | 49 | */ |
50 | - public List<T> findListByCondition(List<Object[]> condition, String sortCondition) throws NoContainTenantException; | |
50 | + public List<T> findListByCondition(List<Object[]> condition, String sortCondition, Long tenantId); | |
51 | 51 | |
52 | 52 | /** |
53 | 53 | * 根据条件sql查询 |
54 | 54 | * sqlCondition 为where 后面的条件。 |
55 | 55 | */ |
56 | - public List<T> findListBySql(String sqlCondition) throws NoContainTenantException; | |
56 | + public List<T> findListBySql(String sqlCondition, Long tenantId); | |
57 | 57 | |
58 | 58 | /** |
59 | 59 | * 按条件分页查询 |
60 | 60 | * Object[]数组长度是3 |
61 | 61 | * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 |
62 | 62 | */ |
63 | - public Map<String, Object> findPageByCondition(List<Object[]> condition, int page, int pageSize) throws NoContainTenantException; | |
63 | + public Map<String, Object> findPageByCondition(List<Object[]> condition, int page, int pageSize, Long tenantId); | |
64 | 64 | |
65 | 65 | /** |
66 | 66 | * 按条件分页查询 |
... | ... | @@ -69,12 +69,12 @@ public interface JdbcRepositoryWrapperTenant<T, ID extends Serializable> { |
69 | 69 | * boolean isUseCache, 是否用缓存,默认用。 |
70 | 70 | * boolean isAddCache, 是否添加缓存,默认添加。 |
71 | 71 | */ |
72 | - public Map<String, Object> findPageByCondition(List<Object[]> condition, String sortCondition, int page, int pageSize) throws NoContainTenantException; | |
72 | + public Map<String, Object> findPageByCondition(List<Object[]> condition, String sortCondition, int page, int pageSize, Long tenantId); | |
73 | 73 | |
74 | 74 | /** |
75 | 75 | * 按sql分页查询, sqlCondition为where 后条件sql |
76 | 76 | */ |
77 | - public Map<String, Object> findPageBySql(String sqlCondition, int page, int pageSize) throws NoContainTenantException; | |
77 | + public Map<String, Object> findPageBySql(String sqlCondition, int page, int pageSize, Long tenantId); | |
78 | 78 | |
79 | 79 | /** |
80 | 80 | * 添加 |
... | ... | @@ -97,20 +97,20 @@ public interface JdbcRepositoryWrapperTenant<T, ID extends Serializable> { |
97 | 97 | * Object[]数组长度是3 |
98 | 98 | * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 |
99 | 99 | */ |
100 | - public int deleteEntityByCondition(List<Object[]> condition) throws NoContainTenantException; | |
100 | + public int deleteEntityByCondition(List<Object[]> condition, Long tenantId); | |
101 | 101 | |
102 | 102 | /** |
103 | 103 | * 删除按condition条件 |
104 | 104 | * 建议使用deleteTByCondition(List<Object[]> condition), 如果removeTByCondition(List<Object[]> condition)满足不了where条件可以使用此方法。 |
105 | 105 | * condition为where后面的条件,condition不能为空。 |
106 | 106 | */ |
107 | - public int deleteEntityBySql(String sqlCondition) throws NoContainTenantException; | |
107 | + public int deleteEntityBySql(String sqlCondition, Long tenantId); | |
108 | 108 | |
109 | 109 | /** |
110 | 110 | * 根据list对象逐个删除。 |
111 | 111 | * @throws NoContainTenantException |
112 | 112 | */ |
113 | - public int deleteEntityList(List<ID> idList, Long tenantId) throws NoContainTenantException; | |
113 | + public int deleteEntityList(List<ID> idList, Long tenantId); | |
114 | 114 | |
115 | 115 | /** |
116 | 116 | * 根据ID修改指定的值 |
... | ... | @@ -121,13 +121,13 @@ public interface JdbcRepositoryWrapperTenant<T, ID extends Serializable> { |
121 | 121 | * List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 |
122 | 122 | * List<Object[]> condition 修改的条件, 数组长度是3, 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 |
123 | 123 | */ |
124 | - public int updateEntityByCondition(List<Object[]> updateObj, List<Object[]> condition) throws NoContainTenantException; | |
124 | + public int updateEntityByCondition(List<Object[]> updateObj, List<Object[]> condition, Long tenantId); | |
125 | 125 | |
126 | 126 | /** |
127 | 127 | * List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 |
128 | 128 | * String sqlCondition 修改的条件。 |
129 | 129 | */ |
130 | - public int updateEntityBySql(List<Object[]> updateObj, String sqlCondition) throws NoContainTenantException; | |
130 | + public int updateEntityBySql(List<Object[]> updateObj, String sqlCondition, Long tenantId); | |
131 | 131 | |
132 | 132 | /** |
133 | 133 | * 获取分页数据 |
... | ... | @@ -137,7 +137,7 @@ public interface JdbcRepositoryWrapperTenant<T, ID extends Serializable> { |
137 | 137 | * @param pageSize |
138 | 138 | * @return |
139 | 139 | */ |
140 | - public Map<String, Object> getPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize) throws NoContainTenantException; | |
140 | + public Map<String, Object> getPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize, Long tenantId); | |
141 | 141 | |
142 | 142 | /** |
143 | 143 | * 获取分页数据 |
... | ... | @@ -148,5 +148,5 @@ public interface JdbcRepositoryWrapperTenant<T, ID extends Serializable> { |
148 | 148 | * @param pageSize |
149 | 149 | * @return |
150 | 150 | */ |
151 | - public Map<String, Object> getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize) throws NoContainTenantException; | |
151 | + public Map<String, Object> getPageData(String selectSql, String fromAndWhereSql, String orderByPartSql, Integer page, Integer pageSize, Long tenantId); | |
152 | 152 | } | ... | ... |