Struts+spring+hibernate sentiment

Source: Internet
Author: User
Struts+spring+hibernate The combination of three is perfect
The key to consolidating the three is the configuration file
1.web.xml for Web server mount filters, servlet, and configuration files
Struts This is loaded Org.apache.struts.action.ActionServlet, and its configuration parameter config file struts-config.xml,spring is loaded here Org.springframework.web.context . Contextloaderservlet also has its configuration file applicationcontext.xml, other omitted not listed
Example:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param> <servlet-name>action</servlet-name>
<servlet-class>
Org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
Org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<!--Action Servlet Mapping-->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2.struts-config.xml this file.
This is the Strust proprietary configuration file, configuration form, global forwarding, action,message-resources (for display hints), plug-ins
The format is as follows:
<struts-config>
<form-beans>
<form-bean name= "LogonForm" type= "Com.binghe.forms.LogonForm"/>
.....
</form-beans>
<global-forwards>
<forward name= "logon" path= "/login.jsp"/>
.......
</global-forwards>
<action-mappings>
<action path= "/logon"
Type= "Org.springframework.web.struts.DelegatingActionProxy" name= "LogonForm"
input= "/login.jsp" scope= "Request" validate= "true" >
<forward name= "logon" path= "/login.jsp"/>
.....
</action>
......
</action-mappings>
<controller locale= "true"/>
<message-resources parameter= "Com.binghe.struts.ApplicationResources"/>
<plug-in classname= "Org.apache.struts.validator.ValidatorPlugIn" >
<set-property property= "Pathnames"
Value= "/web-inf/validator-rules.xml,
/web-inf/validation.xml "/>
</plug-in>
<plug-in
Classname= "Org.springframework.web.struts.ContextLoaderPlugIn" >
<set-property property= "Contextconfiglocation"
Value= "/web-inf/applicationcontext.xml"/>
</plug-in>
</struts-config>
3.applicationcontext.xml.xml
This is spring's proprietary configuration file, which configures agent hibernate resources and Struts action
The format is as follows:
<beans>
<bean id= "DataSource"
Class= "Org.apache.commons.dbcp.BasicDataSource"
destroy-method= "Close" >
<property name= "Driverclassname" >
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</property> <property name= "url" >
<value>jdbc:microsoft:sqlserver://127.0.0.1:1400;DatabaseName=books</value>
</property>
<property name= "username" >
<value>sa</value>
</property>
<property name= "Password" >
<value>123</value>
</property>
</bean>
<bean id= "Sessionfactory"
class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "DataSource" >
<ref local= "DataSource"/>
</property>
<property name= "Mappingresources" >
<list>
<value>
Com/binghe/hibernate/booktype/booktype.hbm.xml
</value>
<value>
Com/binghe/hibernate/book/book.hbm.xml
</value>
</list>
</property>
<property name= "Hibernateproperties" >
<props>
<prop key= "Hibernate.dialect" >
Org.hibernate.dialect.SQLServerDialect
</prop>
<prop key= "Hibernate.show_sql" >false</prop>
</props>
</property>
</bean> <bean id= "TransactionManager"
class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "Sessionfactory" >
<ref local= "Sessionfactory"/>
</property>
</bean> <bean id= "Booktypedao"
class= "COM.BINGHE.SPRING.BOOKTYPE.BOOKTYPEDAOIMP" >
<property name= "Sessionfactory" >
<ref local= "Sessionfactory"/>
</property>
</bean>
<bean id= "Bookdao"
class= "COM.BINGHE.SPRING.BOOK.BOOKDAOIMP" >
<property name= "Sessionfactory" >
<ref local= "Sessionfactory"/>
</property>
<property name= "Booktypedao" >
<ref bean= "Booktypedaoproxy"/>
</property>
</bean>
<bean id= "Booktypedaoproxy"
class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
<property name= "TransactionManager" >
<ref bean= "TransactionManager"/>
</property>
<property name= "Target" >
<ref local= "Booktypedao"/>
</property>
<property name= "Transactionattributes" >
<props>
<prop key= "add*" >PROPAGATION_REQUIRED</prop>
<prop key= "update*" >PROPAGATION_REQUIRED</prop>
<prop key= "delete*" >PROPAGATION_REQUIRED</prop>
<prop key= "check*" >PROPAGATION_REQUIRED,readOnly</prop>
<prop key= "find*" >PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id= "Bookdaoproxy"
class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
<property name= "TransactionManager" >
<ref bean= "TransactionManager"/>
</property>
<property name= "Target" >
<ref local= "Bookdao"/>
</property>
<property name= "Transactionattributes" >
<props>
<prop key= "add*" >PROPAGATION_REQUIRED</prop>
<prop key= "update*" >PROPAGATION_REQUIRED</prop>
<prop key= "delete*" >PROPAGATION_REQUIRED</prop>
<prop key= "zhuxiao*" >PROPAGATION_REQUIRED</prop>
<prop key= "check*" >PROPAGATION_REQUIRED,readOnly</prop>
<prop key= "find*" >PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean name= "/booktype"
Class= "Com.binghe.struts.action.booktype.BookTypeAction"
Singleton= "false" >
<property name= "Booktypedao" >
<ref bean= "Booktypedaoproxy"/>
</property>
</bean>
<bean name= "/book"
Class= "Com.binghe.struts.action.book.BookAction"
Singleton= "false" >
<property name= "Bookdao" >
<ref bean= "Bookdaoproxy"/>
</property>
</bean>
</beans>
4. Hibernate configuration file for Pojoclassname.bhm.xml,pojoclassname is a javabean you define, you can put this configuration file in and Pojobean a directory, can also be placed in other directories, and then reference, the format is as follows:
<id name= "id" column= "id" type= "integer" >
<generator class= "native"/>
</id> <property name= "code" column= "Code" type= "string"
Not-null= "false"/>
<property name= "name" column= "name" type= "string"
Not-null= "false"/>
<property name= "UserId" column= "userId" type= "integer"
Not-null= "false"/>
<property name= "typeID" column= "typeID" type= "integer"
Not-null= "false"/>
<many-to-one name= "user" class= "Com.binghe.utils.UserBean"
Insert= "false" Update= "false" >
<column name= "userid"/>
</many-to-one>
<many-to-one name= "Readertype"
class= "Com.binghe.hibernate.readertype.ReaderType" insert= "false"
Update= "false" >
<column name= "typeID"/>
</many-to-one>
<set name= "Borrowbills" lazy= "false"
Inverse= "true" cascade= "None" >
<key column= "Readerid"/>
<one-to-many
class= "Com.binghe.hibernate.borrowbill.BorrowBill"/>
</set> All of the configuration information above is extracted from a project I made myself.
The dependency injection/control inversion in spring is good, you can define any interface, then implement the method inside the interface, inject your interface into any place through the spring configuration file, if you must refer to an interface, must have interface definitions and Getter methods at the point of reference, But you can use this interface as an attribute like JavaBean, JavaBean have getter and setter methods
The transaction agent in spring is pretty good, too.
<property name= "Target" >
<ref local= "Booktypedao"/>
</property>
Target is the class to be injected, and the interface implemented by the proxy class
<property name= "Transactionattributes" >
<props>
<prop key= "add*" >PROPAGATION_REQUIRED</prop>
<prop key= "update*" >PROPAGATION_REQUIRED</prop>
<prop key= "delete*" >PROPAGATION_REQUIRED</prop>
<prop key= "check*" >PROPAGATION_REQUIRED,readOnly</prop>
<prop key= "find*" >PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
Transactionattributes is the method of the agent to provide transactions, such as you define a method to start with add, then it can have transaction management, for all operations within it can implement transaction mechanism, if there are exceptions to roll back the transaction
Hibernate mapping mechanism is ORM, object-oriented database query, query must provide query class (such as the Find Method "from the book" HQL statements in the book is not the table name but the class name), to get its instance, The JavaBean properties of the corresponding database must all be of an object type, int, double must be defined as Integer and double, and a lazy property of the map table should be set to false, or it will be an error if you load a record after querying the database. One-to-many two-linked:
One-to-many Mappings, complex
<set name= "Borrowbills" lazy= "false"
Inverse= "true" cascade= "None" >
<key column= "Readerid"/>
<one-to-many
class= "Com.binghe.hibernate.borrowbill.BorrowBill"/>
</set>
Cascade property is not easy to use, if there is a delete record operation when I set it to none value, both sides to set, or error, because can not notify each other
Multi-pair mapping, simple
<many-to-one name= "name" type= "com. ClassName "update=" false "insert=" false ">
<column name= "NameId"/>
</many-to-one>

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.