Integrate Hibernate3 with Spring

Source: Internet
Author: User

Integrate Hibernate3 with Spring

Integrate Hibernate3 with Spring

Like integrating Jdbc, Spring integrated Hibernate3 provides templates so that we only need to care about data table operations. when resources are closed and opened, and exceptions are handled by Spring, this template is HibernateTemplate. HibernateTemplate (DAO Support class) is obtained through SessionFactory, so let's take a look at how to configure SessionFactory in XML.

// Data Source

"DataSource"Class = "Org. springframework. jdbc. datasource. DriverManagerDataSource">

"DriverClassName"Value = "Com. mysql. jdbc. Driver">

"Url"Value = & Quot; jdbc: mysql: // localhost: 3306/dataBase & quot">

"Username"Value = "Root">

"Password"Value = "">

// SessionFactory Bean

Beanid ="SessionFactory"Class ="Org. springframework. orm. hibernate3.LocalSessionFactoryBean">

// Data Source

"DataSource">

"DataSource"/>

// Other attributes, such as dialect and whether to print SQL ....

"HibernateProperties">

"Hibernate. dialect">

Org. hibernate. dialect. MySQLDialect

// Ing File

"MappingResources">

Hibernate/Users. hbm. xml

In an application, you can use SessionFactory sf = getBean ("sessionFactory", SessionFactory. class );

HibernateTemplate ht = new HibernateTemplate (sf );

Then operate the table through HibernateTemplate.

ApplicationContextac =NewClassPathXmlApplicationContext ("applicationContext. xml ");

SessionFactorysessionFactory = ac. getBean ("sessionFactory", SessionFactory.Class);

HibernateTemplate ht =NewHibernateTemplate (sessionFactory );

Users user =NewUsers ();

User. setUname ("zhangsan ");

User. setAge (1 );

Ht. save (user );

We can see that this operation omits the opening and closing of resources and handles exceptions.

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.