三者中hibernate作为一个解决关系型数据库和面向对象编程设计的中间解决方案,在如今众多技术特别是spring技术的日趋完善,将会逐渐的退出。
比如 hibernate.cfg.xml 这个配置文件可以去除了,由spring来取而代之:
在spring配置文件applicationContext.xml中:写道
<!-- 数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"></property> <property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;databasename=excersice"></property> <property name="username" value="sa"></property> </bean> <!-- session管理配置 --> <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.SQLServerDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>com/config/student/Student.hbm.xml</value> </list> </property> </bean>
注意在上面的配置文件中com/config/student/Student.hbm.xml路径中不能用.代替/
在 SSH 整合开发中:一般可以分为两步:spring+hibernate 和 spring+struts.
1. spring+hibernate就只需要上面那样配置就可以了.
2. 下面说说spring+struts
先说说spring+struts1.x
如果spring只有一个配置文件,在web.xml文件中: 写道
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml</param-value> </context-param>
只需要其中任一段即可完成spring+struts1.x
如果要由spring来管理控制器:
在struts-config.xml文件中: 写道
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
再来说说spring+struts2.0
在web.xml文件中: 写道
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
下面一段为可选项,不配置程序会自动执行:
在struts.xml配置文件中: 写道
<constant name="struts.objectFactory" value="spring"/>
特别要强调的是:为了避免麻烦:最好将struts的配置文件命名为:struts.xml并放在src目录下;还要注意一点:extends="struts-default"不能写错了。
只要完成上面步骤,就能实现简单的SSH了