Example of a programmatic transaction in spring

Source: Internet
Author: User

Pure JDBC operation, perhaps better for some projects, the Spring JDBC framework lets you not care about connection, Statement, ResultSet. Defining data Sources example of Spring transaction programming<BeanID= "DataSource"class= "Org.springframework.jndi.JndiObjectFactoryBean">examples of spring transaction programming< Propertyname= "Jndiname">examples of spring transaction programming<value>Java:/comp/env/jdbc/oracle</value>examples of spring transaction programming</ Property>examples of spring transaction programming</Bean>defining the transaction manager<BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager">    < Propertyname= "DataSource"ref= "DataSource" /></Bean>example of defining daospring transaction programming<BeanID= "Customerdao"class= "Com.waterye.dao.impl.CustomerDAOImpl">examples of spring transaction programming< Propertyname= "DataSource"ref= "DataSource" />examples of spring transaction programming< Propertyname= "TransactionManager"ref= "TransactionManager" />examples of spring transaction programming</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 D         UAL ";                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 Jdbctempl                ate = 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 (intege        R ID) throws Exception {String deletesql = "Delete from customer where id =?";        Transactiontemplate transactiontemplate = new Transactiontemplate (TransactionManager); Transactiontemplate.execute (New Transactioncallbackwithoutresult () {protected void Dointransactionwithoutresu            Lt (transactionstatus status) {getjdbctemplate (). Update (Deletesql, new object[] {ID}); }}} public List findvalidcustomers () throws Exception {String querysql = "SELECT * from the customer where status = ' valid ' ORDER by code";     Return getjdbctemplate (). Query (Querysql, New Oraclecolumnmaprowmapper ());  }}

(RPM) Spring's programmatic transaction example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.