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

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

/**
 * 
 */
package com.kukababy.plus.dao;

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

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.pojo.PlusPO;
import com.kukababy.plus.pojo.PlusParam;
import com.kukababy.plus.pojo.PlusCols;
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:52:20 */ public class InsertImpl extends BasePlus implements InsertFace { private static final Logger log = LoggerFactory.getLogger(InsertImpl.class); /** * @param injectParam */ public InsertImpl(PlusParam injectParam) { super(injectParam); // TODO Auto-generated constructor stub } @Override public void insert(PlusPO entity, boolean rtnPrimaryVal) { PlusCols plusCols = this.getPlusCols(entity.getClass()); PlusVals plusVals = this.getPlusVals(plusCols, entity); String sql = this.getInsertSql(plusCols); if (rtnPrimaryVal && plusCols.getStrategy().equals(Constant.IDENTITY)) { insertIdentity(plusCols, plusVals, entity, sql); } else { Object allVals[] = getInsertVals(plusCols, plusVals); plusParam.getJdbcTemplate().update(sql, allVals); } } private void insertIdentity(PlusCols plusCols, PlusVals plusVals, PlusPO entity, final String sql) { KeyHolder key = insertIdentityHandle(sql, plusCols, plusVals); Number val = 0; if (plusCols.getKeyType() == Constant.INT) { val = key.getKey().intValue(); } else { val = key.getKey().longValue(); } PlusUtils.setValue(entity, PlusUtils.getVarNameByCol(plusCols.getKey(), plusCols.getCol2Var()), val); } private KeyHolder insertIdentityHandle(final String sql, final PlusCols plusCols, final PlusVals plusVals) { Object allVals[] = getInsertVals(plusCols, plusVals); KeyHolder key = new GeneratedKeyHolder(); plusParam.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) { PlusCols plusCols = this.getPlusCols(entitys.get(0).getClass()); String sql = this.getInsertSql(plusCols); int counts[] = plusParam.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() { @Override public int getBatchSize() { return entitys.size(); } @Override public void setValues(PreparedStatement ps, int i) throws SQLException { PlusVals plusVals = getPlusVals(plusCols, entitys.get(i)); Object allVals[] = getInsertVals(plusCols, plusVals); for (int k = 0; k < allVals.length; k++) { ps.setObject(k + 1, allVals[k]); } } }); return counts.length; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy