最近一直在搞spring,struts,hibernate的环境搭建,包括单独的开发环境和整合的环境,今天就说说最新版的SSH整合
首先,到官方网站上下载jar包,解压后去里面分别拷贝包,之所以自己去拷贝包,这样可以让自己明确到底用到了哪些包,以后再碰到这类问题,就轻车熟路了。
废话不多说,直接开始。
第一步:建立工程
第二步:导入包
struts需要的核心包:
说明:由于struts升级到了2.3以后,需要导入commons-lang.3-3.1.jar才能找到StringUtils.class
hibernate4的核心包:
Spring3的核心包:
注意:这里没有导入aop的包,如果要用到aop的话,就再自己导入相应包就可以了
第三步:编写配置文件
web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>application</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="ssh" namespace="/" extends="struts-default">
</package>
</struts>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<context:annotation-config />
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocations">
<value>WebContent/WEB-INF/hibernate.cfg.xml</value>
</property>
<!-- 注意这里配置了mappingResouces,就不要在hibernate.cfg.xml中配置mapping了,建议在hibernate.cfg.xml中配置 -->
<!--
<property name="mappingResources">
<list>
<value>com/ssh/domain/hbm/Student.hbm.xml</value>
</list>
</property>
-->
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/ssh/domain/Student"/>
</session-factory>
</hibernate-configuration>
这样基本的工程就搭建起来了,不过有几点需要注意
spring3+hibernate4的配置有点变化,<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">原来是hibernate3,现在变成了4,而且spring3移除了HibernateDaoSupport,所以我们操作数据库就需要自己创建session