浏览 1545 次
锁定老帖子 主题:SSH简单整合代码
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-11-23
最后修改:2008-11-23
[size=large][size=medium][size=small][size=x-small][size=xx-small]开发环境 :JDK1.5,Struts1.2,Hibernate3.0,spring1.2
说明:采用MVC模式,引入IOC技术。代码分DAO层,SERVICE层,分别实现DAO层接口,SERVICE接口。在SERVICE层中注入DAO接口。在struts的action里注入SERVICE层接口。使用各层具有相对独立,更好的体现了面向对象思想。DAO的实现可以去extends HibernateDaoSupport 用 getHibernateTemplate()方法操作底层数据库。也可以用这句代码创建一个Session对象 Session session = SessionFactoryUtils.getSession(getSessionFactory(), false); 在用session去操作底层数据库。【完成操作要关闭session释放占用的内存】 【applicationContext.xml】 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <import resource="application-dao.xml"/><!--配置DAO层的BEAN文件信息--> <import resource="application-action.xml"/><!--配置ACTION的BEAN文件信息--> <!--配置数据源采用DBCP连接池--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value><![CDATA[jdbc:mysql://localhost:3306/pethospital?useUnicode=true&characterEncoding=GBK]]></value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean> <bean id="SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <!--加载HIBERANTE映射文件--> <value>net/pethospital/web/value/DoctorInfo.hbm.xml</value> <value>net/pethospital/web/value/PetInfo.hbm.xml</value> </list> </property></bean> </beans> 【application-dao.xml】 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="doctorInfoDaoImpl" class="net.pethospital.web.dao.impl.DoctorInfoDaoImpl"> <property name="sessionFactory"> <ref bean="SessionFactory" /> </property> </bean> <!--在SERVICE层注入DAO接口--> <bean id="doctorInfoManagerImpl" class="net.pethospital.web.service.impl.DoctorInfoManagerImpl_BS"> <property name="doctorDao" ref="doctorInfoDaoImpl"></property> </bean> </beans> 【application-action.xml】 <!--配置有关STRUTS ACTION的BEAN--> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean [color=darkred]name="/doctorinfo_Action" class="net.pethospital.web.action.DoctorInfo_Action" singleton="false"> <property name="doctorInfoManagerImp"> <ref bean="doctorInfoManagerImpl" /> </property> </bean> </beans>[/color]【struts-config.xml】 <!--注意ACTION的PATH要和application-action.xml里的name一样--> <action path="/doctorinfo_Action" name="doctorInfoForm" scope="request" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="success" path="/index/success.jsp"></forward> </action> <!--这句代码完成和SPRING的整合--> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/> </plug-in>【web.xml】 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <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> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>[size=xx-small] 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |