配制Spring事务和JdbcTemplate使用

文章作者 100test 发表时间 2007:03:26 17:53:13
来源 100Test.Com百考试题网


配制一个applicationContext.xml如下
xml version = " 1.0 " encoding = " UTF-8 " ?>
DOCTYPE beans PUBLIC " -//SPRING//DTD BEAN//EN " " http://www.springframework.org/dtd/spring-beans.dtd " >

< beans default - autowire = " autodetect " >
< import resource = " classpath:conf/spring/demo.xml " />
< bean id = " DataSource " class = " org.apache.commons.dbcp.BasicDataSource " >
< property name = " driverClassName " >
< value > com.mysql.jdbc.Driver value >
property >
< property name = " url " >
< value > jdbc:mysql: // 192.168.1.10:3306/test?characterEncoding=UTF-8&.amp.characterSetResults=UTF-8
property >
< property name = " username " >
< value > root value >
property >
< property name = " password " >
< value > xx value >
property >
< property name = " maxActive " >
< value > 10 value >
property >
< property name = " maxIdle " >
< value > 2 value >
property >
bean >
< bean id = " TransactionManager "
class = " org.springframework.jdbc.datasource.DataSourceTransactionManager " >
< property name = " dataSource " >
< ref bean = " DataSource " />
property >
bean >
< bean id = " JdbcTemplate "
class = " org.springframework.jdbc.core.JdbcTemplate " >
< property name = " dataSource " >
< ref bean = " DataSource " />
property >
bean >
beans >
对应的TestDaoImpl中加入这部分代码
private JdbcTemplate jdbcTemplate.

public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate.
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this .jdbcTemplate = jdbcTemplate.
}

// 插入,修改和删除类似
String sql1 = " insert into testdb1 values(1,2) " .
jdbcTemplate.0update(sql1).
// 查询
private class BeanRowMapper implements RowMapper {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
String id
= rs.getString( " ID " ).
String title
= rs.getString( " TITLE " ).
Bean bean
= new Bean(id,title).
return bean.
}

}

String sql1
= " 0select * from testdb1 " .
List list
= jdbcTemplate.query(sql1, new BeanRowMapper()).
// call back (回调)
jt.execute( new ConnectionCallback() {
public Object doInConnection(java.sql.Connection con) throws SQLException, DataAccessException {
return null .
" align=top> }

}
).

相关文章


SCJP认证套题解析之九
SCJP认证套题解析之七
人件管理与程序员豫让的故事
SCJP认证套题解析之八
配制Spring事务和JdbcTemplate使用
SCJP认证考题解析之四
简单的字符转换和反转换函数
SCJP认证套题解析之五
在方法签名中使用控制反转(IoC)[1]
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛