Sping to hibernate for transaction management--annotation

Source: Internet
Author: User

1. Userservicetest.java:

Package Com.bjsxt.service;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import com.bjsxt.model.user;//dependency Injection//inverse of Controlpublic class Userservicetest {@Test public void Testadd () throws Exception {CLASSPATHXMLAPPL Icationcontext CTX = new Classpathxmlapplicationcontext ("Beans.xml"); UserService service = (userservice) ctx.getbean ("UserService"); System.out.println (Service.getclass ()); User User=new User (); User.setname ("Zhangsdddn"); Service.add (user); Ctx.destroy ();}}

2. SRC under Beas.xml: Note the introduction of TX, Properties->xml Catalog, introducing spring-tx-2.5.xsd

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring- Beans-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/cont Ext/spring-context-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SPRINGFRAMEWORK.ORG/SC Hema/aop/spring-aop-2.5.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/s Chema/tx/spring-tx-2.5.xsd "><context:annotation-config/><context:component-scan base-package=" Com.bjsxt "/>
<!--<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" >< Property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/><property name= "url" value= "jdbc:mysql:// Localhost:3306/spring "/><property name=" username "value=" root "/><property name=" password "value=" BJSXT "/></bean>--><beanclass=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "><property name=" Locations "><value>classpath:jdbc.properties</value></property>< /bean><bean id= "DataSource" destroy-method= "Close" class= "Org.apache.commons.dbcp.BasicDataSource" >< Property Name= "Driverclassname" value= "${jdbc.driverclassname}"/><property name= "url" value= "${jdbc.url}"/ ><property name= "username" value= "${jdbc.username}"/><property name= "password" value= "${jdbc.password} "/></bean><bean id=" sessionfactory "class=" org.springframework.orm.hibernate3.annotation.AnNotationsessionfactorybean "><property name=" DataSource "ref=" DataSource "/><property name=" Annotatedclasses "><list><value>com.bjsxt.model.User</value><value> Com.bjsxt.model.log</value></list></property><property name= "HibernateProperties" >< Props><prop key= "Hibernate.dialect" >org.hibernate.dialect.mysqldialect</prop><prop key= " Hibernate.show_sql ">true</prop></props></property></bean><bean id=" TxManager " class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" ><property name= "sessionfactory" ref = "Sessionfactory"/></bean><tx:annotation-driven transaction-manager= "TxManager"/></beans>

3. SRC under Jdbc.properties

Jdbc.driverclassname=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://localhost:3306/springjdbc.username= rootjdbc.password=linda0213

4. Userservice.java:

Add a business logic log, if you add a user error, then log also to ensure rollback, so through the Add method in Beans.xml and Userservice.java to join the @transactional, for unified transaction management

Transactional has a property propagation, which is an enum, the default value is required,

@Transactional (propagation=propagation. REQUIRED)

Package Com.bjsxt.service;import Javax.annotation.resource;import Org.springframework.stereotype.component;import Org.springframework.transaction.annotation.propagation;import Org.springframework.transaction.annotation.transactional;import Com.bjsxt.dao.logdao;import Com.bjsxt.dao.userdao;import Com.bjsxt.model.log;import Com.bjsxt.model.User, @Component ("UserService") public Class UserService {private Userdao userdao;private Logdao logdao;public void init () {System.out.println ("init");} Public User getUser (int id) {return null;}
@Transactionalpublic void Add (user user) {userdao.save (user); Log log = new log (); Log.setmsg ("A user saved!"); Logdao.save (log);} Public Userdao Getuserdao () {return userdao;} @Resource (name= "U") public void Setuserdao (Userdao userdao) {This.userdao = Userdao;} Public Logdao Getlogdao () {return logdao;} @Resourcepublic void Setlogdao (Logdao logdao) {This.logdao = Logdao;} public void Destroy () {System.out.println ("destroy");}}

Userdaoimpl.java:

Remove transaction processing from Save

Package Com.bjsxt.dao.impl;import Java.sql.sqlexception;import Javax.annotation.resource;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.springframework.stereotype.component;import Com.bjsxt.dao.userdao;import Com.bjsxt.model.User; @Component ("U") public class Userdaoimpl implements Userdao { Private Sessionfactory sessionfactory;public sessionfactory getsessionfactory () {return sessionfactory;} @Resourcepublic void Setsessionfactory (Sessionfactory sessionfactory) {this.sessionfactory = sessionfactory;} public void Save (user user) {Session s = sessionfactory.getcurrentsession (); s.save (user);//throw new RuntimeException (" Exeption! ");}}

  

Logdaoimpl.java:

Package Com.bjsxt.dao.impl;import Javax.annotation.resource;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.springframework.stereotype.component;import Com.bjsxt.dao.LogDAO;import Com.bjsxt.model.Log, @Component ("Logdao") public class Logdaoimpl implements Logdao {private sessionfactory Sessionfactory;public sessionfactory getsessionfactory () {return sessionfactory;} @Resourcepublic void Setsessionfactory (Sessionfactory sessionfactory) {this.sessionfactory = sessionfactory;} public void Save (log log) {Session s = sessionfactory.getcurrentsession (); S.save (log);//throw new RuntimeException (" Error! ");}}

Userdao.java:

Package Com.bjsxt.dao;import Com.bjsxt.model.user;public interface Userdao {public void Save (user user);

Logdao.java:

Package Com.bjsxt.dao;import Com.bjsxt.model.user;public interface Userdao {public void Save (user user);

User.java

Package Com.bjsxt.model;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import javax.persistence.Id; @Entitypublic class User {private int id;private String name; @Id @generatedvaluepublic int getId () { return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}

  

Log.java:

Package Com.bjsxt.model;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import javax.persistence.Id; @Entitypublic class User {private int id;private String name; @Id @generatedvaluepublic int getId () { return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}

  

  

  

  

  

Sping to hibernate for transaction management--annotation

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.