目的: 以前比较习惯使用Hibernate,后来觉得mybatis不能按我想要的自动组装为SQL查询条件,所以提供该工具类;
效果图:
如图所示,根据条件自动组装查询条件,下面来说一下实现方法:
1. ServiceImpl书写注意项
Page resultPage = null;
try {
PageHelper.startPage(pager.getCurrentPage(), pager.getPageSize());
// 判断是否有分页
if (ObjectHelper.isNotEmpty(pager.getDirection())
&& ObjectHelper.isNotEmpty(pager.getProperties())) {
specification.addOrderBy(pager.getProperties(),
pager.getDirection());
}
// 判断是否存在逻辑删除筛选
String sqlStr = specification.sql();
if (sqlStr.indexOf("deleted") == -1) {
specification.eq("deleted", "0");
}
resultPage = this.sysLoginMapper.page(specification.sql());
} catch (Exception e) {
result = Result.newFailure("数据错误", "在获取分页列表时发生异常。");
log.error(SimpleLogFormater.formatException(result.getMessage(), e));
return result;
}
2. Mapper.java 书写,查询条件非Map对象,直接就是SQL语句了;
/**
* 分页查询数据
*
* @return
*/
Page page(String sqlStr);
3. 关于XML的配置,会拼装SQL语句
附:SQL拼装工具类
/**
* @Description: (用一句话描述该文件做什么)
* @author heliang
* @date 2018-7-6 下午6:43:42
* @version V2.1
*/
package com.onem2.base.common;
import com.onem2.base.helper.ObjectHelper;
/**
* @ClassName: Specification
* @Description: (这里用一句话描述这个类的作用)
* @author heliang
* @date 2018-7-6 下午6:43:42
* @version V2.1 * Update Logs: * Name: * Date: * Description: 初始化
*/
public class Specification {
private StringBuilder where = new StringBuilder();
private String groupBy;
private String having;
private String orderBy;
public StringBuilder getWhere() {
return where;
}
public void setWhere(StringBuilder where) {
this.where = where;
}
public String getGroupBy() {
return groupBy;
}
public void setGroupBy(String groupBy) {
this.groupBy = groupBy;
}
public String getHaving() {
return having;
}
public void setHaving(String having) {
this.having = having;
}
public String getOrderBy() {
return orderBy;
}
public void setOrderBy(String orderBy) {
this.orderBy = orderBy;
}
public Specification addOrderBy(String sort, String order) {
if (!isEmpty(sort) && !isEmpty(order)) {
this.orderBy = ObjectHelper.underscoreName(sort) + " " + order;
}
return this;
}
public Specification orLike(String value, String columns) {
if (!isEmpty(value)) {
StringBuffer strBuf = new StringBuffer("");
for (String column : columns.split(",")) {
strBuf.append(ObjectHelper.underscoreName(column) + " like '%"
+ value + "%' or ");
}
String orLikeStr = strBuf.substring(0, strBuf.lastIndexOf("or"));
where.append(" and (" + orLikeStr + ")");
}
return this;
}
public Specification eq(String column, String value) {
if (!isEmpty(value)) {
where.append(" and " + ObjectHelper.underscoreName(column) + " = '"
+ sqlParam(value) + "'");
}
return this;
}
public Specification ne(String column, String value) {
if (!isEmpty(value)) {
where.append(" and " + ObjectHelper.underscoreName(column)
+ " != '" + sqlParam(value) + "'");
}
return this;
}
public Specification like(String column, String value) {
if (!isEmpty(value)) {
where.append(" and " + ObjectHelper.underscoreName(column)
+ " like '%" + sqlParam(value) + "%'");
}
return this;
}
public Specification notLike(String column, String value) {
if (!isEmpty(value)) {
where.append(" and " + ObjectHelper.underscoreName(column)
+ " not like '%" + sqlParam(value) + "%'");
}
return this;
}
public Specification in(String column, String... values) {
if (!isEmpty(values)) {
where.append(" and " + ObjectHelper.underscoreName(column)
+ " in (" + inValuesString(values) + ")");
}
return this;
}
public Specification notIn(String column, String... values) {
if (!isEmpty(values)) {
where.append(" and " + ObjectHelper.underscoreName(column)
+ " not in (" + inValuesString(values) + ")");
}
return this;
}
public Specification gt(String column, String value) {
if (!isEmpty(value)) {
where.append(" and " + ObjectHelper.underscoreName(column) + " > '"
+ sqlParam(value) + "'");
}
return this;
}
public Specification gte(String column, String value) {
if (!isEmpty(value)) {
where.append(" and " + ObjectHelper.underscoreName(column)
+ " >= '" + sqlParam(value) + "'");
}
return this;
}
public Specification lt(String column, String value) {
if (!isEmpty(value)) {
where.append(" and " + ObjectHelper.underscoreName(column) + " < '"
+ sqlParam(value) + "'");
}
return this;
}
public Specification lte(String column, String value) {
if (!isEmpty(value)) {
where.append(" and " + ObjectHelper.underscoreName(column)
+ " <= '" + sqlParam(value) + "'");
}
return this;
}
public Specification between(String column, String from, String to) {
if (isEmpty(from) && isEmpty(to)) {
return this;
}
if (isEmpty(to)) {
where.append(" and " + ObjectHelper.underscoreName(column)
+ " >= '" + sqlParam(from) + "'");
} else if (isEmpty(from)) {
where.append(" and " + ObjectHelper.underscoreName(column)
+ " <= '" + sqlParam(to) + "'");
} else {
where.append(" and " + ObjectHelper.underscoreName(column)
+ " between '" + sqlParam(from) + "' and '" + sqlParam(to)
+ "'");
}
return this;
}
public String sql() {
StringBuilder sql = new StringBuilder("");
final int a = 4;
final int b = 5;
if (where.length() > a) {
sql.append(" " + where.substring(b));
}
if (!isEmpty(groupBy)) {
sql.append(" group by " + groupBy);
}
if (!isEmpty(having)) {
sql.append(" having " + having);
}
if (!isEmpty(orderBy)) {
sql.append(" order by " + orderBy);
}
return sql.toString();
}
public String toString() {
return sql();
}
private static boolean isEmpty(String value) {
return value == null || "".equals(value) || value.trim().length() == 0;
}
private static boolean isEmpty(String[] values) {
if (values == null || values.length == 0) {
return true;
}
for (String value : values) {
if (!isEmpty(value)) {
return false;
}
}
return true;
}
private static String inValuesString(String[] values) {
StringBuilder string = new StringBuilder();
for (String value : values) {
if (isEmpty(value)) {
continue;
}
string.append('\'');
string.append(value);
string.append('\'');
string.append(',');
}
if (string.length() > 0) {
string.deleteCharAt(string.length() - 1);
}
return string.toString();
}
private static String sqlParam(String sqlParam) {
return sqlParam.replaceAll("([';]+|(--)+)", "");
}
}
附:ObjectHelper 工具源码:
package com.onem2.base.helper;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Object帮助类 功能:此类提供处理
这样就可以做到动态生成查询条件,复杂的查询条件也不会去改动XML配置了。