Skip to content

Commit b2e772d

Browse files
author
WeiziPlus
committed
1.1.3 优化封装单表查询
1 parent 3ba23c9 commit b2e772d

File tree

12 files changed

+459
-282
lines changed

12 files changed

+459
-282
lines changed

springboot/demo-common/src/main/java/com/weiziplus/common/base/BaseService.java

Lines changed: 83 additions & 149 deletions
Large diffs are not rendered by default.
Lines changed: 97 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,138 @@
11
package com.weiziplus.common.base;
22

3+
import com.weiziplus.common.util.ToolUtils;
34
import lombok.Getter;
45
import lombok.experimental.Accessors;
56

67
import java.io.Serializable;
8+
import java.util.ArrayList;
9+
import java.util.List;
710

811
/**
9-
* sql字段与条件和值
12+
* 查询条件工具
1013
*
1114
* @author wanglongwei
12-
* @date 2020/06/03 14/57
15+
* @date 2020/07/28 16/35
1316
*/
1417
@Getter
1518
@Accessors(chain = true)
16-
public class BaseWhere implements Serializable {
19+
public class BaseWhere<T> extends BaseService implements Serializable {
1720

1821
private static final long serialVersionUID = 1L;
1922

2023
/**
21-
* 数据库字段
24+
* 实体类的class
2225
*/
23-
private String column;
26+
private Class<T> modelClass;
2427

2528
/**
26-
* where条件
29+
* 查询的条件
2730
*/
28-
private String where = "=";
31+
private List<BaseWhereModel> baseWhereModels;
2932

3033
/**
31-
*
34+
* 排序
3235
*/
33-
private Object value;
36+
private String orderBy;
3437

35-
public BaseWhere() {
38+
/**
39+
* 实例化
40+
*/
41+
private BaseWhere() {
3642

3743
}
3844

3945
/**
40-
* 创建where
46+
* 实例化
4147
*
42-
* @param column
43-
* @param where
44-
* @param value
48+
* @param clazz
4549
*/
46-
public BaseWhere(String column, String where, Object value) {
47-
this.column = column;
48-
this.where = where;
49-
this.value = value;
50+
public BaseWhere(Class<T> clazz) {
51+
this.modelClass = clazz;
5052
}
5153

52-
@Getter
53-
public enum Where {
54-
/**
55-
* sql条件
56-
*/
57-
EQUAL("等于", "="),
58-
NOT_EQUAL("不等于", "<>"),
59-
MORE_THAN("大于", ">"),
60-
LESS_THAN("小于", "<"),
61-
MORE_THAN_EQUAL("大于等于", ">="),
62-
LESS_THAN_EQUAL("小于等于", "<="),
63-
IN("IN", "IN"),
64-
NOT_IN("NOT_IN", "NOT IN"),
65-
POSITION("POSITION", "POSITION");
54+
/**
55+
* 添加查询条件
56+
*
57+
* @param baseWhereModel
58+
* @return
59+
*/
60+
public BaseWhere<T> where(BaseWhereModel baseWhereModel) {
61+
List<BaseWhereModel> oldList = this.getBaseWhereModels();
62+
if (null == oldList) {
63+
oldList = new ArrayList<>(ToolUtils.initialCapacity(1));
64+
}
65+
oldList.add(baseWhereModel);
66+
this.baseWhereModels = oldList;
67+
return this;
68+
}
6669

67-
private String name;
68-
private String value;
70+
/**
71+
* 添加查询条件数组
72+
*
73+
* @param baseWhereModelList
74+
* @return
75+
*/
76+
public BaseWhere<T> where(List<BaseWhereModel> baseWhereModelList) {
77+
List<BaseWhereModel> oldList = this.getBaseWhereModels();
78+
if (null == oldList) {
79+
oldList = new ArrayList<>(ToolUtils.initialCapacity(1));
80+
}
81+
oldList.addAll(baseWhereModelList);
82+
this.baseWhereModels = oldList;
83+
return this;
84+
}
6985

70-
Where(String name, String value) {
71-
this.name = name;
72-
this.value = value;
86+
/**
87+
* 倒叙
88+
*
89+
* @param columns
90+
* @return
91+
*/
92+
public BaseWhere<T> descOrderBy(String... columns) {
93+
if (null == columns || 0 >= columns.length) {
94+
return this;
95+
}
96+
String oldOrderBy = this.getOrderBy();
97+
if (null == oldOrderBy) {
98+
oldOrderBy = "";
99+
}
100+
StringBuilder orderBy = new StringBuilder(oldOrderBy);
101+
for (String column : columns) {
102+
//判断实体类是否包含该字段
103+
if (!classIsContainsColumn(this.modelClass, column)) {
104+
throw new RuntimeException("当前实体类" + this.modelClass + "找不到该字段" + column + ";请使用实体类的静态常量");
105+
}
106+
orderBy.append(column).append(" DESC, ");
73107
}
108+
this.orderBy = orderBy.toString();
109+
return this;
110+
}
74111

75-
/**
76-
* 是否存在
77-
*
78-
* @param value
79-
* @return
80-
*/
81-
public static boolean contains(String value) {
82-
for (Where where : Where.values()) {
83-
if (where.getValue().equals(value)) {
84-
return true;
85-
}
112+
/**
113+
* 正序
114+
*
115+
* @param columns
116+
* @return
117+
*/
118+
public BaseWhere<T> ascOrderBy(String... columns) {
119+
if (null == columns || 0 >= columns.length) {
120+
return this;
121+
}
122+
String oldOrderBy = this.getOrderBy();
123+
if (null == oldOrderBy) {
124+
oldOrderBy = "";
125+
}
126+
StringBuilder orderBy = new StringBuilder(oldOrderBy);
127+
for (String column : columns) {
128+
//判断实体类是否包含该字段
129+
if (!classIsContainsColumn(this.modelClass, column)) {
130+
throw new RuntimeException("当前实体类" + this.modelClass + "找不到该字段" + column + ";请使用实体类的静态常量");
86131
}
87-
return false;
132+
orderBy.append(column).append(" ASC, ");
88133
}
134+
this.orderBy = orderBy.toString();
135+
return this;
89136
}
90137

91-
}
138+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.weiziplus.common.base;
2+
3+
import lombok.Getter;
4+
5+
/**
6+
* where条件枚举
7+
*
8+
* @author wanglongwei
9+
* @date 2020/07/28 17/46
10+
*/
11+
@Getter
12+
public enum BaseWhereEnum {
13+
/**
14+
* sql条件
15+
*/
16+
EQUAL("等于", "="),
17+
NOT_EQUAL("不等于", "<>"),
18+
MORE_THAN("大于", ">"),
19+
LESS_THAN("小于", "<"),
20+
MORE_THAN_EQUAL("大于等于", ">="),
21+
LESS_THAN_EQUAL("小于等于", "<="),
22+
IN("IN", "IN"),
23+
NOT_IN("NOT_IN", "NOT IN"),
24+
POSITION("POSITION", "POSITION");
25+
26+
private String name;
27+
private String value;
28+
29+
BaseWhereEnum(String name, String value) {
30+
this.name = name;
31+
this.value = value;
32+
}
33+
34+
/**
35+
* 是否存在
36+
*
37+
* @param value
38+
* @return
39+
*/
40+
public static boolean contains(String value) {
41+
for (BaseWhereEnum where : BaseWhereEnum.values()) {
42+
if (where.getValue().equals(value)) {
43+
return true;
44+
}
45+
}
46+
return false;
47+
}
48+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.weiziplus.common.base;
2+
3+
import jdk.nashorn.internal.objects.annotations.Where;
4+
import lombok.Getter;
5+
import lombok.experimental.Accessors;
6+
7+
import java.io.Serializable;
8+
9+
/**
10+
* sql字段与条件和值
11+
*
12+
* @author wanglongwei
13+
* @date 2020/06/03 14/57
14+
*/
15+
@Getter
16+
@Accessors(chain = true)
17+
public class BaseWhereModel implements Serializable {
18+
19+
private static final long serialVersionUID = 1L;
20+
21+
/**
22+
* 数据库字段
23+
*/
24+
private String column;
25+
26+
/**
27+
* where条件,默认等于=
28+
*/
29+
private BaseWhereEnum where = BaseWhereEnum.EQUAL;
30+
31+
/**
32+
* 值
33+
*/
34+
private Object value;
35+
36+
private BaseWhereModel() {
37+
38+
}
39+
40+
/**
41+
* 创建where
42+
*
43+
* @param column
44+
* @param where 默认为 等于=
45+
* @param value
46+
*/
47+
public BaseWhereModel(String column, BaseWhereEnum where, Object value) {
48+
this.column = column;
49+
this.where = where;
50+
this.value = value;
51+
}
52+
53+
/**
54+
* 创建where,默认为等于
55+
*
56+
* @param column
57+
* @param value
58+
*/
59+
public BaseWhereModel(String column, Object value) {
60+
this.column = column;
61+
this.value = value;
62+
}
63+
64+
}

0 commit comments

Comments
 (0)