Commit a3f02b777c4fe547a7f40833bbfac425c64fe48a
1 parent
abaa9ea8
Exists in
master
and in
2 other branches
1. 增加page bean 2. 增加bean切面接口
Showing
4 changed files
with
65 additions
and
12 deletions
Show diff stats
build.gradle
src/main/java/com/taover/repository/CustomJdbcTemplate.java
| ... | ... | @@ -110,7 +110,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 110 | 110 | * @param name 转换前的驼峰式命名的字符串 |
| 111 | 111 | * @return 转换后下划线大写方式命名的字符串 |
| 112 | 112 | */ |
| 113 | - public String camelToUnderline(String name) { | |
| 113 | + private String camelToUnderline(String name) { | |
| 114 | 114 | StringBuilder result = new StringBuilder(); |
| 115 | 115 | if (name != null && name.length() > 0) { |
| 116 | 116 | // 将第一个字符处理成大写 |
| ... | ... | @@ -135,7 +135,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 135 | 135 | * @param name 转换前的下划线大写方式命名的字符串 |
| 136 | 136 | * @return 转换后的驼峰式命名的字符串 |
| 137 | 137 | */ |
| 138 | - public String underlineToCamel(String name) { | |
| 138 | + private String underlineToCamel(String name) { | |
| 139 | 139 | StringBuilder result = new StringBuilder(); |
| 140 | 140 | // 快速检查 |
| 141 | 141 | if (name == null || name.isEmpty()) { |
| ... | ... | @@ -165,7 +165,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 165 | 165 | return result.toString(); |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - public void appendWhereCondition(StringBuffer sql, StringBuffer pql, List<Object> list, List<Object[]> condition) { | |
| 168 | + private void appendWhereCondition(StringBuffer sql, StringBuffer pql, List<Object> list, List<Object[]> condition) { | |
| 169 | 169 | if (condition == null || condition.size() == 0) return; |
| 170 | 170 | Object[] con = condition.get(0); |
| 171 | 171 | int iLen = condition.size(); |
| ... | ... | @@ -191,7 +191,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 191 | 191 | } |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | - public void appendWhereConditionForCount(StringBuffer sql, List<Object[]> condition) { | |
| 194 | + private void appendWhereConditionForCount(StringBuffer sql, List<Object[]> condition) { | |
| 195 | 195 | if (condition == null || condition.size() == 0) return; |
| 196 | 196 | Object[] con = condition.get(0); |
| 197 | 197 | int iLen = condition.size(); |
| ... | ... | @@ -209,7 +209,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 209 | 209 | } |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | - public void appendSql(StringBuffer sql, StringBuffer pql, List<Object> list, List<Object[]> obj) { | |
| 212 | + private void appendSql(StringBuffer sql, StringBuffer pql, List<Object> list, List<Object[]> obj) { | |
| 213 | 213 | for (Object[] arg : obj) { |
| 214 | 214 | String opt = "="; |
| 215 | 215 | if (arg.length > 2) { |
| ... | ... | @@ -625,6 +625,20 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 625 | 625 | return UtilsSql.createPage(page, Integer.valueOf(countData.get("rows").toString()), queryData); |
| 626 | 626 | } |
| 627 | 627 | |
| 628 | + public <E> Map<String, Object> getBeanPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize, Class<E> beanClass){ | |
| 629 | + //构造查询语句 | |
| 630 | + String querySql = coreSql+orderByPartSql+UtilsSql.getLimitCondition(page, pageSize); | |
| 631 | + | |
| 632 | + //构造统计计数语句 | |
| 633 | + String countSql = "select count(*) rows from ("+coreSql+" ) t "; | |
| 634 | + | |
| 635 | + //执行查询 | |
| 636 | + List<E> queryData = new ArrayList<E>(); | |
| 637 | + Map<String, Object> countData = new HashMap<String, Object>(); | |
| 638 | + queryData = this.jdbcTemplateRead.queryForList(querySql, beanClass); | |
| 639 | + countData = this.jdbcTemplateRead.queryForMap(countSql); | |
| 640 | + return UtilsSql.createPage(page, Integer.valueOf(countData.get("rows").toString()), queryData); | |
| 641 | + } | |
| 628 | 642 | |
| 629 | 643 | /** |
| 630 | 644 | * 按主键查询 | ... | ... |
src/main/java/com/taover/repository/CustomJdbcTemplateRowMapper.java
| 1 | 1 | package com.taover.repository; |
| 2 | 2 | |
| 3 | 3 | import java.lang.reflect.Field; |
| 4 | +import java.lang.reflect.Method; | |
| 4 | 5 | import java.sql.ResultSet; |
| 5 | 6 | import java.sql.SQLException; |
| 6 | 7 | import java.util.Map; |
| ... | ... | @@ -8,24 +9,43 @@ import java.util.Map; |
| 8 | 9 | import org.springframework.jdbc.core.RowMapper; |
| 9 | 10 | import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSetMetaData; |
| 10 | 11 | |
| 11 | -public class CustomJdbcTemplateRowMapper implements RowMapper{ | |
| 12 | - private Class classInfo; | |
| 12 | +public class CustomJdbcTemplateRowMapper <E> implements RowMapper{ | |
| 13 | + private Class<E> classInfo; | |
| 13 | 14 | private Map<String, String> tableToBeanMap; |
| 14 | 15 | |
| 15 | - public CustomJdbcTemplateRowMapper(Class classInfo, Map<String, String> tableToBeanMap) { | |
| 16 | + public CustomJdbcTemplateRowMapper(Class<E> classInfo, Map<String, String> tableToBeanMap) { | |
| 16 | 17 | this.classInfo = classInfo; |
| 17 | 18 | this.tableToBeanMap = tableToBeanMap; |
| 18 | 19 | } |
| 19 | 20 | |
| 20 | 21 | @Override |
| 21 | - public Object mapRow(ResultSet rs, int index) throws SQLException { | |
| 22 | - Object targetObj; | |
| 22 | + public E mapRow(ResultSet rs, int index) throws SQLException { | |
| 23 | + boolean hasImplementPointCut = false; | |
| 24 | + Class[] interfaceArr = this.classInfo.getInterfaces(); | |
| 25 | + for(int i=0; i<interfaceArr.length; ++i){ | |
| 26 | + if(interfaceArr[i].getName().equals("com.taover.repository.EntityPointCut")){ | |
| 27 | + hasImplementPointCut = true; | |
| 28 | + break; | |
| 29 | + } | |
| 30 | + } | |
| 31 | + | |
| 32 | + E targetObj; | |
| 23 | 33 | try { |
| 24 | 34 | targetObj = this.classInfo.newInstance(); |
| 25 | 35 | } catch (Exception e) { |
| 26 | 36 | throw new RuntimeException(e); |
| 27 | 37 | } |
| 28 | - | |
| 38 | + | |
| 39 | + if(hasImplementPointCut){ | |
| 40 | + try{ | |
| 41 | + Method beforeMethod = this.classInfo.getDeclaredMethod("before"); | |
| 42 | + beforeMethod.setAccessible(true); | |
| 43 | + beforeMethod.invoke(targetObj); | |
| 44 | + }catch(Exception e){ | |
| 45 | + e.printStackTrace(); | |
| 46 | + } | |
| 47 | + } | |
| 48 | + | |
| 29 | 49 | ResultSetWrappingSqlRowSetMetaData wapping = new ResultSetWrappingSqlRowSetMetaData(rs.getMetaData()); |
| 30 | 50 | int columnCount = wapping.getColumnCount(); |
| 31 | 51 | for (int i = 1; i<=columnCount; i++) { |
| ... | ... | @@ -42,6 +62,17 @@ public class CustomJdbcTemplateRowMapper implements RowMapper{ |
| 42 | 62 | e.printStackTrace(); |
| 43 | 63 | } |
| 44 | 64 | } |
| 65 | + | |
| 66 | + if(hasImplementPointCut){ | |
| 67 | + try{ | |
| 68 | + Method afterMethod = this.classInfo.getDeclaredMethod("after"); | |
| 69 | + afterMethod.setAccessible(true); | |
| 70 | + afterMethod.invoke(targetObj); | |
| 71 | + }catch(Exception e){ | |
| 72 | + e.printStackTrace(); | |
| 73 | + } | |
| 74 | + } | |
| 75 | + | |
| 45 | 76 | return targetObj; |
| 46 | 77 | } |
| 47 | 78 | } | ... | ... |