Java搜索类
【摘要】
今天给大家介绍个好用的搜索类(上代码)
public class Search<T> {
/**
* like语句
*/
private final Ma...
今天给大家介绍个好用的搜索类(上代码)
public class Search<T> {
/**
* like语句
*/
private final Map<String, String> likeConditionMap = new HashMap<>();
/**
* 查询条件
*/
private final List<String> conditions = new ArrayList<>();
/**
* 查询的字段
*/
private final List<String> columns = new ArrayList<>();
/**
* 左模糊
*/
public final static String LIKE_LEFT = "left";
/**
* 右模糊
*/
public final static String LIKE_RIGHT = "right";
/**
* 全模糊
*/
public final static String LIKE_BOTH = "both";
public List<String> getConditions() {
return conditions;
}
public List<String> getColumns() {
return columns;
}
public Map<String, String> getLikeConditionMap() {
return likeConditionMap;
}
/**
* 添加模糊搜索
*
* @author fengzx
* @date 2020/3/28 12:12
*/
public Search<T> like(IGetter<T> fn, String type) {
//属性名
String propertyName = BeanUtils.convertToFieldName(fn);
String columnName = BeanUtils.HumpToUnderline(propertyName);
String condition = columnName + " like ";
if (LIKE_LEFT.equals(type)) {
condition = condition + "concat('%', ?)";
}
if (LIKE_RIGHT.equals(type)) {
condition = condition + "concat(?,'%')";
}
if (LIKE_BOTH.equals(type)) {
condition = condition + "concat('%',?,'%')";
}
columns.add(columnName);
likeConditionMap.put(columnName, condition);
return this;
}
/**
* 指定日期等于某个时间
*
* @author fengzx
* @date 2020/3/28 12:13
*/
public Search<T> eqDate(IGetter<T> fn, Date param, String pattern) {
compareDate(fn, param, pattern, " = '");
return this;
}
/**
* 指定日期等于某个时间
*
* @author fengzx
* @date 2020/3/28 12:13
*/
public Search<T> eqDate(IGetter<T> fn, String param) {
if (StringUtils.isBlank(param)) {
return this;
}
//属性名
String propertyName = BeanUtils.convertToFieldName(fn);
String columnName = BeanUtils.HumpToUnderline(propertyName);
String condition = columnName + " = '" + param + "'";
conditions.add(condition);
return this;
}
/**
* 指定日期小于某个时间
*
* @author fengzx
* @date 2020/3/28 12:14
*/
public Search<T> ltDate(IGetter<T> fn, String param) {
if (StringUtils.isBlank(param)) {
return this;
}
//属性名
String propertyName = BeanUtils.convertToFieldName(fn);
String columnName = BeanUtils.HumpToUnderline(propertyName);
String condition = columnName + " < '" + param + "'";
conditions.add(condition);
return this;
}
/**
* 指定日期小于某个时间
*
* @author fengzx
* @date 2020/3/28 12:14
*/
public Search<T> ltDate(IGetter<T> fn, Date param, String pattern) {
compareDate(fn, param, pattern, " < '");
return this;
}
/**
* 指定日期大于某个时间
*
* @author fengzx
* @date 2020/3/28 12:14
*/
public void gtDate(IGetter<T> fn, Date param, String pattern) {
compareDate(fn, param, pattern, " > '");
}
/**
* 日期比较
*
* @author fengzx
* @date 2020/4/11 12:26
*/
private Search<T> compareDate(IGetter<T> fn, Date param, String pattern, String compare) {
if (param == null || StringUtils.isBlank(pattern)) {
return this;
}
SimpleDateFormat format = new SimpleDateFormat(pattern);
String dateParam = format.format(param);
//属性名
String propertyName = BeanUtils.convertToFieldName(fn);
String columnName = BeanUtils.HumpToUnderline(propertyName);
String condition = columnName + compare + dateParam + "'";
conditions.add(condition);
return this;
}
/**
* 指定日期大于某个时间
*
* @author fengzx
* @date 2020/3/28 12:14
*/
public Search<T> gtDate(IGetter<T> fn, String param) {
if (StringUtils.isBlank(param)) {
return this;
}
//属性名
String propertyName = BeanUtils.convertToFieldName(fn);
String columnName = BeanUtils.HumpToUnderline(propertyName);
String condition = columnName + " > '" + param + "'";
conditions.add(condition);
return this;
}
}
- 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
使用方法
1.1自己写的搜索类进行实现
1.2 具体使用
只需要controller层具体字段加上具体模糊界限即可
(LEFT:左模糊 RIGHT:右模糊 BOTH:全模糊)
文章来源: baidaguo.blog.csdn.net,作者:白大锅,版权归原作者所有,如需转载,请联系作者。
原文链接:baidaguo.blog.csdn.net/article/details/118160255
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)