利用Spring之NamedParameterJdbcTemplate批量插入和更新Oracle+Sequence

package com.resources.novelcoronavirus.service.impl.opt.dao;

import com.resources.novelcoronavirus.domain.XiYaoZhongChengYaoPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.List;

/**
 * @author yang
 * @date 2018/10/6 22:11
 * @description
 */
@Slf4j
@Service
public class XyzcyDaoImpl implements XyzcyDao {

private static final String BATCH_INSERT_SQL = 
		"INSERT INTO 
			APPGM.YB_XYZCY (
			ID,
			YLMLBM,
			YPSPM,
			TYMBH
		) VALUES (
			APPGM.SEQ_YB_XYZCY.NEXTVAL,
			:YLMLBM,
			:YPSPM, 
			:TYMBH)";

@Resource
private DataSource dataSource;

@Override
@Transactional(rollbackFor = Exception.class)
public void batchInsert(List<XiYaoZhongChengYaoPO> list) {
	NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(new JdbcTemplate(this.dataSource));
	try {
		long start = System.currentTimeMillis();
		SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(list.toArray());
		namedParameterJdbcTemplate.batchUpdate(BATCH_INSERT_SQL, batch);
		long end = System.currentTimeMillis();
		log.error("--------------> {}", end - start);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

private static final String BATCH_UPDATE_SQL = 
			"UPDATE
				APPGM.YB_XYZCY
            SET
				YLMLBM = :YLMLBM,
				YPSPM = :YPSPM,
				TYMBH = :TYMBH,
				YPTYM = :YPTYM,
				HXMC = :HXMC,
				BM = :BM
			WHERE
				YLMLBM = :YLMLBM";

@Override
@Transactional(rollbackFor = Exception.class)
public void batchUpdate(List<XiYaoZhongChengYaoPO> list) {
	NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(new JdbcTemplate(this.dataSource));
	try {
		long start = System.currentTimeMillis();
		SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(list.toArray());
		namedParameterJdbcTemplate.batchUpdate(BATCH_UPDATE_SQL, batch);
		long end = System.currentTimeMillis();
		log.error("==========> {}", end - start);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
}
上一篇:数列 - Sequence - Indexed Tree


下一篇:from keras.preprocessing import sequence 报错解决【2021/10/3】