jdbcTemplate批量插入数据

阅读更多
jdbcTemplate批量插入数据
public void buyTool(List userToolList) throws Exception {
final List intsertlist=new ArrayList();
if(!CollectionUtils.isEmpty(userToolList)){
intsertlist.addAll(userToolList);
}
final Calendar invalidateTimeCalendar = Calendar.getInstance();
invalidateTimeCalendar.add(Calendar.DAY_OF_MONTH, 30);
invalidateTimeCalendar.set(Calendar.HOUR_OF_DAY, 23);
invalidateTimeCalendar.set(Calendar.MINUTE, 59);
invalidateTimeCalendar.set(Calendar.SECOND, 59);
final Timestamp createTime = Timestamp.valueOf(
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
final Timestamp invalidateTime = Timestamp.valueOf(
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").
format(invalidateTimeCalendar.getTime()));
if(!CollectionUtils.isEmpty(intsertlist)){
String insertSql="insert into yyff_user_tool("
+"user_id,tool_id,tool_pool_id,use_flag,own_type,fee,create_time,invalidate_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
jdbcTemplate.batchUpdate(insertSql, new BatchPreparedStatementSetter() {
public int getBatchSize() {
        return intsertlist.size();
        }
        public void setValues(PreparedStatement ps, int i)
throws SQLException {
            UserToolNew userTool=(UserToolNew)intsertlist.get(i);
            ps.setLong(1,userTool.getUserId());
            ps.setInt(2, userTool.getToolId());
            ps.setInt(3, userTool.getToolPoolId());
            ps.setString(4, "1");
            ps.setString(5, "1");
            ps.setInt(6, userTool.getFee());
            ps.setTimestamp(7,  createTime);
            ps.setTimestamp(8,  invalidateTime);
        }
});
}
}

你可能感兴趣的:(db,数据库,jdbcTemplate,批量插入)