使用Spring JDBC Framework簡(jiǎn)化開(kāi)發(fā)
純JDBC操作, 對(duì)某些項(xiàng)目來(lái)說(shuō), 也許更好, Spring JDBC Framework讓你不用關(guān)心Connection, Statement, ResultSet.定義數(shù)據(jù)源





定義事務(wù)管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<property name="dataSource" ref="dataSource" />
</bean>
定義dao




demo
public class CustomerDaoImpl extends JdbcDaoSupport implements CustomerDAO {
private DataSource dataSource;
pirvate TransactionManager transactionManager;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setTransactionManager(DataSourceTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public Map get(Integer id) throws Exception {
String querySql = "select * from customer where id = ?";
return getJdbcTemplate().queryForMap(querySql, new Object[] { id });
}
public void insert(final Map customer) throws Exception {
String seqSql = "select customer_seql.nextval from dual";
String insertSql = "insert into customer (id, code, name, status) values (?, ?, ?, ?)";
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
JdbcTemplate jdbcTemplate = getJdbcTemplate();
int id = jdbcTemplate.queryForInt(seqSql);
Object[] params = new Object[] { new Integer(id), customer.get("code"), customer.get("name"), map.get("status") };
jdbcTemplate.update(insertSql, params);
}
}
}
public void update(final Map customer) throws Exception {
//
}
public void delete(Integer id) throws Exception {
String deleteSql = "delete from customer where id = ?";
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
getJdbcTemplate().update(deleteSql, new Object[] { id });
}
}
}
public List findValidCustomers() throws Exception {
String querySql = "select * from customer where status = 'valid' order by code";
return getJdbcTemplate().query(querySql, new OracleColumnMapRowMapper());
}
}
private DataSource dataSource;
pirvate TransactionManager transactionManager;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setTransactionManager(DataSourceTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public Map get(Integer id) throws Exception {
String querySql = "select * from customer where id = ?";
return getJdbcTemplate().queryForMap(querySql, new Object[] { id });
}
public void insert(final Map customer) throws Exception {
String seqSql = "select customer_seql.nextval from dual";
String insertSql = "insert into customer (id, code, name, status) values (?, ?, ?, ?)";
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
JdbcTemplate jdbcTemplate = getJdbcTemplate();
int id = jdbcTemplate.queryForInt(seqSql);
Object[] params = new Object[] { new Integer(id), customer.get("code"), customer.get("name"), map.get("status") };
jdbcTemplate.update(insertSql, params);
}
}
}
public void update(final Map customer) throws Exception {
//
}
public void delete(Integer id) throws Exception {
String deleteSql = "delete from customer where id = ?";
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
getJdbcTemplate().update(deleteSql, new Object[] { id });
}
}
}
public List findValidCustomers() throws Exception {
String querySql = "select * from customer where status = 'valid' order by code";
return getJdbcTemplate().query(querySql, new OracleColumnMapRowMapper());
}
}
說(shuō)明:
1. 沒(méi)有使用聲明性事務(wù), 使用編程式事務(wù)
2. 沒(méi)有使用POJO模式,使用HashMap, (ActiveMapper還在sandbax狀態(tài))
3. OracleColumnMapRowMapper implements RowMapper, 實(shí)現(xiàn)oracle風(fēng)格到j(luò)ava bean風(fēng)格mapping
如: 字段customer_id 對(duì)應(yīng)rowMap.get("customerId");
posted on 2005-08-21 20:13 waterye 閱讀(2446) 評(píng)論(3) 編輯 收藏 所屬分類(lèi): spring