Commit 7a32a07a7c0fd8eb2269a0ea8974b792ea59e60f

Authored by 王彬
1 parent 486880e3

upgrade repository row mapper version

@@ -22,6 +22,7 @@ dependencies { @@ -22,6 +22,7 @@ dependencies {
22 compile("org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final") 22 compile("org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final")
23 compile('org.springframework:spring-jdbc:5.1.9.RELEASE') 23 compile('org.springframework:spring-jdbc:5.1.9.RELEASE')
24 compile('mysql:mysql-connector-java:5.1.47') 24 compile('mysql:mysql-connector-java:5.1.47')
  25 + compile('com.alibaba:druid:1.2.4')
25 } 26 }
26 27
27 repositories { 28 repositories {
@@ -55,7 +56,7 @@ uploadArchives { @@ -55,7 +56,7 @@ uploadArchives {
55 authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) 56 authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
56 } 57 }
57 pom.project { 58 pom.project {
58 - version '2.1.36' 59 + version '2.1.38'
59 artifactId ARTIFACT_Id 60 artifactId ARTIFACT_Id
60 groupId GROUP_ID 61 groupId GROUP_ID
61 packaging TYPE 62 packaging TYPE
src/main/java/com/taover/repository/CustomJdbcTemplate.java
@@ -45,6 +45,12 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { @@ -45,6 +45,12 @@ public class CustomJdbcTemplate<T, ID extends Serializable> {
45 return this.customJdbcTemplateRowMapper; 45 return this.customJdbcTemplateRowMapper;
46 } 46 }
47 47
  48 + public CustomJdbcTemplate(JdbcTemplate jdbcTemplateWrite) throws Exception{
  49 + this();
  50 + this.jdbcTemplateWrite = jdbcTemplateWrite;
  51 + this.jdbcTemplateRead = jdbcTemplateWrite;
  52 + }
  53 +
48 public CustomJdbcTemplate() throws Exception{ 54 public CustomJdbcTemplate() throws Exception{
49 //获取泛型类Class 55 //获取泛型类Class
50 this.tClassInfo = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 56 this.tClassInfo = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
src/main/java/com/taover/repository/CustomJdbcTemplateRowMapper.java
@@ -4,22 +4,38 @@ import java.lang.reflect.Field; @@ -4,22 +4,38 @@ import java.lang.reflect.Field;
4 import java.lang.reflect.Method; 4 import java.lang.reflect.Method;
5 import java.sql.ResultSet; 5 import java.sql.ResultSet;
6 import java.sql.SQLException; 6 import java.sql.SQLException;
  7 +import java.util.HashMap;
7 import java.util.Map; 8 import java.util.Map;
8 9
  10 +import org.apache.commons.logging.Log;
  11 +import org.apache.commons.logging.LogFactory;
9 import org.springframework.jdbc.core.RowMapper; 12 import org.springframework.jdbc.core.RowMapper;
10 import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSetMetaData; 13 import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSetMetaData;
11 14
12 -public class CustomJdbcTemplateRowMapper <E> implements RowMapper{ 15 +public class CustomJdbcTemplateRowMapper <E> implements RowMapper<E>{
  16 + Log log = LogFactory.getLog(this.getClass());
  17 +
  18 + private Map<String, Field> beanFielNameToField = new HashMap<String, Field>();
  19 + private Method beforeMethod = null;
  20 + private Method afterMethod = null;
  21 +
13 private Class<E> classInfo; 22 private Class<E> classInfo;
14 - private Map<String, String> tableToBeanMap;  
15 - 23 + private Map<String, String> tableToBeanMap;
  24 +
16 public CustomJdbcTemplateRowMapper(Class<E> classInfo, Map<String, String> tableToBeanMap) { 25 public CustomJdbcTemplateRowMapper(Class<E> classInfo, Map<String, String> tableToBeanMap) {
17 this.classInfo = classInfo; 26 this.classInfo = classInfo;
18 this.tableToBeanMap = tableToBeanMap; 27 this.tableToBeanMap = tableToBeanMap;
19 - }  
20 -  
21 - @Override  
22 - public E mapRow(ResultSet rs, int index) throws SQLException { 28 +
  29 + Field[] fields = this.classInfo.getDeclaredFields();
  30 + for(Field item: fields) {
  31 + try {
  32 + item.setAccessible(true);
  33 + this.beanFielNameToField.put(item.getName(), item);
  34 + }catch (Exception e) {
  35 + log.error("set field accessible:"+e.getMessage());
  36 + }
  37 + }
  38 +
23 boolean hasImplementPointCut = false; 39 boolean hasImplementPointCut = false;
24 Class[] interfaceArr = this.classInfo.getInterfaces(); 40 Class[] interfaceArr = this.classInfo.getInterfaces();
25 for(int i=0; i<interfaceArr.length; ++i){ 41 for(int i=0; i<interfaceArr.length; ++i){
@@ -29,6 +45,25 @@ public class CustomJdbcTemplateRowMapper &lt;E&gt; implements RowMapper{ @@ -29,6 +45,25 @@ public class CustomJdbcTemplateRowMapper &lt;E&gt; implements RowMapper{
29 } 45 }
30 } 46 }
31 47
  48 + if(hasImplementPointCut){
  49 + try{
  50 + beforeMethod = this.classInfo.getDeclaredMethod("before");
  51 + beforeMethod.setAccessible(true);
  52 + }catch(Exception e){
  53 + log.error("set before method exception:"+e.getMessage());
  54 + }
  55 +
  56 + try{
  57 + afterMethod = this.classInfo.getDeclaredMethod("after");
  58 + afterMethod.setAccessible(true);
  59 + }catch(Exception e){
  60 + log.error("set after method exception:"+e.getMessage());
  61 + }
  62 + }
  63 + }
  64 +
  65 + @Override
  66 + public E mapRow(ResultSet rs, int index) throws SQLException {
32 E targetObj; 67 E targetObj;
33 try { 68 try {
34 targetObj = this.classInfo.newInstance(); 69 targetObj = this.classInfo.newInstance();
@@ -36,13 +71,11 @@ public class CustomJdbcTemplateRowMapper &lt;E&gt; implements RowMapper{ @@ -36,13 +71,11 @@ public class CustomJdbcTemplateRowMapper &lt;E&gt; implements RowMapper{
36 throw new RuntimeException(e); 71 throw new RuntimeException(e);
37 } 72 }
38 73
39 - if(hasImplementPointCut){ 74 + if(this.beforeMethod != null){
40 try{ 75 try{
41 - Method beforeMethod = this.classInfo.getDeclaredMethod("before");  
42 - beforeMethod.setAccessible(true);  
43 beforeMethod.invoke(targetObj); 76 beforeMethod.invoke(targetObj);
44 - }catch(Exception e){  
45 - e.printStackTrace(); 77 + }catch(Exception e){
  78 + log.error("invoke before exception:"+e.getMessage());
46 } 79 }
47 } 80 }
48 81
@@ -52,25 +85,33 @@ public class CustomJdbcTemplateRowMapper &lt;E&gt; implements RowMapper{ @@ -52,25 +85,33 @@ public class CustomJdbcTemplateRowMapper &lt;E&gt; implements RowMapper{
52 String tableFieldName = wapping.getColumnLabel(i); 85 String tableFieldName = wapping.getColumnLabel(i);
53 String beanFieldName = this.tableToBeanMap.get(tableFieldName); 86 String beanFieldName = this.tableToBeanMap.get(tableFieldName);
54 Object value = rs.getObject(i); 87 Object value = rs.getObject(i);
  88 + Field beanField = this.beanFielNameToField.get(beanFieldName);
  89 + if(null == value || beanFieldName == null && beanField == null){
  90 + continue;
  91 + }
55 try { 92 try {
56 - if(null != value && beanFieldName != null){  
57 - Field beanField = this.classInfo.getDeclaredField(beanFieldName);  
58 - beanField.setAccessible(true);  
59 - beanField.set(targetObj, value);  
60 - }  
61 - } catch (Exception e) {  
62 - e.printStackTrace(); 93 + beanField.set(targetObj, value);
  94 + } catch (IllegalArgumentException e) {
  95 + if("Integer".equals(value.getClass().getSimpleName()) && "Long".equals(beanField.getType().getSimpleName())) {
  96 + try {
  97 + beanField.set(targetObj, Long.valueOf(value.toString()));
  98 + } catch (IllegalArgumentException | IllegalAccessException e1) {
  99 + log.error("map set object field error -> Integer convert Long:"+e.getMessage());
  100 + }
  101 + }else {
  102 + log.error("map set object field error:"+e.getMessage());
  103 + }
  104 + } catch (IllegalAccessException e) {
  105 + log.error("map set object field error:"+e.getMessage());
63 } 106 }
64 } 107 }
65 108
66 - if(hasImplementPointCut){  
67 - try{  
68 - Method afterMethod = this.classInfo.getDeclaredMethod("after");  
69 - afterMethod.setAccessible(true); 109 + if(this.afterMethod != null){
  110 + try {
70 afterMethod.invoke(targetObj); 111 afterMethod.invoke(targetObj);
71 - }catch(Exception e){  
72 - e.printStackTrace();  
73 - } 112 + }catch (Exception e) {
  113 + log.error("invoke after exception:"+e.getMessage());
  114 + }
74 } 115 }
75 116
76 return targetObj; 117 return targetObj;
src/main/java/com/taover/repository/CustomJdbcTemplateWrapperTenant.java
@@ -48,6 +48,11 @@ public class CustomJdbcTemplateWrapperTenant&lt;T, ID extends Serializable&gt; impleme @@ -48,6 +48,11 @@ public class CustomJdbcTemplateWrapperTenant&lt;T, ID extends Serializable&gt; impleme
48 return this.customJdbcTemplateRowMapper; 48 return this.customJdbcTemplateRowMapper;
49 } 49 }
50 50
  51 + public CustomJdbcTemplateWrapperTenant(JdbcTemplate jdbcTemplateWrite) throws Exception{
  52 + this();
  53 + this.jdbcTemplateWrite = jdbcTemplateWrite;
  54 + }
  55 +
51 public CustomJdbcTemplateWrapperTenant() throws Exception{ 56 public CustomJdbcTemplateWrapperTenant() throws Exception{
52 //获取泛型类Class 57 //获取泛型类Class
53 this.tClassInfo = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 58 this.tClassInfo = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
src/test/java/com/taover/repository/test/TestMapper.java 0 → 100644
@@ -0,0 +1,32 @@ @@ -0,0 +1,32 @@
  1 +package com.taover.repository.test;
  2 +
  3 +import java.util.Properties;
  4 +
  5 +import javax.sql.DataSource;
  6 +
  7 +import org.springframework.jdbc.core.JdbcTemplate;
  8 +
  9 +import com.alibaba.druid.pool.DruidDataSourceFactory;
  10 +import com.taover.repository.test.repository.CommonRegionRepository;
  11 +
  12 +public class TestMapper {
  13 + public static void main(String[] args) {
  14 + Properties properties = new Properties();
  15 + properties.setProperty(DruidDataSourceFactory.PROP_URL, "jdbc:mysql://rdsifmezqifmezqo.mysql.rds.aliyuncs.com:3306/bzyun_wxorder?characterEncoding=UTF-8");
  16 + properties.setProperty(DruidDataSourceFactory.PROP_USERNAME, "tylife");
  17 + properties.setProperty(DruidDataSourceFactory.PROP_PASSWORD, "lexi365");
  18 + properties.setProperty(DruidDataSourceFactory.PROP_INITIALSIZE, "5");
  19 + properties.setProperty(DruidDataSourceFactory.PROP_MAXACTIVE, "8");
  20 + DataSource ds = null;
  21 + try {
  22 + ds = new DruidDataSourceFactory().createDataSource(properties);
  23 + JdbcTemplate template = new JdbcTemplate(ds);
  24 + //System.out.println(template.queryForObject("select count(*) from wxorder_order", Integer.class));
  25 + CommonRegionRepository repo = new CommonRegionRepository(template);
  26 + System.out.println(repo.findListBySql("1=1"));
  27 + } catch (Exception e) {
  28 + // TODO Auto-generated catch block
  29 + e.printStackTrace();
  30 + }
  31 + }
  32 +}
src/test/java/com/taover/repository/test/repository/CommonRegionEntity.java 0 → 100644
@@ -0,0 +1,113 @@ @@ -0,0 +1,113 @@
  1 +package com.taover.repository.test.repository;
  2 +
  3 +import java.io.Serializable;
  4 +import java.util.List;
  5 +
  6 +import javax.persistence.Column;
  7 +import javax.persistence.Entity;
  8 +import javax.persistence.Id;
  9 +import javax.persistence.Table;
  10 +
  11 +/**
  12 + * @version 1.0.0
  13 + */
  14 +@Entity
  15 +@Table(name="common_region", catalog="")
  16 +public class CommonRegionEntity implements Serializable {
  17 +
  18 + private static final long serialVersionUID = 1L;
  19 +
  20 +
  21 + /**
  22 + * 主键ID
  23 + */
  24 + @Id
  25 + @Column(name="region_id")
  26 + private java.lang.Long regionId;
  27 +
  28 + public java.lang.Long getRegionId(){
  29 + return regionId;
  30 + }
  31 + public void setRegionId(java.lang.Long regionId){
  32 + this.regionId = regionId;
  33 + }
  34 +
  35 + /**
  36 + * 父级ID
  37 + */
  38 + @Column(name="parent_id")
  39 + private java.lang.Long parentId;
  40 +
  41 + public java.lang.Long getParentId(){
  42 + return parentId;
  43 + }
  44 + public void setParentId(java.lang.Long parentId){
  45 + this.parentId = parentId;
  46 + }
  47 +
  48 + /**
  49 + * 地区名称
  50 + */
  51 + @Column(name="region_name")
  52 + private java.lang.String regionName;
  53 +
  54 + public java.lang.String getRegionName(){
  55 + return regionName;
  56 + }
  57 + public void setRegionName(java.lang.String regionName){
  58 + this.regionName = regionName;
  59 + }
  60 +
  61 + /**
  62 + * 地区级别
  63 + */
  64 + @Column(name="region_type")
  65 + private java.lang.Integer regionType;
  66 +
  67 + public java.lang.Integer getRegionType(){
  68 + return regionType;
  69 + }
  70 + public void setRegionType(java.lang.Integer regionType){
  71 + this.regionType = regionType;
  72 + }
  73 +
  74 + /**
  75 + * 地区编码
  76 + */
  77 + @Column(name="region_code")
  78 + private java.lang.String regionCode;
  79 +
  80 + public java.lang.String getRegionCode(){
  81 + return regionCode;
  82 + }
  83 + public void setRegionCode(java.lang.String regionCode){
  84 + this.regionCode = regionCode;
  85 + }
  86 +
  87 + /**
  88 + * 大地区
  89 + */
  90 + @Column(name="large_area")
  91 + private java.lang.String largeArea;
  92 +
  93 + public java.lang.String getLargeArea(){
  94 + return largeArea;
  95 + }
  96 + public void setLargeArea(java.lang.String largeArea){
  97 + this.largeArea = largeArea;
  98 + }
  99 +
  100 + public transient List<CommonRegionEntity> children = null;
  101 +
  102 + public List<CommonRegionEntity> getChildren() {
  103 + return children;
  104 + }
  105 + public void setChildren(List<CommonRegionEntity> children) {
  106 + this.children = children;
  107 + }
  108 +
  109 + @Override
  110 + public String toString() {
  111 + return "CommonRegionEntity: [regionId="+regionId+",parentId="+parentId+",regionName="+regionName+",regionType="+regionType+",regionCode="+regionCode+",largeArea="+largeArea+"]";
  112 + }
  113 + }
src/test/java/com/taover/repository/test/repository/CommonRegionRepository.java 0 → 100644
@@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
  1 +package com.taover.repository.test.repository;
  2 +
  3 +import org.springframework.jdbc.core.JdbcTemplate;
  4 +
  5 +import com.taover.repository.CustomJdbcTemplate;
  6 +
  7 +public class CommonRegionRepository extends CustomJdbcTemplate<CommonRegionEntity, Long>{
  8 + public static int TYPE_PROVINCE = 1;
  9 + public static int TYPE_CITY = 2;
  10 + public static int TYPE_DISTRICT = 3;
  11 + public static Long PROVINCE_PARENT_ID = 1L;
  12 +
  13 + public CommonRegionRepository(JdbcTemplate jdbcTemplate) throws Exception {
  14 + super(jdbcTemplate);
  15 + }
  16 +}
  17 +