Commit a3f02b777c4fe547a7f40833bbfac425c64fe48a

Authored by 王彬
1 parent abaa9ea8

1. 增加page bean 2. 增加bean切面接口

@@ -51,7 +51,7 @@ uploadArchives { @@ -51,7 +51,7 @@ uploadArchives {
51 authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) 51 authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
52 } 52 }
53 pom.project { 53 pom.project {
54 - version '2.0.4' 54 + version '2.1.0'
55 artifactId ARTIFACT_Id 55 artifactId ARTIFACT_Id
56 groupId GROUP_ID 56 groupId GROUP_ID
57 packaging TYPE 57 packaging TYPE
src/main/java/com/taover/repository/CustomJdbcTemplate.java
@@ -110,7 +110,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { @@ -110,7 +110,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> {
110 * @param name 转换前的驼峰式命名的字符串 110 * @param name 转换前的驼峰式命名的字符串
111 * @return 转换后下划线大写方式命名的字符串 111 * @return 转换后下划线大写方式命名的字符串
112 */ 112 */
113 - public String camelToUnderline(String name) { 113 + private String camelToUnderline(String name) {
114 StringBuilder result = new StringBuilder(); 114 StringBuilder result = new StringBuilder();
115 if (name != null && name.length() > 0) { 115 if (name != null && name.length() > 0) {
116 // 将第一个字符处理成大写 116 // 将第一个字符处理成大写
@@ -135,7 +135,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { @@ -135,7 +135,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> {
135 * @param name 转换前的下划线大写方式命名的字符串 135 * @param name 转换前的下划线大写方式命名的字符串
136 * @return 转换后的驼峰式命名的字符串 136 * @return 转换后的驼峰式命名的字符串
137 */ 137 */
138 - public String underlineToCamel(String name) { 138 + private String underlineToCamel(String name) {
139 StringBuilder result = new StringBuilder(); 139 StringBuilder result = new StringBuilder();
140 // 快速检查 140 // 快速检查
141 if (name == null || name.isEmpty()) { 141 if (name == null || name.isEmpty()) {
@@ -165,7 +165,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { @@ -165,7 +165,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> {
165 return result.toString(); 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 if (condition == null || condition.size() == 0) return; 169 if (condition == null || condition.size() == 0) return;
170 Object[] con = condition.get(0); 170 Object[] con = condition.get(0);
171 int iLen = condition.size(); 171 int iLen = condition.size();
@@ -191,7 +191,7 @@ public class CustomJdbcTemplate&lt;T, ID extends Serializable&gt; { @@ -191,7 +191,7 @@ public class CustomJdbcTemplate&lt;T, ID extends Serializable&gt; {
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 if (condition == null || condition.size() == 0) return; 195 if (condition == null || condition.size() == 0) return;
196 Object[] con = condition.get(0); 196 Object[] con = condition.get(0);
197 int iLen = condition.size(); 197 int iLen = condition.size();
@@ -209,7 +209,7 @@ public class CustomJdbcTemplate&lt;T, ID extends Serializable&gt; { @@ -209,7 +209,7 @@ public class CustomJdbcTemplate&lt;T, ID extends Serializable&gt; {
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 for (Object[] arg : obj) { 213 for (Object[] arg : obj) {
214 String opt = "="; 214 String opt = "=";
215 if (arg.length > 2) { 215 if (arg.length > 2) {
@@ -625,6 +625,20 @@ public class CustomJdbcTemplate&lt;T, ID extends Serializable&gt; { @@ -625,6 +625,20 @@ public class CustomJdbcTemplate&lt;T, ID extends Serializable&gt; {
625 return UtilsSql.createPage(page, Integer.valueOf(countData.get("rows").toString()), queryData); 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 package com.taover.repository; 1 package com.taover.repository;
2 2
3 import java.lang.reflect.Field; 3 import java.lang.reflect.Field;
  4 +import java.lang.reflect.Method;
4 import java.sql.ResultSet; 5 import java.sql.ResultSet;
5 import java.sql.SQLException; 6 import java.sql.SQLException;
6 import java.util.Map; 7 import java.util.Map;
@@ -8,24 +9,43 @@ import java.util.Map; @@ -8,24 +9,43 @@ import java.util.Map;
8 import org.springframework.jdbc.core.RowMapper; 9 import org.springframework.jdbc.core.RowMapper;
9 import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSetMetaData; 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 private Map<String, String> tableToBeanMap; 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 this.classInfo = classInfo; 17 this.classInfo = classInfo;
17 this.tableToBeanMap = tableToBeanMap; 18 this.tableToBeanMap = tableToBeanMap;
18 } 19 }
19 20
20 @Override 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 try { 33 try {
24 targetObj = this.classInfo.newInstance(); 34 targetObj = this.classInfo.newInstance();
25 } catch (Exception e) { 35 } catch (Exception e) {
26 throw new RuntimeException(e); 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 ResultSetWrappingSqlRowSetMetaData wapping = new ResultSetWrappingSqlRowSetMetaData(rs.getMetaData()); 49 ResultSetWrappingSqlRowSetMetaData wapping = new ResultSetWrappingSqlRowSetMetaData(rs.getMetaData());
30 int columnCount = wapping.getColumnCount(); 50 int columnCount = wapping.getColumnCount();
31 for (int i = 1; i<=columnCount; i++) { 51 for (int i = 1; i<=columnCount; i++) {
@@ -42,6 +62,17 @@ public class CustomJdbcTemplateRowMapper implements RowMapper{ @@ -42,6 +62,17 @@ public class CustomJdbcTemplateRowMapper implements RowMapper{
42 e.printStackTrace(); 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 return targetObj; 76 return targetObj;
46 } 77 }
47 } 78 }
src/main/java/com/taover/repository/EntityPointCut.java 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +package com.taover.repository;
  2 +
  3 +public interface EntityPointCut {
  4 +
  5 + public void before();
  6 +
  7 + public void after();
  8 +}