时间:2021-07-01 10:21:17 帮助过:11人阅读
在Spring配置文件中配置DAO一般分为4个步骤:
基本数据操作
尽量使用可绑定参数的SQL,以便数据库可以复用SQL的执行计划,提高数据库的执行效率。
例如:
final String sqlString = "INSERT INTO t_forum(forum_name, forum_desc) VALUES (?, ?)"; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { PreparedStatement ps = conn.prepareStatement(sqlString); ps.setString(1, forum.getForumnName()); ps.setString(2, forum.getForumnDesc()); return ps; } }, keyHolder); forum.setForumnId(keyHolder.getKey().intValue());
private final String BATCH_INSERT_SQL = "INSERT INTO t_forum(forum_name, forum_desc) VALUES (?, ?)";
public void addForums(final List<Forum> forums) { jdbcTemplate.batchUpdate(BATCH_INSERT_SQL, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int index) throws SQLException { Forum forum = forums.get(index); ps.setString(1, forum.getForumName()); ps.setString(2, forum.getForumDesc()); } @Override public int getBatchSize() { return forums.size(); } }); }
Spring提供了RowCallbackHandler回调接口,通过该接口可以定义如何从结果集中获取数据。
单条结果集处理:
private final String SELECT_SQL = "SELECT forum_name, forum_desc FROM t_forum WHERE forum_id=?";
public Forum getForum(final int forumId) { final Forum forum = new Forum(); jdbcTemplate.query(SELECT_SQL, new Object[]{forumId}, new RowCallbackHandler() { @Override public void processRow(ResultSet rs) throws SQLException { forum.setForumId(forumId); forum.setForumDesc(rs.getString("forum_desc")); forum.setForumName(rs.getString("forum_name")); } }); return forum; }
多条数据结果集的处理:
private final String SELECT_MULTI_SQL = "SELECT forum_id,forum_name, forum_desc FROM t_forum WHERE forum_id between ? and ?";
public List<Forum> getForums(final int fromId, final int toId) { final List<Forum> forums = new ArrayList<>(); jdbcTemplate.query(SELECT_MULTI_SQL, new Object[]{fromId, toId}, new RowCallbackHandler() { @Override public void processRow(ResultSet rs) throws SQLException { Forum forum = new Forum(); forum.setForumId(rs.getInt("forum_id")); forum.setForumDesc(rs.getString("forum_desc")); forum.setForumName(rs.getString("forum_name")); forums.add(forum); } }); return forums; }
使用RowMapper<T>处理结果集,RowMapper<T>更适合在多行结果集中使用:
private final String SELECT_MULTI_SQL = "SELECT forum_id,forum_name, forum_desc FROM t_forum WHERE forum_id between ? and ?";
public List<Forum> getForumsByRowMapper(final int fromId, final int toId) { return jdbcTemplate.query(SELECT_MULTI_SQL, new Object[]{fromId, toId}, new RowMapper<Forum>() { @Override public Forum mapRow(ResultSet rs, int index) throws SQLException { Forum forum = new Forum(); forum.setForumId(rs.getInt("forum_id")); forum.setForumDesc(rs.getString("forum_desc")); forum.setForumName(rs.getString("forum_name")); return forum; } }); }
RowCallbackHandler vs RowMapper<T>
从功能上来说,RowCallbackHandler和RowMapper<T>并没有太大的区别,它们都是用于定义结果集行的读取逻辑,将ResultSet中的数据映射到对象或List中。
当处理大结果集时,如果使用RowMapper,结果集中的所有数据最终都会映射并汇总成一个List<T>对象,占用大量的JVM内存,甚至可直接引发OOM,这时,应该使用RowCallbackHandler接口,用processRow()方法内部处理结果集数据。
int类型的单值查询接口:int queryForInt(String sql)
private final String COUNT_SQL = "SELECT COUNT(*) FROM t_forum"; public int getForumNum() { return jdbcTemplate.queryForInt(COUNT_SQL); }
long类型的单值查询接口:long queryForLong(String sql)
其他类型的单值查询接口:<T> T queryForObject(String sql, Class<T> requiredType);
使用RowMapper获取单值对象:
public class TopicDao { @Autowired private JdbcTemplate jdbcTemplate; public double getReplyRate(int userId) { String sql = "SELECT topic_replies, topic_views FROM t_topic WHERE user_id=?"; double rate = jdbcTemplate.queryForObject(sql, new Object[]{userId}, new RowMapper<Double>() { @Override public Double mapRow(ResultSet rs, int index) throws SQLException { // TODO Auto-generated method stub int replies = rs.getInt("topic_replies"); int views = rs.getInt("topic_views"); if (views > 0) { return new Double((double)replies/views); } else { return new Double(0.0); } } }); return rate; } }
调用存储过程
Spring---- JDBC
标签:nts dao encoding require dstat instance array -- 如何