江西广告网

标题: 配制Spring事务和JdbcTemplate使用 [打印本页]

作者: 一寒    时间: 2008-12-30 11:14
标题: 配制Spring事务和JdbcTemplate使用
配制一个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 ;

登录/注册后可看大图
}

登录/注册后可看大图
} );




欢迎光临 江西广告网 (http://bbs.jxadw.com/) Powered by Discuz! X3.2