首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring入门范例-DAO,事务

2012-11-12 
spring入门实例-DAO,事务spring入门实例-DAO,事务?实例:配置文件:beans xmlnshttp://www.springframewo

spring入门实例-DAO,事务

spring入门实例-DAO,事务

?

实例:

配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean name="dataSource"name="code">public class CustomerDAO{/** * datasource */private DataSource dataSource;public void setDataSource(DataSource dataSource){this.dataSource = dataSource;}public void setTransactionManager(PlatformTransactionManager transactionManager){this.transactionManager = transactionManager;}/** * 事务管理类,初始化已将datasource作为参数 */private PlatformTransactionManager transactionManager;/** * 普通使用事务的方法  * 定义事务 * 记录事务点 * 提交事务点/回滚事务点 */public void insertCustomer(){DefaultTransactionDefinition def = new DefaultTransactionDefinition();TransactionStatus status = transactionManager.getTransaction(def);try{JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname1')");jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname2')");jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000025','testname3')");transactionManager.commit(status);}catch (Exception e){transactionManager.rollback(status);System.out.println("*********roll back");}}/** * 事务模板,无返回值 */public void insertCustomerTamplate(){TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);transactionTemplate.execute(new TransactionCallbackWithoutResult(){@Overrideprotected void doInTransactionWithoutResult(TransactionStatus arg0){// TODO Auto-generated method stubJdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname4')");jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000024','testname5')");jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000025,'testname6')");}});}/** * 事务模板,带有返回值 */public void queryCustomerTamplate(){TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);Object result = transactionTemplate.execute(new TransactionCallback<Object>(){@Overridepublic Object doInTransaction(TransactionStatus arg0){JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname4')");jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000024','testname5')");jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000025','testname6')");List<Map<String,Object>> rows = jdbcTemplate.queryForList("select eee from t_lpromis_yxgl_khxx");return rows;}});if(result instanceof List){List re = (List) result;for (int i = 0; i < re.size(); i++){Map<String,Object> one = (Map<String, Object>) re.get(i);System.out.println(one.get("id"));}}}public static void main(String[] args){ApplicationContext context = new ClassPathXmlApplicationContext("com/myspring/bussiness/transactionManager.xml");CustomerDAO cd = (CustomerDAO) context.getBean("userDAO");cd.queryCustomerTamplate();}}
?以上用JdbcTemplate实现的jdbc的一系列操作,以及设置事务,其中的sql语句故意错误,验证事务rollback效果

热点排行