struts+spring+hibernate感悟
1.web.xml用于web服务器装载过滤器,servlet,以及配置文件
struts在此被装载org.apache.struts.action.ActionServlet,还有它的配置参数config文件struts-config.xml,spring在此被装载org.springframework.web.context.ContextLoaderServlet还有它的配置文件applicationContext.xml,其他省略不列出
例子:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<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这个文件
这个是strust专有配置文件,配置form,全局转发,action,message-resources(用于显示提示信息),插件
格式如下:
<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
这个是spring的专有配置文件,里面配置代理hibernate资源和struts的action
格式如下:
<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</property>
<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>
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
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的配置文件为POJOClassName.hbm.xml,POJOClassName是你定义的一个javabean,你可以把这个配置文件放在和pojobean一个目录下,也可以放在其他目录,然后引用,格式如下:
<hibernate-mapping package="com.binghe.hibernate.reader">
<id name="id" column="id" type="integer">
<generator class="native" />
</id>
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>
</hibernate-mapping>
上面的所有配置信息是我从自己做的一个项目中抽取出来的
spring中的依赖注入/控制反转是不错的,你可以定义任何接口,然后实现接口里面的方法,通过spring的配置文件把你的接口注入到任何地方,前提是你引用的必须是接口,在引用的地方必须有接口定义以及getter方法,不过你可以把这个接口当作类似javabean的一个属性一样来使用,javabean都有getter和setter方法的
spring里面的事务代理也挺挺不错的
<property name="target">
<ref local="bookTypeDAO" />
</property>
target它是指向要注入的类,代理这个类所实现的接口
<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是对所代理的方法哪些方法提供事务,比如你定义一个以add开头的方法,那它就可以有事务管理了,对于它里面的所有操作,都可以实现事务机制,若有异常就回滚事务
hibernate的映射机制是orm,面向对象的数据库查询,查询时必须提供查询类(如find方法"from Book"hql语句中的Book不是表名而是类名),以得到它的实例,对应数据库的javabean的属性必须都是对象型的,int、double必须定义为Integer和Double类型,映射表有个lazy属性应该设置false,不然在查询数据库后加载一条记录时会报错。一对多双相关联:
一对多映射,复杂
<set name="borrowBills" lazy="false"
inverse="true" cascade="none">
<key column="readerid" />
<one-to-many
class="com.binghe.hibernate.borrowbill.BorrowBill" />
</set>
cascade属性不好用,如果有删除记录操作时我都把它设置成none值,双方都要设置,否则报错,因为没法相互通知
多对一映射,简单
<many-to-one name="name" type="com.ClassName" update="false" insert="false">
<column name="nameId" />
</many-to-one>