BaseServiceImpl.java
4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.viontech.fanxing.commons.base;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class BaseServiceImpl<T extends BaseModel> implements BaseService<T> {
/**
* slf4j 日志对象 用来记录log
*/
protected Logger logger = LoggerFactory.getLogger(getClass());
private Map<String, String> columnNamePropertyMap = null;
public T selectByPrimaryKey(Object id) {
return getMapper().selectByPrimaryKey(id);
}
public T insert(T record) {
getMapper().insert(record);
return record;
}
@Override
public T insertSelective(T record) {
getMapper().insertSelective(record);
return record;
}
@Override
public int deleteByPrimaryKey(Object b) {
return getMapper().deleteByPrimaryKey(b);
}
@Override
public int updateByPrimaryKey(T record) {
return getMapper().updateByPrimaryKey(record);
}
@Override
public int updateByPrimaryKeySelective(T record) {
return getMapper().updateByPrimaryKeySelective(record);
}
@Override
public PageInfo<T> pagedQuery(BaseExample example, int pageNum, int pageSize) {
if (pageSize > 0) {
PageHelper.startPage(pageNum, pageSize);
Page<T> result = (Page<T>) getMapper().selectByExample(example);
return new PageInfo<T>(result);
} else {
List<T> result = getMapper().selectByExample(example);
Page<T> p = new Page<T>();
p.addAll(result);
return new PageInfo<T>(p);
}
}
@Override
public int countByExample(BaseExample example) {
return getMapper().countByExample(example);
}
@Override
public int deleteByExample(BaseExample example) {
return getMapper().deleteByExample(example);
}
@Override
public List<T> selectByExample(BaseExample example) {
return getMapper().selectByExample(example);
}
@Override
public int updateByExampleSelective(T record, BaseExample example) {
return getMapper().updateByExampleSelective(record, example);
}
@Override
public int updateByExample(T record, BaseExample example) {
return getMapper().updateByExample(record, example);
}
@Override
public List<Object> getIdsByByExample(BaseExample example) {
List<T> list = selectByExample(example);
List<Object> result = new ArrayList<Object>();
for (T t : list) {
Object pk = getPKByModel(t);
if (pk != null) {
result.add(pk);
}
}
return result;
}
@Override
public Object getPKByModel(BaseModel baseModel) {
throw new UnsupportedOperationException();
}
public String getColumnNameByProperty(String property) {
if (columnNamePropertyMap == null) {
columnNamePropertyMap = getColumnNamePropertyMap();
}
return columnNamePropertyMap.get(property);
}
public Map getColumnNamePropertyMap() {
Map<String, String> result = new HashMap<String, String>();
try {
SqlSession sqlSession = getSqlSessionByMapper(getMapper());
ResultMap resultMap = sqlSession.getConfiguration().getResultMap(getMapperClassByMapper(getMapper()).getName() + ".BaseResultMapRoot");
List<ResultMapping> propertyResultMappings = resultMap.getPropertyResultMappings();
for (ResultMapping resultMapping : propertyResultMappings) {
result.put(resultMapping.getProperty(), resultMapping.getColumn());
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private SqlSession getSqlSessionByMapper(BaseMapper mapper) throws Exception {
Object mapperProxy = ((Proxy) mapper).getInvocationHandler(mapper);
Field sqlSession = mapperProxy.getClass().getDeclaredField("sqlSession");
sqlSession.setAccessible(true);
return (SqlSession) sqlSession.get(mapperProxy);
}
private Class getMapperClassByMapper(BaseMapper mapper) throws Exception {
Object mapperProxy = ((Proxy) mapper).getInvocationHandler(mapper);
Field mapperInterface = mapperProxy.getClass().getDeclaredField("mapperInterface");
mapperInterface.setAccessible(true);
return (Class) mapperInterface.get(mapperProxy);
}
}