Merge and integration of Struts2 + Spring + Hibernate frameworks

Source: Internet
Author: User

This time, let's take a look at the integrated applications of the three frameworks Struts2 + Spring + Hibernate, mainly the integration of Spring and Hibernate, because the front has already integrated Strtus2 + Spring.


Let's take a look at the division of labor:

Struts2's MVC process framework mainly completes the process from client access to anction selection. The filter plays the role of Controller, action is model, and jsp is the display of view pages.


Spring mainly uses the features of Ioc to manage various objects: action, service, dao, data access source, core object SessionFactory of Hibernate, and declarative transaction management.


The Hibernate framework mainly refers to the ing between the entity layer and the tables in the database, and encapsulates the JDBC code to provide corresponding methods for us to choose. In this way, from the front-end page to the back-end database, there is a framework to help us maintain, so that our development becomes simple, fast, and efficient. Of course, the premise is that we can use these frameworks flexibly.

In addition, it is the jar packages that need to be imported from the three frameworks, which are not listed here. And the corresponding configuration file. here we need to explain that because the core object SessionFactory Of The Hibernate framework is handed over to the Spring framework for maintenance, hibernate is not needed here. cfg. xml configuration file. You can write this information to the Spring core configuration file.

Let's take a look at the compilation of various configuration files!


1. Key items of the web. xml file:

 
 
  
   
ContextConfigLocation
  
  
   
Classpath: applicationContext. xml
  
 
 
 
  
   
Org. springframework. web. context. ContextLoaderListener
  
 
 
 
  
   
OpenSessionInViewFilter
  
  
   
Org. springframework. orm. hibernate3.support. OpenSessionInViewFilter
  
 
 
  
   
OpenSessionInViewFilter
  
  
   
/*
  
 
 
 
  
   
Struts2
  
  
   
Org. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter
  
 
 
  
   
Struts2
  
  
   
/*
  
 


2. Compile the Struts2 core configuration file:


                     
  
   /success.jsp
              
  
   /login.jsp
              
 



3. Compile the Spring core configuration file:

 
  Aspectj-autoproxy>
  
  
  
  
   
      
  
  
   
    
     
      
Classpath: db. properties
     
    
   
  
  
  
   
   
   
   
  
  
  
   
   
        
     
     
      
True
     
     
     
      
True
     
     
     
      
Update
     
     
     
      
Org. hibernate. dialect. MySQLDialect
     
    
   
   
   
    
     
      
Classpath: com/lw./ egov/bean
     
    
   
  
  
  
   
  
  
  
   
    
    
    
    
   
  
 



4. In the preparation of the Dao layer, we will briefly explain how to obtain the session object, call related methods, and complete our data operation functions. The Dao layer first needs to implement our HibernateDaoSupport to use the getHibernateTemplate method in this parent class to call related methods. First, let's look at the implementation of a query method:


// The parameter must be final modified public User select (final User user) throws Exception {// The HibernateTemplate template class cannot directly support Query and Criteria interface operations, so the Session object can be passed through the callback function, to use these two interfaces. // When the execute method is called, The execute method calls back the override method of the parameter object (doInHibernate ). // The anonymous internal class is used as a callback function to use the Session object return (user%this.gethibernatetemplate(cmd.exe cute (new HibernateCallback () {// called the callback function, generally, the framework is responsible for calling and executing. Public Object doInHibernate (Session session) throws HibernateException, SQLException {// local variables that reference methods in the anonymous internal class, must be final modified. Query query = session. createQuery ("from User u where u. usercode =? And u. userpswd =? "); Query. setParameter (0, user. getUsercode (); query. setParameter (1, user. getUserpswd (); return query. uniqueResult ();}});}



Other configuration files are not provided here, which is similar to the single-user framework! Of course, we can also divide these configuration files according to the actual business. Each configuration file has a single function and the capacity is not too large, which is convenient for us to manage.

Finally, let's briefly explain the optimization considerations for the three frameworks!


Struts2:

1. Deploy the DevMode)

2. If no interceptor is used, the more components such as the interceptor, the lower the performance, and the principle of least use to achieve the goal, is required for relevant development.

3. Try not to use filter rules/*. Here we can develop rules to minimize the filter scope and facilitate quick filtering by the framework!


Spring:

1. Try not to use automatic assembly. Use manual assembly to specify the direction. The framework can quickly find related classes.

2. Try to use the delayed Loading Function to reduce communication with the database and improve performance.


Hibernate:

1. All frameworks use newer versions to provide better performance support. After all, everything is moving towards a better direction. But do not blindly pursue the latest, if there is a bug or the situation is still not good, rush to seek the latest principles on the basis of confidence.

2. A reasonable cache policy is mainly used for second-level cache and query cache, which can be reasonably used according to the actual situation.

3. Try to use the delayed loading function.

4. We recommend that you use the uuid primary key generation policy with good portability and support for large data volumes.

5. We recommend that you use optimistic locks instead of pessimistic locks.

All in all, make rational use of some of the functions of the framework and consider comprehensively to improve the performance of the framework.

Of course, this is just a simple framework, and there are many other functions, not added. We need to constantly improve the configuration file according to the actual situation, so that its functions become more and more powerful, so that our project development is fully integrated into the framework for development, so that we can be handy. All in all, we need to use more, observe more, summarize more, and reflect more ......


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.