使用TransactionProxyFactoryBean

来源:互联网 发布:nginx rtmp 延时配置 编辑:程序博客网 时间:2024/04/30 15:38
 
使用TransactionProxyFactoryBean
2008年04月24日 星期四 14:32
----------------------------------------------------------------------------------------------------------
Spring声明式事务方法
直接采用JDBC操作数据库
使用TransactionProxyFactoryBean
-----------------------------------------------------------------------------------------------------------
   drop TABLE sampledb.orders;
CREATE TABLE sampledb.orders(
`ID` BIGINT NOT NULL,
`ORDER_NUMBER` VARCHAR(15),
`CUSTOMER_ID` BIGINT,
PRIMARY KEY(`ID`)
)
ENGINE = InnoDB;

drop TABLE `sampledb`.`customers`;
CREATE TABLE `sampledb`.`customers` (
   `ID` BIGINT NOT NULL,
`NAME` VARCHAR(45),
PRIMARY KEY(`ID`)
)
ENGINE = InnoDB;


alter table sampledb.orders
add constraint constraint_constraint_fk
foreign key (CUSTOMER_ID)
references sampledb.customers(ID);
  
-----------------------------------------------------------------------------------------------------------
applicationContext.xml
-----------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <!-- 定义DataSource -->
    <bean
        >
        <property value="com.mysql.jdbc.Driver" />
        <property
            value="jdbc:mysql://localhost:3306/SAMPLEDB" />
        <property value="root" />
        <property value="" />
    </bean>


    <bean
        >
        <property ref="dataSource" />
    </bean>

    <bean >
        <property >
            <ref bean="dataSource"/>
        </property>
    </bean>
   
    <bean >
        <property >
            <ref bean="transactionManager"/>
        </property>
        <property >
            <ref bean="userDAO"/>
        </property>
        <property >
            <props>
                <prop key="insert*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>       
    </bean>
</beans>

-----------------------------------------------------------------------------------------------------------
UserDAO.java
-----------------------------------------------------------------------------------------------------------
package db;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
public class UserDAO {
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        jdbcTemplate = new JdbcTemplate(dataSource);
    }
    public void insertUser(Customers c) {
        String sql = "insert into customers(id, name) values(";
        sql += c.getId();
        sql += ",'" + c.getName() + "'";
        sql += ")";
        System.out.println(sql);
        jdbcTemplate.update(sql);
    }
    public void queryAllBooks() {
        List<Customers> customers =jdbcTemplate.query(
                "select * from customers", new CustomerRowMapper());
        System.err.println("-- All Customers ---------------------------");
        for (Customers customer : customers) {
            System.err.println(" " + customer.getName() + ", by "
                    + customer.getName());
        }
        System.err.println("----------------------------------------");
    }
}
class CustomerRowMapper implements RowMapper {
    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        Customers book = new Customers();
        book.setId(Long.parseLong(rs.getString("id")));
        book.setName(rs.getString("name"));
        return book;
    }
}
-----------------------------------------------------------------------------------------------------------
Demo.java
-----------------------------------------------------------------------------------------------------------
package db;
import javax.sql.DataSource;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Demo {
    public static void main(String[] args) {
        XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(
                "applicationContext.xml"));
        DataSource dataSource = (DataSource) factory.getBean("dataSource");
        UserDAO dao = (UserDAO) factory.getBean("userDAO");
        Customers c = new Customers();
        c.setId(5l);
        c.setName("xiong");
        dao.insertUser(c);
        dao.queryAllBooks();
        System.out.println("END");
    }
}


-----------------------------------------------------------------------------------------------------------

本篇日志被作者设置为禁止发表新评论


©2008 Baidu



引文来源  使用TransactionProxyFactoryBean_熊熊之家
原创粉丝点击