STRUT2 Spring Hibernate integration

Source: Internet
Author: User

First, create the Web project engineering Wzz

Click Finish

2. Add Spring jar Package aop,core,persistence Core, Web jar

Click Next

Click Finish

3. Configure Database Driver

I am using the Jtds jar package, Jtds when configuring the URL address is a bit different from using the SQL Seriver URL address, and then click Finish directly

4. Add Hibernate configuration

Click Next

Select Use Spring's Applicationcontext.xml and click Next

Select existing Spring configuration file, fill in the Sessionfactory ID, click Next

Click Next

Do not create sessionfactory, use Spring's sessionfactory to click Finish

Upon completion

[C-sharp]View Plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemalocation="Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans /spring-beans-2.5.xsd ">
  5. <bean id="DataSource"
  6. class="Org.apache.commons.dbcp.BasicDataSource" >
  7. <property name="Driverclassname"
  8. value="Net.sourceforge.jtds.jdbc.Driver" >
  9. </property>
  10. <property name="url"
  11. value="Jdbc:jtds:sqlserver://localhost:1433/wzdb" >
  12. </property>
  13. <property name="username" value="sa" ></property>
  14. <property name="password" value="123" ></property>
  15. </bean>
  16. <bean id="Sessionfactory"
  17. class="Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
  18. <property name="DataSource" >
  19. <ref bean="DataSource"/>
  20. </property>
  21. <property name="Hibernateproperties" >
  22. <props>
  23. <prop key="Hibernate.dialect" >
  24. Org.hibernate.dialect.SQLServerDialect
  25. </prop>
  26. </props>
  27. </property>
  28. </bean>
  29. </beans>

This is the file configuration that Application.xml automatically adds,

Now modify Web. XML,

<!--Spring Application Context--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>

<!--spring's listener to automatically load spring's configuration at startup--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--encoding Filter--
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

--------------------------------------------------------------------------------------------------------------- ---------------

Using the Database Explorer view to reflect the mapping of entity classes and entity classes to database tables using tables in databases

Select the project and entity class and map file storage path, click Next

Select Native in ID Generator, primary key generation is auto-growth, click Finish,spring profile applicationcontext.xml sessionfactory beans will change, add < Property Name= "Mappingresources" ><list><value>com/aweb/entity/DicAnimal.hbm.xml</value>
</list></property> configuration, add the entity class reflection file generated at the time of reflection to Sessionfactory's mappingresources.

Adding transactions in Applicationconext.xml

<!--configuration Transaction Management--
<bean id= "TransactionManager"
class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Sessionfactory"/>
</bean>

<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" >
<tx:attributes>
<!--propagation represents the propagation characteristics of a transaction, when using required, it is when the method at the beginning of the add is detected, that there is no open transaction at this time, if any, the method is put into the transaction, if not, a new transaction is created. Then put the method in. -
<tx:method name= "save*" propagation= "REQUIRED"/>
<tx:method name= "del*" propagation= "REQUIRED"/>
<tx:method name= "update*" propagation= "REQUIRED"/>
<tx:method name= "batch*" propagation= "REQUIRED"/>
<!--If another method is detected, give it a read-only database property. That is, when this method is read, the other methods cannot be written again. Guarantee the integrity of a transaction--
<tx:method name= "*" read-only= "true"/>
</tx:attributes>
</tx:advice>

Asm-2.2.3.jar is in conflict with Asm.jar and can be innovated to download ASM jar packages or remove Asm-2.2.3.jar

The integration of spring and Hibernate has been completed.

Configure Strut2

Add Struts2 jar Package Xwork-core-2.2.1.1.jar,struts2-core-2.2.1.1.jar,ognl-3.0.jar, Freemarker-2.3.16.jar

Modify the Web. XML configuration file to increase the STRUTS2 filter configuration

<!--struts2 Filters--
<filter>
<filter-name>struts2</filter-name>
<filter-class>
Org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

New Struts.xml file in src directory, struts2 spring hibernate configuration completed

--------------------------------------------------------------------------------------------------------------- ---------------------------------------

You can use spring's Hibernatedaosupport class to perform persistence operations:

Package com.aweb.util;

Import Org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class Basedao<t> extends Hibernatedaosupport {
public void Save (T t) {
Gethibernatetemplate (). Save (t);
}
}

STRUT2 Spring Hibernate integration

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.