Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.webapp.mybatis.helper.DaoHelper Maven / Gradle / Ivy
package com.webapp.mybatis.helper;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.jdbc.SQL;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.webapp.mybatis.helper.Where.Op;
import com.webapp.mybatis.helper.Where.Order;
import com.webapp.utils.string.Utils;
public class DaoHelper {
private static final String pkey = "key";
private static final String pval = "val";
private static DateFormat df = DateFormat.getDateTimeInstance();
protected enum Like{
LEFT, RIGHT, ALL;
}
protected enum AndOr{
AND("AND"), OR("OR");
private String andOr;
AndOr(String andOr){
this.andOr = andOr;
}
public String toString() {
return this.andOr.toString();
}
}
protected enum Keys{
ORDER("ORDER BY"), LIMIT("LIMIT");
private String keys;
Keys(String keys){
this.keys = keys;
}
public String toString() {
return this.keys.toString();
}
}
public static String wrap(SQL base, Where where){
return String.format("%1$s %2$s", base.toString(), where.toSql());
}
public static String kv(Map param) {
return param.get(pkey) + String.format("=#{%s}", pval);
}
public static String exp(T model) {
return exp(model, " " + AndOr.AND.name());
}
public static String upd(T model) {
return exp(model, ",");
}
private static String exp(T model, String spilt) {
JSONObject json = JSON.parseObject(JSON.toJSONString(model));
StringBuffer data = new StringBuffer();
json.forEach((key,val)->data.append(String.format("%1$s=#{%2$s}%3$s", Utils.toSnake(key), key, spilt)).append(" "));
return Utils.delTail(data.toString().trim(), spilt);
}
protected static String limits(int index, int count){
return String.format("%1$s %2$s,%3$s", Keys.LIMIT, index, count);
}
protected static String orderBy(Order order, String ...name){
String names = Utils.split(Arrays.asList(name));
return String.format("%1$s %2$s %3$s", Keys.ORDER, names, order.name());
}
protected static String between(AndOr andOr, String name, Object min, Object max){
return String.format("%1$s %2$s %3$s %4$s AND %5$s", andOr, name, Op.BETWEEN, fmt(min), fmt(max));
}
protected static String in(AndOr andOr, String name, boolean isNot, Object ...vals){
List asList = Arrays.asList(vals);
StringBuffer val = new StringBuffer();
val.append(String.format("%1$s %2$s %3$s (", andOr, name, isNot ? Op.NIN : Op.IN));
asList.forEach(x->val.append(fmt(x) + ","));
return Utils.delTail(val.toString(), ",").trim() + ")";
}
protected static String like(AndOr andOr, String name, boolean isNot, Like like, String val){
StringBuffer likes = new StringBuffer();
likes.append(String.format("%1$s %2$s %3$s", andOr, name, isNot ? Op.NLike : Op.LIKE)).append(" ");
if(like.equals(Like.LEFT)) likes.append(String.format("'%1$s%2$s'", val, "%"));
if(like.equals(Like.RIGHT)) likes.append(String.format("'%1$s%2$s'", "%", val));
if(like.equals(Like.ALL)) likes.append(String.format("'%1$s%2$s%3$s'", "%", val, "%"));
return likes.toString();
}
protected static String where(T model, String spilt) {
JSONObject json = JSON.parseObject(JSON.toJSONString(model));
StringBuffer data = new StringBuffer();
json.forEach((key,val)->data.append(String.format("%1$s=%2$s %3$s", Utils.toSnake(key), fmt(val), spilt)).append(" "));
return Utils.delTail(data.toString().trim(), spilt);
}
protected static String where(AndOr andOr, String name, Op op, Object val){
return String.format("%1$s %2$s %3$s %4$s", andOr, name, op, fmt(val));
}
protected static String where(AndOr andOr, String name, Op op){
return String.format("%1$s %2$s %3$s", andOr, name, op);
}
protected static String all(AndOr andOr, String name, Op op, Object ...vals){
String where = "";
if(op.equals(Op.BETWEEN)){
if(vals.length != 2) throw new RuntimeException("Parameter number is not correct");
where = DaoHelper.between(AndOr.AND, name, vals[0], vals[1]);
}else if(op.equals(Op.ISNULL) || op.equals(Op.NISNULL)){
where = DaoHelper.where(AndOr.AND, name, op);
}else if(op.equals(Op.IN) || op.equals(Op.NIN)){
where = DaoHelper.in(AndOr.AND, name, op.equals(Op.NIN) ? false : true, vals);
}else {
if(vals.length != 1) throw new RuntimeException("Parameter number is not correct");
where = DaoHelper.where(AndOr.AND, name, op, vals[0]);
}
return where;
}
private static String fmt(Object val){
String kv = "";
if(val instanceof Date){
kv += String.format("'%s'", df.format(val));
}else if(val instanceof String){
kv += String.format("'%s'", val);
}else {
kv += String.format("%s", val);
}
return kv;
}
}