_BaseModel.java
3.1 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
package com.teplot.common;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
/**
* Depiction:
* <p>
* Modify:
* <p>
* Author: Kevin Lynn
* <p>
* Create Date:2016年7月29日 下午9:01:49
* <p>
*
* @version 1.0
* @since 1.0
*/
public abstract class _BaseModel<M extends _BaseModel<M>> extends Model<M> {
private static final long serialVersionUID = 5181566137071912778L;
/**
* 数据库表名
*
* @return
*/
public String table() {
return getClass().getSimpleName();
}
public java.sql.Timestamp currentTimestamp() {
java.util.Date date = new java.util.Date();
java.sql.Timestamp tt = new java.sql.Timestamp(date.getTime());
return tt;
}
public M searchFirst() {
return findFirst("select * from " + table());
}
public List<M> searchAll() {
return find("select * from " + table());
}
private void checkTableName() {
if (StrKit.isBlank(table()))
throw new IllegalArgumentException("tableName can not be blank,please setTableName(tableName)");
}
public M searchFirst(String key, Object value) {
List<M> mList = search(key, value, "");
return mList != null && mList.size() > 0 ? mList.get(0) : null;
}
public List<M> search(String key, Object value) {
return search(key, value, "");
}
public List<M> search(String key, Object value, String orderBy) {
checkTableName();
String sql = "select * from " + table() + " where " + key + "=? " + orderBy;
return find(sql, value);
}
public M searchFirst(Map<String, Object> maps) {
List<M> mList = search(maps, "");
return mList != null && mList.size() > 0 ? mList.get(0) : null;
}
public List<M> search(Map<String, Object> maps) {
return search(maps, "");
}
public List<M> search(Map<String, Object> maps, String orderBy) {
checkTableName();
StringBuilder sb = new StringBuilder();
sb.append("select * from ").append(table()).append(" where 1=1 ");
List<Object> values = new ArrayList<Object>();
for (Entry<String, Object> entry : maps.entrySet()) {
if (entry.getValue() != null) {
sb.append(" and ").append(entry.getKey()).append("=?");
values.add(entry.getValue());
}
}
sb.append(" ").append(orderBy);
return find(sb.toString(), values.toArray());
}
public Page<M> searchPaginate(int pageNumber, int pageSize, Map<String, Object> maps) {
return this.searchPaginate(pageNumber, pageSize, maps, "");
}
public Page<M> searchPaginate(int pageNumber, int pageSize, Map<String, Object> maps, String orderBy) {
checkTableName();
StringBuilder sb = new StringBuilder();
sb.append("from ").append(table()).append(" where 1=1");
List<Object> values = new ArrayList<Object>();
for (Entry<String, Object> entry : maps.entrySet()) {
if (entry.getValue() != null) {
sb.append(" and ").append(entry.getKey()).append("=?");
values.add(entry.getValue());
}
}
sb.append(orderBy);
return paginate(pageNumber, pageSize, "select *", sb.toString(), values.toArray());
}
@Override
public String toString() {
return toJson();
}
}