| |
| 配制Spring事务和JdbcTemplate使用 |
| |
发布者: 发布时间:2007-02-25 |
|
|
配制一个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&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 ;
}
} );
|
| (转载文章请保留出处:北天JAVA技术网(www.java114.com)) |
| |
| 更多精彩文章: |
| struts标签使用举例--logic篇 |
| 使用xmlhttp和Java session监听改善站内消息系统 |
| 开发狂想曲:如何在开源Java下生存 |
| 毫无意义的Java基础类恶作剧 |
| 利用JavaBeans在应用程序中创建组件 |
| 新手入门:JAVA数据库基本操作指南 |
| |
| 最近评论: |
|
|
| 你曾悄悄的来过! |
| wow gold,wow gold,wow gold,ffxi gil max(462) |
|
|
| 冰封的往事! |
| wow power leveling,wow gold,wow power leveling,wow gold
max(9425) |
|
|
| 冰封的往事! |
| wow power leveling,wow gold,WoW Gold,wow gold
max(2806) |
|
|
| 飞舞的传奇! |
| 传世私服,传世私服.传奇世界私服传奇世界私服,传世私服传世私服, 传奇世界私服传奇世界私服.传奇私服传奇私服. max(7744) |
|
|
| |
| 免责声明:该文章由网友发表,如果对您造成侵权,请联系站长。 |
|