为了防止sql注入我已经把所有生成sql的地方,加了’’,将他全部转成了文本,所以请勿担心sql注入的问题
java工具类
@Data
public class BaseCondition implements Cloneable {
/**
* where条件
*/
@Getter
private String criteria;
/**
* orderBy
*/
private String orderByColumn;
private String orderByType;
/**
* groupBy
*/
private String groupBy;
/**
* 分页
*/
private Integer start;
private Integer length;
public BaseCondition() {
criteria = "";
}
/**
* 手动添加条件语句
*
* @param condition
* @return
*/
public BaseCondition andCondition(String condition) {
criteria += " and " + condition;
return this;
}
/**
* 字段排序
*
* @param column 哪一列
* @param type 哪种形式
* @return
*/
public BaseCondition orderBy(String column, String type) {
orderByColumn = column;
orderByType = type;
return this;
}
/**
* 分页
*
* @param pageNum
* @param length
* @return
*/
public BaseCondition limit(int pageNum, int length) {
if(pageNum<1){
pageNum = 1;
}
this.start = (pageNum - 1) * length;
this.length = length;
return this;
}
/**
* isnull
*
* @param property 字段
* @return
*/
public BaseCondition andIsNull(String property) {
criteria += " and " + property + " is null";
return this;
}
/**
* isNotNull
*
* @param property 字段
* @return
*/
public BaseCondition andIsNotNull(String property) {
criteria += " and " + property + " is not null";
return this;
}
/**
* =
*
* @param property 字段
* @param value 值
* @return
*/
public BaseCondition andEqualTo(String property, Object value) {
criteria += " and " + property + " = '" + value + "'";
return this;
}
/**
* !=
*
* @param property 字段
* @param value 值
* @return
*/
public BaseCondition andNotEqualTo(String property, Object value) {
criteria += " and " + property + " != '" + value + "'";
return this;
}
/**
* >
*
* @param property 字段
* @param value 值
* @return
*/
public BaseCondition andGreaterThan(String property, Object value) {
if(value instanceof Date){
value = DateUtils.format((Date)value);
}
criteria += " and " + property + " > '" + value + "'";
return this;
}
/**
* >=
*
* @param property 字段
* @param value 值
* @return
*/
public BaseCondition andGreaterThanOrEqualTo(String property, Object value) {
if(value instanceof Date){
value = DateUtils.format((Date)value);
}
criteria += " and " + property + " >= '" + value + "'";
return this;
}
/**
* <
*
* @param property 字段
* @param value 值
* @return
*/
public BaseCondition andLessThan(String property, Object value) {
if(value instanceof Date){
value = DateUtils.format((Date)value);
}
criteria += " and " + property + " < '" + value + "'";
return this;
}
/**
* <=
*
* @param property 字段
* @param value 值
* @return
*/
public BaseCondition andLessThanOrEqualTo(String property, Object value) {
if(value instanceof Date){
value = DateUtils.format((Date)value);
}
criteria += " and " + property + " <= '" + value + "'";
return this;
}
/**
* in
*
* @param property 字段
* @param values 值
* @return
*/
public BaseCondition andIn(String property, List values) {
criteria += " and " + property + " in " + " (" + StringUtils.join(values.toArray(), ",") + ")";
return this;
}
/**
* not in
*
* @param property 字段
* @param values 值
* @return
*/
public BaseCondition andNotIn(String property, List values) {
criteria += " and " + property + " not in " + " (" + StringUtils.join(values.toArray(), ",") + ")";
return this;
}
/**
* between 范围选取 前包后不包
*
* @param property
* @param value1
* @param value2
* @return
*/
public BaseCondition andBetween(String property, Object value1, Object value2) {
criteria += " and " + property + " between '" + value1 + "'" + " and '" + value2 + "'";
return this;
}
/**
* not between 范围选取 前包后不包
*
* @param property
* @param value1
* @param value2
*/
public void andNotBetween(String property, Object value1, Object value2) {
criteria += " and " + property + " not between '" + value1 + "'" + " and '" + value2 + "'";
}
/**
* like
*
* @param property
* @param value
* @return
*/
public BaseCondition andLike(String property, String value) {
criteria += " and " + property + " like '" + value + "'";
return this;
}
/**
* not like
*
* @param property
* @param value
* @return
*/
public BaseCondition andNotLike(String property, String value) {
criteria += " and " + property + " not like '" + value + "'";
return this;
}
/**
* 去除条件语句最前面的and字符
*
* @return
*/
public String getCriteria() {
if (criteria != null && criteria.length() > 3) {
return criteria.substring(4);
}
return null;
}
public String getOrderByColumn() {
return "'" + orderByColumn + "'";
}
public String getGroupBy() {
return "'" + groupBy + "'";
}
public int getStart() {
if(start == null){
return 0;
}
return start;
}
public int getLength() {
if(length == null){
return 0;
}
return length;
}
@Override
public BaseCondition clone()
{
Object object = null;
try {
object = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return (BaseCondition)object;
}
使用方法
mapper sql 写法
注:orderBy 不可以拼接只能手写了(如果有好的解决办法欢迎联系我,分享给大家)
select
字段
from 表
left join 另一个表 on 关联条件
${criteria}
order by 字段
limit #{start},#{length}
\
java service写法
BaseCondition baseCondition = new BaseCondition();
baseCondition.andEqualTo(字段, 字段值);
baseCondition.limit(pageNum,pageSize);