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

com.kukababy.plus.dao.UpdateImpl Maven / Gradle / Ivy

The newest version!
package com.kukababy.plus.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.script.ScriptEngine;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;

import com.kukababy.plus.exception.PlusRuntimeException;
import com.kukababy.plus.pojo.SqlPo;
import com.kukababy.plus.pojo.SqlCfg;
import com.kukababy.plus.pojo.SqlVal;

/**
 * 
 * 描述:
 * 
* * @author [email protected] * @date 2019年3月5日 下午10:53:19 */ public class UpdateImpl extends BasePlus implements UpdateFace { private static final Logger log = LoggerFactory.getLogger(UpdateImpl.class); /** * @param injectParam */ public UpdateImpl(SqlCfg sqlCfg, ScriptEngine engine) { super(sqlCfg,engine); // TODO Auto-generated constructor stub } @Override public int update(Object entity) { SqlPo sqlPo = this.getSqlPo(entity.getClass(),sqlCfg.getCamel()); SqlVal sqlVal = this.getSqlVal(sqlPo, entity); if (sqlPo.getKey() == null || sqlVal.getKeyVal() == null) { throw new PlusRuntimeException("没有主键或主键值为NULL"); } String updateSql = this.getUpdateSql(sqlPo); Object allVals[] = this.getUpdateVals(sqlPo, sqlVal); return sqlCfg.getJdbcTemplate().update(updateSql, allVals); } @Override public int updateBatch(List entitys) { SqlPo sqlPo = this.getSqlPo(entitys.get(0).getClass(),sqlCfg.getCamel()); String sql = this.getUpdateSql(sqlPo); int counts[] = sqlCfg.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() { @Override public int getBatchSize() { return entitys.size(); } @Override public void setValues(PreparedStatement ps, int i) throws SQLException { SqlVal sqlVal = getSqlVal(sqlPo, entitys.get(i)); String sql = getUpdateSql(sqlPo); Object updateVals[] = getUpdateVals(sqlPo, sqlVal); for (int k = 0; k < updateVals.length; k++) { ps.setObject(k + 1, updateVals[k]); } } }); return counts.length; } @Override public int update(Class entityClass, Map plusMap) { if (plusMap == null || plusMap.isEmpty() || plusMap.size() < 2) { return 0; } SqlPo sqlPo = this.getSqlPo(entityClass,sqlCfg.getCamel()); if (sqlPo.getKey() == null) { throw new PlusRuntimeException("对象没有定义主键:" + entityClass.getName()); } Map resMap = getUpdateSqlAndVals(sqlPo, plusMap); String updateSql = (String) resMap.get("updateSql"); List updateVals = (List) resMap.get("updateVals"); return sqlCfg.getJdbcTemplate().update(updateSql, updateVals.toArray()); } }