Commit 085176f3a6f75f5a780b62fdd4ac8346632fbc55
1 parent
460ec6a3
Exists in
master
and in
2 other branches
1.add find by class methods
Showing
2 changed files
with
184 additions
and
1 deletions
Show diff stats
build.gradle
... | ... | @@ -19,12 +19,15 @@ mainClassName = 'com.taover.repository.UtilsString' |
19 | 19 | |
20 | 20 | dependencies { |
21 | 21 | compile("org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final") |
22 | - compile('org.springframework:spring-jdbc:5.1.9.RELEAS') | |
22 | + compile('org.springframework:spring-jdbc:5.1.9.RELEASE') | |
23 | 23 | compile('mysql:mysql-connector-java:5.1.47') |
24 | 24 | } |
25 | 25 | |
26 | 26 | repositories { |
27 | 27 | jcenter() |
28 | + maven{ url 'http://repository.sonatype.org/content/groups/public/' } | |
29 | + maven{ url 'https://repository.jboss.org/nexus/content/groups/public/' } | |
30 | + maven{ url 'http://47.93.122.69:9001/repository/maven-releases/' } | |
28 | 31 | } |
29 | 32 | |
30 | 33 | task sourcesJar(type: Jar, dependsOn: classes) { | ... | ... |
src/main/java/com/taover/repository/CustomJdbcTemplate.java
... | ... | @@ -622,4 +622,184 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
622 | 622 | countData = this.jdbcTemplateRead.queryForMap(countSql); |
623 | 623 | return UtilsSql.createPage(page, Integer.valueOf(countData.get("rows").toString()), queryData); |
624 | 624 | } |
625 | + | |
626 | + | |
627 | + /** | |
628 | + * 按主键查询 | |
629 | + */ | |
630 | + public <E> E findBeanByID(ID id, Class<E> beanClass) { | |
631 | + return findBeanByID(id, true, false, beanClass); | |
632 | + } | |
633 | + | |
634 | + /** | |
635 | + * 按主键查询 | |
636 | + * isLock 是否锁定, 默认不锁 | |
637 | + * fromWriteDB 是否从写库读写,默认从读库查询 | |
638 | + */ | |
639 | + public <E> E findBeanByID(ID id, boolean fromWriteDB, boolean isLock, Class<E> beanClass) { | |
640 | + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); | |
641 | + StringBuffer pql = new StringBuffer(sql.toString()); | |
642 | + sql.append(" WHERE "+idTableFieldName+" = ?"); | |
643 | + pql.append(" WHERE "+idTableFieldName+" = " + id); | |
644 | + if (isLock) { | |
645 | + sql.append(" FOR UPDATE"); | |
646 | + pql.append(" FOR UPDATE"); | |
647 | + } | |
648 | + return (E) (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForObject(sql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField), id); | |
649 | + } | |
650 | + | |
651 | + /** | |
652 | + * 根据条件List<Object[]>查询 | |
653 | + * Object[]数组长度是3 | |
654 | + * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
655 | + */ | |
656 | + public <E> E findBeanByCondition(List<Object[]> condition, Class<E> beanClass) throws Exception { | |
657 | + List<E> tempList = findBeanListByCondition(condition, null, false, beanClass); | |
658 | + if(tempList == null || tempList.size() == 0){ | |
659 | + return null; | |
660 | + } | |
661 | + if(tempList != null && tempList.size() == 1){ | |
662 | + return tempList.get(0); | |
663 | + }else{ | |
664 | + throw new Exception("数据库存在多条记录满足条件"); | |
665 | + } | |
666 | + } | |
667 | + | |
668 | + /** | |
669 | + * 根据条件sql查询 | |
670 | + * sqlCondition 为where 后面的条件。 | |
671 | + */ | |
672 | + public <E> E findBeanBySql(String sqlCondition, Class<E> beanClass) throws Exception{ | |
673 | + List<E> tempList = findBeanListBySql(sqlCondition, false, beanClass); | |
674 | + if(tempList == null || tempList.size() == 0){ | |
675 | + return null; | |
676 | + } | |
677 | + if(tempList != null && tempList.size() == 1){ | |
678 | + return tempList.get(0); | |
679 | + }else{ | |
680 | + throw new Exception("数据库存在多条记录满足条件"); | |
681 | + } | |
682 | + } | |
683 | + | |
684 | + /** | |
685 | + * 根据条件List<Object[]>查询 | |
686 | + * Object[]数组长度是3 | |
687 | + * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
688 | + */ | |
689 | + public <E> List<E> findBeanListByCondition(List<Object[]> condition, Class<E> beanClass) { | |
690 | + return findBeanListByCondition(condition, null, false, beanClass); | |
691 | + } | |
692 | + | |
693 | + /** | |
694 | + * 根据条件List<Object[]>查询 | |
695 | + * Object[]数组长度是3 | |
696 | + * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
697 | + */ | |
698 | + public <E> List<E> findBeanListByCondition(List<Object[]> condition, String sortCondition, boolean fromWriteDB, Class<E> beanClass) { | |
699 | + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); | |
700 | + StringBuffer pql = new StringBuffer(sql.toString()); | |
701 | + List<Object> list = new ArrayList<Object>(); | |
702 | + this.appendWhereCondition(sql, pql, list, condition); | |
703 | + if(sortCondition != null && !sortCondition.equals("")){ | |
704 | + sql.append(" " + sortCondition + " "); | |
705 | + pql.append(" " + sortCondition + " "); | |
706 | + } | |
707 | + return (List<E>)(fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField), list.toArray()); | |
708 | + } | |
709 | + | |
710 | + /** | |
711 | + * 根据条件sql查询 | |
712 | + * sqlCondition 为where 后面的条件。 | |
713 | + */ | |
714 | + public <E> List<E> findBeanListBySql(String sqlCondition, Class<E> beanClass) { | |
715 | + return findBeanListBySql(sqlCondition, false, beanClass); | |
716 | + } | |
717 | + | |
718 | + /** | |
719 | + * 根据条件sql查询 | |
720 | + * sqlCondition 为where 后面的条件。 | |
721 | + */ | |
722 | + public <E> List<E> findBeanListBySql(String sqlCondition, boolean fromWriteDB, Class<E> beanClass) { | |
723 | + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
724 | + return (List<E>)(fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField)); | |
725 | + } | |
726 | + | |
727 | + /** | |
728 | + * 按条件分页查询 | |
729 | + * Object[]数组长度是3 | |
730 | + * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
731 | + */ | |
732 | + public <E> Map<String, Object> findBeanPageByCondition(List<Object[]> condition, int page, int pageSize, Class<E> beanClass) { | |
733 | + return findBeanPageByCondition(condition, null , page, pageSize, false, beanClass); | |
734 | + } | |
735 | + | |
736 | + /** | |
737 | + * 按条件分页查询 | |
738 | + * Object[]数组长度是3 | |
739 | + * Object[]第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 | |
740 | + * boolean isUseCache, 是否用缓存,默认用。 | |
741 | + * boolean isAddCache, 是否添加缓存,默认添加。 | |
742 | + */ | |
743 | + public <E> Map<String, Object> findBeanPageByCondition(List<Object[]> condition,String sortCondition, int page, int pageSize, boolean fromWriteDB, Class<E> beanClass) { | |
744 | + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); | |
745 | + StringBuffer pql = new StringBuffer(sql.toString()); | |
746 | + StringBuffer sqlCount = new StringBuffer("SELECT COUNT(1) rowCount FROM "+this.getTableSql()); | |
747 | + | |
748 | + List<Object> count_list = new ArrayList<Object>(); | |
749 | + List<Object> page_list = new ArrayList<Object>(); | |
750 | + this.appendWhereConditionForCount(sqlCount, condition); | |
751 | + this.appendWhereCondition(sql, pql, count_list, condition); | |
752 | + for (int i = 0; i < count_list.size(); i++) | |
753 | + page_list.add(count_list.get(i)); | |
754 | + | |
755 | + page_list.add((page - 1) * pageSize); | |
756 | + page_list.add(pageSize); | |
757 | + | |
758 | + if(sortCondition != null && !sortCondition.equals("")){ | |
759 | + sql.append(" " + sortCondition + " "); | |
760 | + pql.append(" " + sortCondition + " "); | |
761 | + } | |
762 | + | |
763 | + String pageSql = sql.toString() + " limit ?, ?"; | |
764 | + | |
765 | + Map<String, Object> totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString(), count_list.toArray()) ; | |
766 | + | |
767 | + Map<String, Object> resultMap = new HashMap<String, Object>(); | |
768 | + resultMap.put("page", page); | |
769 | + resultMap.put("total", totalRowsMap.get("rowCount")); | |
770 | + List<E> resultList = null; | |
771 | + resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField), page_list.toArray()); | |
772 | + resultMap.put("rows", resultList); | |
773 | + return resultMap; | |
774 | + } | |
775 | + | |
776 | + /** | |
777 | + * 按sql分页查询, sqlCondition为where 后条件sql | |
778 | + */ | |
779 | + public <E> Map<String, Object> findBeanPageBySql(String sqlCondition, int page, int pageSize, Class<E> beanClass) { | |
780 | + return findBeanPageBySql(sqlCondition, page, pageSize, false, beanClass); | |
781 | + } | |
782 | + | |
783 | + /** | |
784 | + * 按sql分页查询, sqlCondition为where 后条件sql | |
785 | + */ | |
786 | + public <E> Map<String, Object> findBeanPageBySql(String sqlCondition, int page, int pageSize,boolean fromWriteDB, Class<E> beanClass) { | |
787 | + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition ); | |
788 | + StringBuffer sqlCount = new StringBuffer("SELECT count(1) rowCount FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
789 | + String pageSql = sql.toString() + " limit ?, ?"; | |
790 | + String pagePql = sql.toString() + " limit " + ((page -1) * pageSize) + ", " + (page * pageSize); | |
791 | + List<Object> page_list = new ArrayList<Object>(); | |
792 | + page_list.add((page - 1) * pageSize); | |
793 | + page_list.add(page * pageSize); | |
794 | + | |
795 | + Map<String, Object> totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString()); | |
796 | + | |
797 | + Map<String, Object> resultMap = new HashMap<String, Object>(); | |
798 | + resultMap.put("page", page); | |
799 | + resultMap.put("total", totalRowsMap.get("rowCount")); | |
800 | + List<E> resultList = null; | |
801 | + resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField), page_list.toArray()); | |
802 | + resultMap.put("rows", resultList); | |
803 | + return resultMap; | |
804 | + } | |
625 | 805 | } | ... | ... |