Spring manages things based on hibernatetransactionmanager

Source: Internet
Author: User

Dao:

Package com. Test. Dao;

Import com. Test. model. customer;

Public interface userdao {
 

Public void add (customer C );
}

Daoimpl:

Package com. Test. daoimpl;

Import org. hibernate. hibernateexception;
Import org. hibernate. Classic. Session;
Import org. springframework. Orm. hibernate3.support. hibernatedaosupport;
Import com. Test. Dao. userdao;
Import com. Test. model. customer;

Public class userdaoimpl extends hibernatedaosupport implements userdao {
 
Public void add (customer C ){
Session session = NULL;
Try {
Gethibernatetemplate (). Save (C );
} Catch (hibernateexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}

Service:

Package com. Test. Service;

Import com. Test. Dao. userdao;

Public class addservice {
Private userdao;

Public userdao getuserdao (){
Return userdao;
}

Public void setuserdao (userdao ){
This. userdao = userdao;
}
}

Test:

Package com. Test. Service;

Import org. springframework. Context. Support. classpathxmlapplicationcontext;

Import com. Test. Dao. userdao;
Import com. Test. daoimpl. userdaoimpl;
Import com. Test. model. customer;

Public class addservicetest {
Public static void main (string ARGs []) {
Classpathxmlapplicationcontext CTX = new classpathxmlapplicationcontext ("applicationcontext. xml ");

Userdao impl = (userdaoimpl) CTX. getbean ("addservice ");

Customer c = new customer ();
C. setname ("comment ");
C. setpassword ("zhushihuan ");
Impl. Add (C );

}
}

Applicationcontext. xml:

 

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.