com.kukababy.plus.dao.BasePlus Maven / Gradle / Ivy
/**
*
*/
package com.kukababy.plus.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.kukababy.plus.exception.PlusRuntimeException;
import com.kukababy.plus.pojo.PlusCols;
import com.kukababy.plus.pojo.PlusMap;
import com.kukababy.plus.pojo.PlusPO;
import com.kukababy.plus.pojo.PlusParam;
import com.kukababy.plus.pojo.PlusVals;
import com.kukababy.plus.utils.Constant;
import com.kukababy.plus.utils.PlusUtils;
/**
*
* 描述:
*
*
* @author [email protected]
* @date 2019年3月5日 下午10:51:13
*/
public class BasePlus {
private static final Logger log = LoggerFactory.getLogger(BasePlus.class);
protected PlusParam plusParam;
public BasePlus(PlusParam plusParam) {
this.plusParam = plusParam;
this.plusParam.setDbType(this.plusParam.getDbType().toLowerCase());
}
protected String getInsertSql(PlusCols plusCols) {
final StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("insert into " + plusCols.getTableName() + " (");
List allCols = new ArrayList();
if (plusCols.getStrategy().equals(Constant.AUTO)) {// 前端提供主键,增加插入主键字段
allCols.add(plusCols.getKey());
}
for (String col : plusCols.getCols()) {
allCols.add(col);
}
for (int i = 0; i < allCols.size(); i++) {
if (i == allCols.size() - 1) {
sqlBuilder.append(allCols.get(i) + ") values(");
} else {
sqlBuilder.append(allCols.get(i) + ",");
}
}
for (int i = 0; i < allCols.size(); i++) {
if (i == allCols.size() - 1) {
sqlBuilder.append("?)");
} else {
sqlBuilder.append("?,");
}
}
String sql = sqlBuilder.toString();
if (log.isDebugEnabled()) {
log.debug(Constant.LogPre + sql);
}
return sql;
}
protected String getUpdateSql(PlusCols plusCols) {
final StringBuilder sqlBuilder = new StringBuilder();
List updateCols = new ArrayList();
for (String col : plusCols.getCols()) {
updateCols.add(col);
}
String sql = getUpdateSql(plusCols.getTableName(), plusCols.getKey(), updateCols);
if (log.isDebugEnabled()) {
log.debug(Constant.LogPre + sql);
}
return sql;
}
protected Map getUpdateSqlAndVals(PlusCols plusCols, PlusMap plusMap) {
List updateCols = new ArrayList();// 实际需要更新的列
List