All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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 updateVals = new ArrayList(); Object primaryKeyVal = null;// plusMap更新对象是否有主键 for (Map.Entry entry : plusMap.entrySet()) { String key = entry.getKey();// map里的列名,可能是字段名 String colName = PlusUtils.getColNameByVar(key, plusCols.getCol2Var()); if (colName.equals(plusCols.getKey())) {// 主键的列名 primaryKeyVal = entry.getValue();// 主键的值 continue; } for (String col : plusCols.getCols()) { if (col.equals(colName)) {// 属于表的列才有效 updateCols.add(col); updateVals.add(entry.getValue()); } } } if (primaryKeyVal == null) { throw new PlusRuntimeException("更新对象没有主键或主键值为null"); } updateVals.add(primaryKeyVal); String sql = getUpdateSql(plusCols.getTableName(), plusCols.getKey(), updateCols); if (log.isDebugEnabled()) { log.debug(Constant.LogPre + sql); } Map resMap = new HashMap(); resMap.put("updateSql", sql); resMap.put("updateVals", updateVals); return resMap; } private String getUpdateSql(String tableName, String primaryKey, List cols) { final StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.append("update " + tableName + " set "); for (int i = 0; i < cols.size(); i++) { if (i == cols.size() - 1) { sqlBuilder.append(cols.get(i) + " = ?"); } else { sqlBuilder.append(cols.get(i) + " = ?" + ","); } } sqlBuilder.append(" where " + primaryKey + " = ?"); return sqlBuilder.toString(); } protected Object[] getUpdateVals(final PlusCols plusCols, final PlusVals plusVals) { List allVals = new ArrayList(); for (Object val : plusVals.getVals()) { allVals.add(val); } allVals.add(plusVals.getKeyVal()); Object vals[] = allVals.toArray(); if (log.isDebugEnabled()) { log.debug(Constant.LogPre + allVals); } return vals; } protected Object[] getInsertVals(final PlusCols plusCols, final PlusVals plusVals) { List allVals = new ArrayList(); if (plusCols.getStrategy().equals(Constant.AUTO)) {// 前端提供主键,增加插入主键字段 allVals.add(plusVals.getKeyVal()); } for (Object val : plusVals.getVals()) { allVals.add(val); } return allVals.toArray(); } protected PlusCols getPlusCols(Class entityClass) { PlusCols plusCols = PlusUtils.getPlusCols(entityClass); if (log.isDebugEnabled()) { log.debug(Constant.LogPre + plusCols); } return plusCols; } protected PlusVals getPlusVals(PlusCols plusCols, PlusPO entity) { PlusVals plusVals = PlusUtils.getPlusVals(plusCols, entity); if (log.isDebugEnabled()) { log.debug(Constant.LogPre + plusVals); } return plusVals; } }