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

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

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

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.script.ScriptEngine;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

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

/**
 * 
 * 描述:
 * 
* * @author [email protected] * @date 2019年3月5日 下午10:52:20 */ public class InsertImpl extends BasePlus implements InsertFace { private static final Logger log = LoggerFactory.getLogger(InsertImpl.class); /** * @param injectParam */ public InsertImpl(SqlCfg sqlCfg, ScriptEngine engine) { super(sqlCfg,engine); // TODO Auto-generated constructor stub } @Override public void insert(Object entity) { SqlPo sqlPo = this.getSqlPo(entity.getClass(), sqlCfg.getCamel()); SqlVal sqlVal = this.getSqlVal(sqlPo, entity); String sql = this.getInsertSql(sqlPo); if (sqlPo.getStrategy().equals(Constant.IDENTITY)) { insertIdentity(sqlPo, sqlVal, entity, sql); } else { Object allVals[] = getInsertVals(sqlPo, sqlVal); sqlCfg.getJdbcTemplate().update(sql, allVals); } } private void insertIdentity(SqlPo sqlPo, SqlVal sqlVal, Object entity, final String sql) { KeyHolder key = insertIdentityHandle(sql, sqlPo, sqlVal); Number val = 0; if (sqlPo.getKeyType() == Constant.INT) { val = key.getKey().intValue(); } else { val = key.getKey().longValue(); } SqlUtil.setValue(entity, sqlPo.getCol2VarMap().get(sqlPo.getKey()), val); } private KeyHolder insertIdentityHandle(final String sql, final SqlPo sqlPo, final SqlVal sqlVal) { Object allVals[] = getInsertVals(sqlPo, sqlVal); KeyHolder key = new GeneratedKeyHolder(); sqlCfg.getJdbcTemplate().update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); for (int i = 0; i < allVals.length; i++) { ps.setObject(i + 1, allVals[i]); } return ps; } }, key); return key; } /** * */ @Override public int insertBatch(List entitys) { SqlPo sqlPo = this.getSqlPo(entitys.get(0).getClass(), sqlCfg.getCamel()); if (sqlPo.getStrategy().equals(Constant.IDENTITY)) { return insertBatchIdentity(sqlPo, entitys); } else { return insertBatchNorml(sqlPo, entitys); } } /** * * 描述: 主键值由前端给定
*
* * @param plusCols * @param entitys * @return * @throws SQLException */ private int insertBatchIdentity(SqlPo sqlPo, List entitys) { String sql = this.getInsertSql(sqlPo); Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; List idVals = new ArrayList(); try { con = sqlCfg.getJdbcTemplate().getDataSource().getConnection(); con.setAutoCommit(false); pstmt = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS); for (Object plusPO : entitys) { SqlVal sqlVal = getSqlVal(sqlPo, plusPO); Object allVals[] = getInsertVals(sqlPo, sqlVal); for (int k = 0; k < allVals.length; k++) { pstmt.setObject(k + 1, allVals[k]); } pstmt.addBatch(); } pstmt.executeBatch(); con.commit(); rs = pstmt.getGeneratedKeys(); // 获取结果 while (rs.next()) { if (sqlPo.getKeyType() == Constant.INT) { idVals.add(rs.getInt(1)); } else { idVals.add(rs.getLong(1)); } } } catch (SQLException e) { throw new PlusRuntimeException(e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } for (int i = 0; i < idVals.size(); i++) { SqlUtil.setValue(entitys.get(i), sqlPo.getCol2VarMap().get(sqlPo.getKey()), idVals.get(i)); } return entitys.size(); } private int insertBatchNorml(SqlPo sqlPo, List entitys) { String sql = this.getInsertSql(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)); Object allVals[] = getInsertVals(sqlPo, sqlVal); for (int k = 0; k < allVals.length; k++) { ps.setObject(k + 1, allVals[k]); } } }); return counts.length; } }