From 085176f3a6f75f5a780b62fdd4ac8346632fbc55 Mon Sep 17 00:00:00 2001 From: 王彬 Date: Mon, 19 Aug 2019 14:36:24 +0800 Subject: [PATCH] 1.add find by class methods --- build.gradle | 5 ++++- src/main/java/com/taover/repository/CustomJdbcTemplate.java | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 820c037..ce7a99e 100644 --- a/build.gradle +++ b/build.gradle @@ -19,12 +19,15 @@ mainClassName = 'com.taover.repository.UtilsString' dependencies { compile("org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final") - compile('org.springframework:spring-jdbc:5.1.9.RELEAS') + compile('org.springframework:spring-jdbc:5.1.9.RELEASE') compile('mysql:mysql-connector-java:5.1.47') } repositories { jcenter() + maven{ url 'http://repository.sonatype.org/content/groups/public/' } + maven{ url 'https://repository.jboss.org/nexus/content/groups/public/' } + maven{ url 'http://47.93.122.69:9001/repository/maven-releases/' } } task sourcesJar(type: Jar, dependsOn: classes) { diff --git a/src/main/java/com/taover/repository/CustomJdbcTemplate.java b/src/main/java/com/taover/repository/CustomJdbcTemplate.java index 09da3a8..427fbb3 100644 --- a/src/main/java/com/taover/repository/CustomJdbcTemplate.java +++ b/src/main/java/com/taover/repository/CustomJdbcTemplate.java @@ -622,4 +622,184 @@ public class CustomJdbcTemplate { countData = this.jdbcTemplateRead.queryForMap(countSql); return UtilsSql.createPage(page, Integer.valueOf(countData.get("rows").toString()), queryData); } + + + /** + * 按主键查询 + */ + public E findBeanByID(ID id, Class beanClass) { + return findBeanByID(id, true, false, beanClass); + } + + /** + * 按主键查询 + * isLock 是否锁定, 默认不锁 + * fromWriteDB 是否从写库读写,默认从读库查询 + */ + public E findBeanByID(ID id, boolean fromWriteDB, boolean isLock, Class beanClass) { + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); + StringBuffer pql = new StringBuffer(sql.toString()); + sql.append(" WHERE "+idTableFieldName+" = ?"); + pql.append(" WHERE "+idTableFieldName+" = " + id); + if (isLock) { + sql.append(" FOR UPDATE"); + pql.append(" FOR UPDATE"); + } + return (E) (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForObject(sql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField), id); + } + + /** + * 根据条件List查询 + * Object[]数组长度是3 + * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 + */ + public E findBeanByCondition(List condition, Class beanClass) throws Exception { + List tempList = findBeanListByCondition(condition, null, false, beanClass); + if(tempList == null || tempList.size() == 0){ + return null; + } + if(tempList != null && tempList.size() == 1){ + return tempList.get(0); + }else{ + throw new Exception("数据库存在多条记录满足条件"); + } + } + + /** + * 根据条件sql查询 + * sqlCondition 为where 后面的条件。 + */ + public E findBeanBySql(String sqlCondition, Class beanClass) throws Exception{ + List tempList = findBeanListBySql(sqlCondition, false, beanClass); + if(tempList == null || tempList.size() == 0){ + return null; + } + if(tempList != null && tempList.size() == 1){ + return tempList.get(0); + }else{ + throw new Exception("数据库存在多条记录满足条件"); + } + } + + /** + * 根据条件List查询 + * Object[]数组长度是3 + * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 + */ + public List findBeanListByCondition(List condition, Class beanClass) { + return findBeanListByCondition(condition, null, false, beanClass); + } + + /** + * 根据条件List查询 + * Object[]数组长度是3 + * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 + */ + public List findBeanListByCondition(List condition, String sortCondition, boolean fromWriteDB, Class beanClass) { + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); + StringBuffer pql = new StringBuffer(sql.toString()); + List list = new ArrayList(); + this.appendWhereCondition(sql, pql, list, condition); + if(sortCondition != null && !sortCondition.equals("")){ + sql.append(" " + sortCondition + " "); + pql.append(" " + sortCondition + " "); + } + return (List)(fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField), list.toArray()); + } + + /** + * 根据条件sql查询 + * sqlCondition 为where 后面的条件。 + */ + public List findBeanListBySql(String sqlCondition, Class beanClass) { + return findBeanListBySql(sqlCondition, false, beanClass); + } + + /** + * 根据条件sql查询 + * sqlCondition 为where 后面的条件。 + */ + public List findBeanListBySql(String sqlCondition, boolean fromWriteDB, Class beanClass) { + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition); + return (List)(fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField)); + } + + /** + * 按条件分页查询 + * Object[]数组长度是3 + * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 + */ + public Map findBeanPageByCondition(List condition, int page, int pageSize, Class beanClass) { + return findBeanPageByCondition(condition, null , page, pageSize, false, beanClass); + } + + /** + * 按条件分页查询 + * Object[]数组长度是3 + * Object[]第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 + * boolean isUseCache, 是否用缓存,默认用。 + * boolean isAddCache, 是否添加缓存,默认添加。 + */ + public Map findBeanPageByCondition(List condition,String sortCondition, int page, int pageSize, boolean fromWriteDB, Class beanClass) { + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); + StringBuffer pql = new StringBuffer(sql.toString()); + StringBuffer sqlCount = new StringBuffer("SELECT COUNT(1) rowCount FROM "+this.getTableSql()); + + List count_list = new ArrayList(); + List page_list = new ArrayList(); + this.appendWhereConditionForCount(sqlCount, condition); + this.appendWhereCondition(sql, pql, count_list, condition); + for (int i = 0; i < count_list.size(); i++) + page_list.add(count_list.get(i)); + + page_list.add((page - 1) * pageSize); + page_list.add(pageSize); + + if(sortCondition != null && !sortCondition.equals("")){ + sql.append(" " + sortCondition + " "); + pql.append(" " + sortCondition + " "); + } + + String pageSql = sql.toString() + " limit ?, ?"; + + Map totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString(), count_list.toArray()) ; + + Map resultMap = new HashMap(); + resultMap.put("page", page); + resultMap.put("total", totalRowsMap.get("rowCount")); + List resultList = null; + resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField), page_list.toArray()); + resultMap.put("rows", resultList); + return resultMap; + } + + /** + * 按sql分页查询, sqlCondition为where 后条件sql + */ + public Map findBeanPageBySql(String sqlCondition, int page, int pageSize, Class beanClass) { + return findBeanPageBySql(sqlCondition, page, pageSize, false, beanClass); + } + + /** + * 按sql分页查询, sqlCondition为where 后条件sql + */ + public Map findBeanPageBySql(String sqlCondition, int page, int pageSize,boolean fromWriteDB, Class beanClass) { + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition ); + StringBuffer sqlCount = new StringBuffer("SELECT count(1) rowCount FROM "+this.getTableSql()+" WHERE " + sqlCondition); + String pageSql = sql.toString() + " limit ?, ?"; + String pagePql = sql.toString() + " limit " + ((page -1) * pageSize) + ", " + (page * pageSize); + List page_list = new ArrayList(); + page_list.add((page - 1) * pageSize); + page_list.add(page * pageSize); + + Map totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString()); + + Map resultMap = new HashMap(); + resultMap.put("page", page); + resultMap.put("total", totalRowsMap.get("rowCount")); + List resultList = null; + resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), new CustomJdbcTemplateRowMapper(beanClass, this.tableToBeanField), page_list.toArray()); + resultMap.put("rows", resultList); + return resultMap; + } } -- libgit2 0.21.2