jdbcTemplate 对数据库的一些操作
// 数据批量插入数据库
this.jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i)
throws SQLException {
ps.setString(1,sendFax.getSendDetailId()[i]);
ps.setLong(2, sendFax.getSendId());
}
public int getBatchSize() {
return faxId.length;
}
});
//查询返回对象
this.jdbcTemplate.queryForObject(sql,new Object[] { recvId }, new ReceiveFaxRowMapper());
sql += "where 1 = 1 ";
List<Object> params = new ArrayList<Object>();
if (Util.isNotEmpty(banner.getVersion()))
{
sql += " and a.version = ? ";
params.add(banner.getVersion());
}
return this.jdbcTemplate.queryForObject(sql, params.toArray(new Object[params.size()]), new ReceiveFaxRowMapper());
/**params.toArray(new Object[params.size()])====new Object[] { recvId }*****/
//查询的一些操作
this.jdbcTemplate.query(sql, new Object[] {uid, starttime},new ReceiveFaxRowMapper());
this.jdbcTemplate.queryForInt(sql,new Object[] { uid, starttime});
this.jdbcTemplate.queryForList(sql,new Object[] { uid, starttime});
this.jdbcTemplate.queryForMap(sql, new Object[] { thirdUid });
//修改并且返回主键
this.jdbcTemplate.update(sql,new Object[] { Integer.parseInt(resultCode),sendId });
this.jdbcTemplate.update(new PreparedStatementCreator()
{
@Override
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setInt(1, banner.getCityId());
ps.setInt(2, banner.getOsType());
return ps;
}
}, keyHolder);
return keyHolder.getKey().longValue();
//mapRow对象
class ReceiveFaxRowMapper implements RowMapper<ReceiveFax> {
public ReceiveFax mapRow(ResultSet rs, int rowNum) throws SQLException {
ReceiveFax receiveFax = new ReceiveFax();
receiveFax.setUid(rs.getLong("uid"));
receiveFax.setFaxId(rs.getString("recv_id"));
return receiveFax;
}
}