注册
 找回密码
 注册
江西广告网
查看: 324|回复: 0
打印 上一主题 下一主题

配制Spring事务和JdbcTemplate使用

[复制链接]

该用户从未签到

1
跳转到指定楼层
发表于 2008-12-30 11:14:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?注册

x
配制一个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</value> </ 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.update(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 = " select * 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 ; } } );
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表