spring JDBCTemplete
来源:互联网 发布:在线订餐系统源码asp 编辑:程序博客网 时间:2024/06/05 04:07
http://static.springsource.org/spring/docs/2.5.x/reference/jdbc.html#jdbc-JdbcTemplate
Querying (SELECT)
A simple query for getting the number of rows in a relation.
int rowCount = this.jdbcTemplate.queryForInt("select count(0) from t_accrual");
A simple query using a bind variable.
int countOfActorsNamedJoe = this.jdbcTemplate.queryForInt( "select count(0) from t_actors where first_name = ?", new Object[]{"Joe"});
Querying for a String.
String surname = (String) this.jdbcTemplate.queryForObject( "select surname from t_actor where id = ?", new Object[]{new Long(1212)}, String.class);
Querying and populating a single domain object.
Actor actor = (Actor) this.jdbcTemplate.queryForObject( "select first_name, surname from t_actor where id = ?", new Object[]{new Long(1212)}, new RowMapper() { public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Actor actor = new Actor(); actor.setFirstName(rs.getString("first_name")); actor.setSurname(rs.getString("surname")); return actor; } });
Querying and populating a number of domain objects.
Collection actors = this.jdbcTemplate.query( "select first_name, surname from t_actor", new RowMapper() { public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Actor actor = new Actor(); actor.setFirstName(rs.getString("first_name")); actor.setSurname(rs.getString("surname")); return actor; } });
If the last two snippets of code actually existed in the same application, it would make sense to remove the duplication present in the two RowMapper anonymous inner classes, and extract them out into a single class (typically a static inner class) that can then be referenced by DAO methods as needed. For example, the last code snippet might be better off written like so:
public Collection findAllActors() { return this.jdbcTemplate.query( "select first_name, surname from t_actor", new ActorMapper());}private static final class ActorMapper implements RowMapper { public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Actor actor = new Actor(); actor.setFirstName(rs.getString("first_name")); actor.setSurname(rs.getString("surname")); return actor; }}
Updating (INSERT/UPDATE/DELETE)
this.jdbcTemplate.update( "insert into t_actor (first_name, surname) values (?, ?)", new Object[] {"Leonor", "Watling"});
this.jdbcTemplate.update( "update t_actor set weapon = ? where id = ?", new Object[] {"Banjo", new Long(5276)});
this.jdbcTemplate.update( "delete from actor where id = ?", new Object[] {new Long.valueOf(actorId)});
- spring JDBCTemplete
- Spring jdbcTemplete datasource
- SpringMvc + spring jdbctemplete 事务
- Spring jdbcTemplete查询
- spring中jdbctemplete的使用
- Hibernate的HQL中in参数设置和JdbcTemplete中的in参数
- Spring
- Spring
- spring
- spring
- spring
- spring
- Spring
- spring
- spring
- Spring
- Spring
- spring
- Protel DXP布线规则设置
- Eclipse中java文件和jsp字体大小设置
- Android系统开发编译环境配置 (感觉好经典的系列)(其四)
- ural 1864. Get-Together at Den's (坑爹浮点数)
- [ oracle ] oracle之路4---存储过程!
- spring JDBCTemplete
- Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
- android屏蔽返回键和home键等各个按键的方法
- Spring 图片上传实例
- C++
- Eclipse添加SVN、Maven 插件
- Eclipse Maven m2eclipse常见问题
- 一些常见svn图标的含义
- Maven常用命令