BaseModel.java
6.52 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* Copyright 2015-2025 FLY的狐狸(email:jflyfox@sina.com qq:369191470).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.jflyfox.jfinal.base;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;
import com.jflyfox.util.cache.Cache;
import com.jflyfox.util.cache.CacheManager;
/**
* Model 优化修改
*
* @author flyfox 2014-2-11
* @param <M>
*/
public class BaseModel<M extends Model<M>> extends Model<M> {
protected static final long serialVersionUID = 1L;
protected static final Log log = Log.getLog(BaseModel.class);
@Override
public Integer getInt(String attr) {
Object obj = get(attr);
if (obj == null) {
return null;
} else if (obj instanceof Integer) {
return (Integer) obj;
} else if (obj instanceof BigDecimal) {
return ((BigDecimal) obj).intValue();
} else if (obj instanceof String) {
try {
return Integer.parseInt((String) obj);
} catch (Exception e) {
throw new RuntimeException("String can not cast to Integer : " + attr.toString());
}
} else {
try {
return Integer.parseInt(obj.toString());
} catch (Exception e) {
throw new RuntimeException("Object can not cast to Integer : " + attr.toString());
}
}
}
public Table getTable() {
return TableMapping.me().getTable(getClass());
}
/**
* Paginate.
*
* @param paginator
* the page
* @param select
* the select part of the sql statement
* @param sqlExceptSelect
* the sql statement excluded select part
* @param paras
* the parameters of sql
* @return Page
*/
public Page<M> paginate(Paginator paginator, String select, String sqlExceptSelect, Object... paras) {
return paginate(paginator.getPageNo(), paginator.getPageSize(), select, sqlExceptSelect, paras);
}
/**
* Find model.
*
* @param where
* an SQL statement that may contain one or more '?' IN parameter
* placeholders
* @param paras
* the parameters of sql
* @return the list of Model
*/
public List<M> findByWhere(String where, Object... paras) {
return findByWhereAndColumns(where, "*", paras);
}
/**
* Find model.
*
* @param where
* an SQL statement that may contain one or more '?' IN parameter
* placeholders
* @param columns
* @param paras
* the parameters of sql
* @return the list of Model
*/
public List<M> findByWhereAndColumns(String where, String columns, Object... paras) {
String sql = " select " + columns + " from " + getTable().getName() + " " + where;
return find(sql, paras);
}
/**
* Find first model. I recommend add "limit 1" in your sql.
*
* @param where
* an SQL statement that may contain one or more '?' IN parameter
* placeholders
* @param paras
* the parameters of sql
* @return Model
*/
public M findFirstByWhere(String where, Object... paras) {
return findFirstByWhereAndColumns(where, "*", paras);
}
/**
* Find first model. I recommend add "limit 1" in your sql.
*
* @param where
* an SQL statement that may contain one or more '?' IN parameter
* placeholders
* @param columns
* @param paras
* the parameters of sql
* @return Model
*/
public M findFirstByWhereAndColumns(String where, String columns, Object... paras) {
String sql = " select " + columns + " from " + getTable().getName() + " " + where;
return findFirst(sql, paras);
}
// ///////////////////////////////////////缓存部分/////////////////////////////////////////////////
/**
* paginateCache是重构版paginateByCache,使用自己的Cache
*
* 2015年5月7日 下午12:20:23 flyfox 330627517@qq.com
*
* @param cacheName
* @param key
* @param paginator
* @param select
* @param sqlExceptSelect
* @param paras
* @return
*/
public Page<M> paginateCache(String cacheName, String key, Paginator paginator, String select,
String sqlExceptSelect, Object... paras) {
Cache cache = CacheManager.get(cacheName);
Page<M> result = cache.get(key);
if (result == null) {
result = paginate(paginator.getPageNo(), paginator.getPageSize(), select, sqlExceptSelect, paras);
cache.add(key, result);
}
return result;
}
/**
* findCache是重构版findByCache,使用自己的Cache
*
* 2015年5月7日 下午12:12:08 flyfox 330627517@qq.com
*
* @param cacheName
* @param key
* @param sql
* @param paras
* @return
*/
public List<M> findCache(String cacheName, String key, String sql, Object... paras) {
Cache cache = CacheManager.get(cacheName);
List<M> result = cache.get(key);
if (result == null) {
result = find(sql, paras);
cache.add(key, result);
}
return result;
}
/**
* findFirstCache是重构版findByCache,使用自己的Cache
*
* 2015年5月7日 下午12:12:08 flyfox 330627517@qq.com
*
* @param cacheName
* @param key
* @param sql
* @param paras
* @return
*/
public M findFirstCache(String cacheName, String key, String sql, Object... paras) {
Cache cache = CacheManager.get(cacheName);
M result = cache.get(key);
if (result == null) {
result = findFirst(sql, paras);
cache.add(key, result);
}
return result;
}
/**
* 根据Key删除Cache
*
* 2015年5月7日 下午12:16:43 flyfox 330627517@qq.com
*
* @param cacheName
* @param key
*/
public void removeCache(String cacheName, String key) {
Cache cache = CacheManager.get(cacheName);
cache.remove(key);
}
/**
* 清理Cache所有数据
*
* 2015年5月7日 下午12:16:52 flyfox 330627517@qq.com
*
* @param cacheName
*/
public void clearCache(String cacheName) {
Cache cache = CacheManager.get(cacheName);
cache.clear();
}
public Map<String, Object> getAttrs() {
return super._getAttrs();
}
public Object[] getAttrValues() {
return super._getAttrValues();
}
}