Integrate Spring with Hibernate and Mybatis

Source: Internet
Author: User

In Web projects, various web frameworks are generally used together, such as spring + hibernate, spring + ibatis, so that other frameworks are integrated into spring, A little inconvenience, of course, spring has made this integration very easy. I combined the spring and ibatis used in the project and the integration with mybatis, and then tested the integration with hibernate. I hope to criticize and correct it.

I. Integration of spring and hibernate

Spring not only supports Spring JDBC as the persistence technology, but also supports Hibernate, iBatis, JPA, JDO, and other Persistence Technologies. Spring provides convenient template classes to simplify the encapsulation of the original ORM. Let's first recall the process of using Hibernate separately. We need to write a ing file named like xxx. hbm. xml, and then use the Hibernate configuration file. cfg. xml. hbm. the xml ing file is assembled, and the SessionFactory instance is obtained through two lines of classic code, as shown in

SessionFactory sessionFactory = cfg. buildSessionFactory ();

Use Annotation

1. Use HibernateTemplate

The data source, object Association ing file, and Hibernate control attribute information are configured in the Hibernate. cfg. xml file. Therefore, after being integrated into spring, you need to take all the content in this file as follows:












Com. demo. hibernate. domain/xxx. hbm. xml




Org. hibernate. dialect. MySQLDialect
True











As shown in the preceding configuration, using hibernate Based on the template class is relatively simple. spring provides the template-based support class HibernateDaoSupport class, and calls the template class instance to the subclass through the getHibernateTemplate () method. If you do not want to add HibernateTemplate to each dao, you can put it in a BaseDao, and then create a Dao using HibernateTemplate by extending this class. getHibernateTemplate () is used directly (). op (obj. You can also directly inherit the HibernateDaoSupport class and implement the Dao interface. This method is also very simple. The rest is very different from using Hibernate alone.

For lob data, we also need to reset a Lob Data Processor in the Spring configuration file so that SessionFactory can process Lob data. Reference the bean in SessionFactory:

2. Use native Hibernate API

By injecting SessionFactory, we can directly get the Session bound to the current thread through getCurrentSession. Hibernate has the ability to obtain the Session object bound to the Transaction Thread, which is the same as the Session bound to the transaction in Spring HibernateTemplate. Therefore, using native APIS also works with spring and the Transaction Manager. Unlike template, the exception thrown by native APIS is a Hibernate exception.

3. Use annotations

Hibernate uses the addAnnotatedClass and addPackage methods of AnnotationConfiguration to load entity classes that use JPA annotations, obtain ing metadata information, and create SessionFactory instances on this basis. Spring provides a companion AnnotationSessionFactoryBean to create SessionFactory Based on JPA annotations. This class extends the LocalSessionFactoryBean class. The enhanced function is to obtain the ORM configuration information based on the annotation of the entity class. You can also configure object relationship ing using XML configuration and annotations. Hibernate automatically integrates the metadata information without conflict.

In addition, Spring specifically provides an OpenSessionInViewFilter filter to prevent the Web layer from accessing objects that are loaded late at the Service layer. Its main function is to bind each request process to a Hibernate Session, even if the initial transaction has been completed, you can perform the delayed loading operation on the Web layer. The OpenSessionInViewFilter binds HibernateSession to the request thread and is automatically detected by Spring's Transaction Manager. Therefore, it uses an environment for managing transactions using HibernateTransactionManager or JtaTransactionManager, it can also be used in data operations of non-read-only transactions.

2. Integrate ibatis/mybatis with spring

Later versions of ibatis were renamed mybatis, which is frequently used. The biggest difference between the two is that mybatis provides interface binding. Instead of implementing the Dao interface, you can directly call it at the Service layer and focus on the ing file. The core class of ibatis is SqlMapClient. XmlSqlMapClientFactoryBean constructs a SqlMapClient by reading sqlMapConfig (including many sqlmaps of course) in the file, and uses sqlMap to execute each ing SQL statement; the core class of mybatis is SqlSessionFactory. SqlSessionFactoryBuilder reads the configuration file (or annotation) and creates a SqlSessionFactory. It uses SqlSession to execute ing SQL statements. The following configurations are integrated with spring:

 










Configure sqlMapClient in the spring-prop.xml file, as shown in






Like the location of the configuration specified by property, the configuration is required and each xml file is introduced into the ibatis-config.xml as follows:

LazyLoadingEnabled



......

2. In Mybatis, this configuration is not required:
Specify sqlSessionFactory in spring-prop, for example,













Various er files are specified by mapperLocations. Mybatis is relatively simple.

Similar to Hibernate above, ibatis can directly inject sqlMapClientTemplate, or inherit the SqlMapClientDaoSupport class to implement the Dao interface at the same time, through

The integration of Mybatis and spring varies with different data processing methods. The above method adopts the MapperScannerConfigurer processing method and uses the annotation form. You can also use

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.