jdbc原生api批量插入数据库

jdbc原生api批量插入数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

@Autowired
JdbcTemplate jdbcTemplate;

public int[] batchInsertOrUpdate( final List<InsertEntity> InsertEntiyList ){
StringBuilder stringBuilder = new StringBuilder();
//写sql
stringBuilder.append( " insert into XXX " )
.append( "( xx,xx,xx,xx...." )
.append( " values (" )
.append( " ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)" )
.append( " on duplicate key update " )
.append( " xx =?, x=?" );
String sql = stringBuilder.toString();
int[] updateCounts = jdbcTemplate.batchUpdate( sql, new BatchPreparedStatementSetter() {
@Override
public void setValues( PreparedStatement ps, int i ) throws SQLException {
InsertEntity insertEntity = InsertEntiyList.get( i );
ps.setString( 1, xxx);
ps.setTimestamp( 2, new java.sql.Timestamp( costDetail.getDate().getTime() ) );
//省略其他
}
@Override
public int getBatchSize() {
return InsertEntiyList.size();
}
} );
return updateCounts;
}
文章目录
  1. 1. jdbc原生api批量插入数据库
|