• (转)Spring的编程式事务例子


    
    
    纯JDBC操作, 对某些项目来说, 也许更好, Spring JDBC Framework让你不用关心Connection, Statement, ResultSet.
    
    定义数据源
    spring事务编程的例子<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    spring事务编程的例子    <property name="jndiName">
    spring事务编程的例子        <value>java:/comp/env/jdbc/oracle</value>
    spring事务编程的例子    </property>
    spring事务编程的例子</bean>
    
    定义事务管理器
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    定义dao
    spring事务编程的例子<bean id="customerDAO" class="com.waterye.dao.impl.CustomerDAOImpl">
    spring事务编程的例子    <property name="dataSource" ref="dataSource" />
    spring事务编程的例子    <property name="transactionManager" ref="transactionManager" />
    spring事务编程的例子</bean>
    
    
    public class CustomerDaoImpl extends JdbcDaoSupport implements CustomerDAO {
        private DataSource dataSource; 
        pirvate TransactionManager transactionManager;
    
        public void setDataSource(DataSource dataSource) {
            this.dataSource = dataSource;
        }
    
        public void setTransactionManager(DataSourceTransactionManager transactionManager) {
            this.transactionManager = transactionManager;
        } 
    
        public Map get(Integer id) throws Exception {
            String querySql = "select * from customer where id = ?";
    
            return getJdbcTemplate().queryForMap(querySql, new Object[] { id }); 
        }
    
        public void insert(final Map customer) throws Exception {
            String seqSql = "select customer_seql.nextval from dual"; 
            String insertSql = "insert into customer (id, code, name, status) values (?, ?, ?, ?)";
            
            TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
            transactionTemplate.execute(new TransactionCallbackWithoutResult() { 
                protected void doInTransactionWithoutResult(TransactionStatus status) { 
                    JdbcTemplate jdbcTemplate = getJdbcTemplate();
                    int id = jdbcTemplate.queryForInt(seqSql);
                    Object[] params = new Object[] { new Integer(id), customer.get("code"), customer.get("name"), map.get("status") };
                    jdbcTemplate.update(insertSql, params);
                }
            }
        }
    
        public void update(final Map customer) throws Exception {
            //  
        }
    
        public void delete(Integer id) throws Exception {
            String deleteSql = "delete from customer where id = ?";
    
            TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
            transactionTemplate.execute(new TransactionCallbackWithoutResult() { 
                protected void doInTransactionWithoutResult(TransactionStatus status) { 
                    getJdbcTemplate().update(deleteSql, new Object[] { id });
                }
            }
        }
    
        public List findValidCustomers() throws Exception {
            String querySql = "select * from customer where status = 'valid' order by code";
    
            return getJdbcTemplate().query(querySql, new OracleColumnMapRowMapper()); 
        }
    } 
  • 相关阅读:
    linux-gcc 编译时头文件和库文件搜索路径
    程序自启动位置(8种方法,注册表有6处)
    谷歌、flick网站图片 一次性下载 javaWeb项目 多线程下载,
    部署vc2008开发的程序(vcredist_x86是其中一个办法)
    vs2012-vs2013编译出来的程序不能在xp上运行解决方法
    openssl编译
    libcurl编译
    qt 国际化(翻译时会触发changeEvent)
    uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
    qtcreator增加doxygen注释
  • 原文地址:https://www.cnblogs.com/zuiyirenjian/p/4005965.html
Copyright © 2020-2023  润新知