Struts + spring + hibernate integration (4)

Source: Internet
Author: User

Use transactions for management

JavaBean

@Entity@GenericGenerator(name="uid",strategy="uuid")public class Person {@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}@Columnpublic String getName() {return name;}public void setName(String name) {this.name = name;}private int id;private String name;}

Business Logic

public class Base {private HibernateTemplate hibernateTemplate;@Resource(name="hibernateTemplate")public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {this.hibernateTemplate = hibernateTemplate;}public HibernateTemplate getHibernateTemplate() {return hibernateTemplate;}}@Component("personDao")public class PersonDaoImpl extends Base implements PersonDao {@SuppressWarnings("unchecked")public Person checkUser(final int id ,final String username){final String sql = "from Person p where p.id =? and p.name =?"; return this.getHibernateTemplate().execute(new HibernateCallback<Person>(){public Person doInHibernate(Session arg0)throws HibernateException, SQLException {   Query query = arg0.createQuery(sql).setInteger(0,id).setString(1,username);      return  (Person) query.list().get(0);}  }); }}

Service Layer

@Component("personService")public class PersonServiceImpl implements PersonService {private PersonDao personDao;public Person check(int id, String username) {return getPersonDao().checkUser(id,username);}@Resource(name="personDao")public void setPersonDao(PersonDao personDao) {this.personDao = personDao;}public PersonDao getPersonDao() {return personDao;} }

Control Layer action

@Component("LoginAction")@Scope(value="prototype")public class LoginAction extends ActionSupport implements ModelDriven<Person>{private PersonService personService;private Person person;@Overridepublic String execute() throws Exception {     this.getPersonService().check(person.getId(),person.getName());return SUCCESS;}@Resource(name="personService")public void setPersonService(PersonService personService) {this.personService = personService;}public PersonService getPersonService() {return personService;}public Person getModel() {if(person==null)      person= new Person();return person;}}

Applicationcontext. xml

<Context: component-scan base-package = "com. zzia"/> <! -- Configure the data source --> <bean id = "datasource" class = "com. mchange. v2.c3p0. combopooleddatasource "> <property name =" user "value =" root "> </property> <property name =" password "value =" 123 "> </property> <property name = "driverclass" value = "com. mySQL. JDBC. driver "> </property> <property name =" jdbcurl "value =" JDBC: mysql: // localhost: 3306/test "> </property> </bean> <! -- Configure sessionfactory --> <bean id = "sessionfactory" class = "org. springframework. orm. hibernate3.annotation. annotationsessionfactorybean "> <property name =" datasource "ref =" datasource "> </property> <! -- <Property name = "annotatedclasses"> <list> <value> COM. zzia. po. person </value> </List> </property> achieves the same effect as packagetoscan --> <property name = "packagestoscan"> <list> <value> COM/zzia/PO </ value> </List> </property> <property name = "hibernateproperties"> <props> <prop key = "hibernate. dialect "> Org. hibernate. dialect. mysqldialect </prop> <prop key = "current_session_context_class"> thread </prop> <prop key = "hibernate. show_ SQL "> true </Prop> <prop key = "hibernate. format_ SQL "> true </prop> <prop key =" hibernate. hbm2ddl. auto "> Update </prop> </props> </property> </bean> <! -- Configure hibernatetemplate --> <bean id = "hibernatetemplate" class = "org. springframework. orm. hibernate3.hibernatetemplate "> <property name =" sessionfactory "ref =" sessionfactory "> </property> </bean> <! -- Manage transactions --> <bean id = "transactionmanager" class = "org. springframework. orm. hibernate3.hibernatetransactionmanager "> <property name =" sessionfactory "ref =" sessionfactory "> </property> </bean> <! -- Configure a notification for a transaction --> <TX: Advice id = "txadvice" transaction-Manager = "transactionmanager"> <TX: Attributes> <! -- The method starts with get and does not use transactions --> <TX: method name = "get *" Read-Only = "true" propagation = "not_supported"/> <! -- Other methods are performed by default transactions --> <TX: method name = "*" Read-Only = "false" propagation = "required"/> </TX: attributes> </TX: Advice> <AOP: config> <! -- Cut --> <AOP: pointcut expression = "execution (* COM. zzia. daoimpl. *. * (..)" id = "cut"/> <! -- Cut --> <AOP: Advisor advice-ref = "txadvice" id = "Advisor" pointcut-ref = "cut"/> </AOP: config>

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.