在spring中使用JDBC事务配置
2016-09-05 15:35
323 查看
1:首先在applicationContext.xml文件中配置如下信息
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
2:在dao层的代码片断如下
@Resource
private JdbcTemplate jdbcTemplate;
public void addBatch(final List<TestT> ts){
final List<TestT> tts = ts;
try {
String sql = "insert into TestT(id,name,sex) values(?,?,?)";
this.jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
TestT t = tts.get(i);
ps.setString(1, t.getId());
ps.setString(2, t.getName());
ps.setString(3, t.getSex());
}
@Override
public int getBatchSize() {
return tts.size();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
2:在dao层的代码片断如下
@Resource
private JdbcTemplate jdbcTemplate;
public void addBatch(final List<TestT> ts){
final List<TestT> tts = ts;
try {
String sql = "insert into TestT(id,name,sex) values(?,?,?)";
this.jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
TestT t = tts.get(i);
ps.setString(1, t.getId());
ps.setString(2, t.getName());
ps.setString(3, t.getSex());
}
@Override
public int getBatchSize() {
return tts.size();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
相关文章推荐
- spring jdbc 事务配置----TransactionTemplate使用
- 使用注解方式配置spring中的jdbc事务
- Spring JDBC-使用注解配置声明式事务
- 【Spring】在Spring框架下使用注解配置JDBC事务
- Spring视频学习(十)使用XML配置事务
- Spring学习笔记(16)----使用Spring配置文件实现事务管理
- Spring JDBC和Hibernate混用时,如何配置事务管理
- spring JDBC事务使用经验
- 使用spring2.5.6整合jdbc的基本配置
- 使用spring管理事务的时候,配置文件的主意事项。
- 在Spring中采用声明式方法对Hibernate和JDBC进行统一的事务配置(AOP)
- 在Spring中采用声明式方法对Hibernate和JDBC进行统一的事务配置(AOP)
- Spring使用配置文件读取jdbc.properties
- Spring 下事务管理 - 使用 AOP XML 配置管理(iBatis 为例)
- spring 同时配置hibernate and jdbc 事务
- Spring使用TimerTask配置调度事务
- 在Red5中使用Spring-JDBC配置MySql连接池
- Spring 使用配置文件读取jdbc.properties
- Spring 使用配置文件读取jdbc.properties
- Spring使用配置文件读取jdbc.properties