Java Framework Spring Learning Note (18): Transactional operations

Source: Internet
Author: User

Transaction Operations Create service and DAO classes to complete the injection relationship

    • The service layer is called the business logic layer
    • The DAO layer is purely on the database operations layer and does not add business to the DAO layer

Suppose now there is a demand for money transfer, the dog eggs have 10000 yuan, the founding of 20000 yuan, the dog eggs to the founding of 1000 yuan.

Write the service layer to create the business logic, Orderservice.java

1 ImportCn.dao.OrderDao;2 3  Public classOrderService {4     PrivateOrderdao Orderdao;5 6      Public voidSetorderdao (Orderdao Orderdao) {7          This. Orderdao =Orderdao;8     }9 Ten     //methods to invoke DAO One     //business logic layer, write transfer business A      Public voidAccountmoney () { -         //Dog Egg Transfer to the founding, on the book is the dog eggs to reduce money, the founding of more money -         //The dog and the egg cut the money the Orderdao.lessmoney (); -         //Founding more money - Orderdao.moremoney (); -     } +}

Writing the DAO layer for database operations, Orderdao.java

1  PackageCn.dao;2 3 Importorg.springframework.jdbc.core.JdbcTemplate;4 5  Public classOrderdao {6     //inject JdbcTemplate7     Privatejdbctemplate JdbcTemplate;8      Public voidsetjdbctemplate (JdbcTemplate jdbctemplate) {9          This. JdbcTemplate =JdbcTemplate;Ten     } One  A     /** - * operation on database, do not do business operation -      */ the     //How to lose money by dog eggs -      Public voidLessmoney () { -String sql = "Update account set salary=salary-?" where Username=? "; -Jdbctemplate.update (sql,1000, "Dog egg"); +     } -     //How to add money to the Nation +      Public voidMoremoney () { AString sql = "Update account set salary=salary+?" where Username=? "; atJdbctemplate.update (sql,1000, "Jianguo"); -     } -}

Writing a configuration file Bean.xml

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:tx= "Http://www.springframework.org/schema/tx"4 XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"5 Xmlns:context= "Http://www.springframework.org/schema/context"6 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"7 xsi:schemalocation="8 Http://www.springframework.org/schema/beans9 http://www.springframework.org/schema/beans/spring-beans.xsdTen Http://www.springframework.org/schema/context One http://www.springframework.org/schema/context/spring-context.xsd A Http://www.springframework.org/schema/tx - http://www.springframework.org/schema/tx/spring-tx.xsd - HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP the http://www.springframework.org/schema/aop/spring-aop.xsd "> -  -     <!--Configuring the C3P0 connection pool - -     <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"> +         <!--Inject DAO object - -         < Propertyname= "Driverclass"value= "Com.mysql.jdbc.Driver"></ Property> +         < Propertyname= "Jdbcurl"value= "Jdbc:mysql:///test"></ Property> A         < Propertyname= "User"value= "root"></ Property> at         < Propertyname= "Password"value= "jqbjqbjqb123"></ Property> -     </Bean> -  -     <BeanID= "OrderService"class= "Cn.service.OrderService"> -         < Propertyname= "Orderdao"ref= "Orderdao"></ Property> -     </Bean> in     <BeanID= "Orderdao"class= "Cn.dao.OrderDao"> -         <!--inject JdbcTemplate Object - to         < Propertyname= "JdbcTemplate"ref= "JdbcTemplate"></ Property> +     </Bean> -  the     <!--Create a JdbcTemplate object - *     <BeanID= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate"> $         <!--Pass the DataSource to the template object -Panax Notoginseng         < Propertyname= "DataSource"ref= "DataSource"></ Property> -     </Bean> the  + </Beans>

Writing Test Files Testservice.java

1  Packagecn.test;2 ImportCn.service.OrderService;3 Importorg.junit.Test;4 ImportOrg.springframework.context.ApplicationContext;5 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;6 7  Public classTestservice {8 @Test9      Public voidTestdemo () {TenApplicationContext context =NewClasspathxmlapplicationcontext ("Bean.xml"); OneOrderService OrderService = (orderservice) context.getbean ("OrderService"); A Orderservice.accountmoney (); -     } -}

File structure

After running

Database content changes, complete transfer

Java Framework Spring Learning Note (18): Transactional operations

Related Article

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.