今天給你們介紹一下最新版本的SSH(struts2.2.1+ hibernate3.6+spring3.0.5)組合。注意本講解爲手工搭建!
1、爲SSH作好準備
java
struts2-2.2.1-all.zip
hibernate-distribution-3.6.0.Final-dist.zip
spring-framework-3.0.5.RELEASE.zip
spring-framework-2.5.6-with-dependencies.zip
slf4j-1.6.1.zip
apache-tomcat-6.0.29.zip
mysql-connector-java-5.1.13-bin.jar
mysql-essential-5.1.53-win32.msinode
工具用eclipse或者myeclipse 文件都行mysql
2、搭建開發環境 打開MyEclipse,新建一個web project (選擇Java EE5.0)web
3、須要的jar包spring
一、hibernate-3.6.0 配置sql
hibernate-distribution-3.6.0.Final-dist.zip中須要以下jar express
hibernate3.jar apache
lib/required/antlr-2.7.6.jar api
lib/required/commons-collections-3.1.jar
lib/required/dom4j-1.6.1.jar
lib/required/javassist-3.12.0.GA.jar
lib/required/jta-1.1.jar
lib/required/slf4j-api-1.6.1.jar
lib/jpa/hibernate-jpa-2.0-api-1.0.0.Final.jar //新版本須要jar
slf4j-1.6.1.zip中須要以下jar
slf4j-nop-1.6.1.jar
mysql-connector-java-5.1.13-bin.jar //mysql 的驅動包
注意:新版本已經和Annotation作了組合 要用Annotation不須要另外加入jar。
在測試的時候也不須要第一種寫法:
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory(); //如今已通過時
用第二種:
SessionFactory sf = new Configuration().configure().buildSessionFactory();//能夠直接使用Annotation
二、spring 3.0.5配置
spring-framework-3.0.5.RELEASE.zip中須要以下jar
dist/* //爲了方便考入此目錄下的全部jar,不想所有考入的本身選擇
spring-framework-2.5.6-with-dependencies.zip 在此包中考入spring
aopalliance/aopalliance.jar
aspectj/aspectjrt.jar
aspectj/aspectjweaver.jar
cglib/cglib-nodep-2.1_3.jar
jakarta-commons/commons-pool.jar
jakarta-commons/commons-dbcp.jar
jakarta-commons/commons-logging.jar
你們能夠看到有了spring2.5.6的包 3.0所須要的其餘類就能在其中找比較方便。
注意:cglib-nodep-2.1_3.jar 包也能夠換成asm-2.2.3.jar和cglib-2.2.jar
三、struts2.2.1 配置
struts2-2.2.1-all.zip 中加入以下jar
lib/ognl-3.0.jar
lib/xwork-core-2.2.1.jar
lib/freemarker-2.3.16.jar
lib/struts2-core-2.2.1.jar
lib/struts2-spring-plugin-2.2.1.jar
lib/commons-io-1.3.2.jar
lib/commons-fileupload-1.2.1.jar
lib/commons-logging-1.0.4.jar
javassist-3.7.ga.jar //這個包在lib下沒有;從apps/struts2-blank-2.2.1.war中的lib文件裏找到
注意:若是使用ognl的jar包是2.7如下的就不用 javassist-3.7.ga.jar 了
到此爲止全部的jar包就加完畢了 javassist-3.7.ga.jar 和 commons-logging.jar 已經重複刪除不須要的(保留版本高的就行)。總共是44個jar
4、XML文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 用註解方式注入bean -->
<context:annotation-config/>
<context:component-scan base-package="com.yj"/>
<!-- 數據庫鏈接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/sshtest" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- hibernate sessionFactory 建立 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>com.yj.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事物配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(public * com.yj.service..*.*(..))" id="myPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
</aop:config>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
注意:一、只用在spring中配置了事物才能在web.xml配置openSessionInViewFilter
不然會出錯。
二、openSessionInViewFilter必須配置在strutsfilter以前。
結束:我把本身的demo上傳到附件 沒有jar文件須要的本身下載把,也能夠聯繫我。關於demo中的問題是service和demo都沒有提取接口這樣在開發中是不容許的。各位不要學我
在demo中我全部的測試spring 的測試 須要junit-4.4以上版本。用這個測試的好處是測試不會破壞數據庫的內容,由於是事物級的測試。
package com.yj.service;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import com.yj.model.User;
@ContextConfiguration("classpath:applicationContext.xml")
public class UserServiceTest extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private UserService userService;
public void testSave() {
User user = new User();
user.setPassword("1234");
user.setUsername("張三3");
userService.save(user);
}
public void testExits() {
UserService userService = new UserService();
User user = new User();
user.setPassword("1234");
user.setUsername("張三");
userService.exits(user.getUsername());
}
}
因爲時間關係在這裏沒有給你們介紹每一個jar的做用。
ssh.rar (18 KB)
下載次數: 1690
hibernate_lib.rar (6 MB)
下載次數: 2707
spring_lib.rar (5.8 MB)
下載次數: 2672
struts_lib.rar (3.1 MB)
下載次數: 1778