SSH的整合


 

我發表下SSH的整合,為了我能更好地記住。

struts2和hibernate的配置我這里就不多說了,先把兩個有關的東西說下。一個是所有的包。struts2+hibernate3+spring2.5我包准備放上去給大家下載。

http://pan.baidu.com/share/link?shareid=3160001548&uk=3307409781

放在的我百度雲盤里面

閑話不說:開始。

先是struts2和hibernate的配置

web.xml:

<?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">
	<!-- structs2的配置 -->
	<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>

 然后在src下添加一個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>
</struts>

 然后在src下添加一個hibernate.cfg.xml:(這是一些數據庫的配置)

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC 
 "-//Hibernate/Hibernate Configuration DTD3.0//EN" 
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.  -->
<hibernate-configuration>
    <session-factory>
        <property name="connection.username">sa</property>
        <property name="connection.url">
            jdbc:jtds:sqlserver://localhost:1433;DatabaseName=LQQ
        </property>
        <property name="dialect">
            org.hibernate.dialect.SQLServerDialect
        </property>
        <!--<property name="myeclipse.connection.profile">
            LoginSystem
            </property>  -->
        <property name="connection.password">sa</property>
        <property name="connection.driver_class">
            net.sourceforge.jtds.jdbc.Driver
        </property>
        <property name="show_sql">true</property>
        <!-- POJO 類映射配置-->
        <mapping resource="sedion/xq/ORM/Stuinfo.hbm.xml" />
    </session-factory>
</hibernate-configuration>

重點講Spring:
1. src下面新建applicationContext.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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 這是個例子待會會用到  可以先不要 -->
    <bean id="TUser_id" class="test.TUser">
        <property name="username" value=""></property>
        <property name="allname" value=""></property>
        <property name="address" value="溫州市"></property>
    </bean>
    
</beans>

 然后在web-xml:

<context-param>
         <param-name>contextConfigLocation</param-name>
		<!--spring 的配置文件加載路徑-->
          <param-value>classpath:applicationContext.xml</param-value>
</context-param>

 

2.Spring基本配置完畢

下面我們測試下:src建個test包

建兩個類User和SpringTest類:

 

public class User implements java.io.Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String username;
    private String allname;
    private String address;

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAllname() {
        return this.allname;
    }

    public void setAllname(String allname) {
        this.allname = allname;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

 

 

public class SpringTest {
	public static void main(String[] args) {
		// 加載spring配置文件,初始化IoC容器
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		// 從容器 接管Bean
		TUser user = (TUser) ac.getBean("TUser_id");
		// 輸出歡迎信息
		System.out.println("Hello:" + user.getUsername() + ";u is in "
				+ user.getAddress() + " ; and u is  " + user.getAllname());
	}
}


 

別忘了:applicationContext.xml中配置一個bean,在xml中增加如下代碼:

 

<!-- 這是個例子待會會用到  可以先不要 -->
  <bean id="TUser_id" class="test.TUser">
        <property name="username" value="你"></property>
        <property name="allname" value="李"></property>
        <property name="address" value="溫州市"></property>
    </bean>

 

 

 

運行main方法后

控制台出現:Hello:你;u is in 溫州市 ; and u is  李

下面開始整合

整合struts2

  1.整合struts2配置web.xml文件。在web.xml中加入以下代碼:

 

	<!-- Spring與Struts的整合其實就是把Struts的Action類交給Spring來管理 -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener	
		</listener-class>
	</listener>

 

 2.舉個例子:

  把strutsaction交給spring:

  先把 

<action name="login" class="test.LoginAction ">

 改為

<action name="login" class="loginAction_id">

 

 

然后在

applicationContext.xml中增加如下代碼:

 

<bean id="loginAction_id" class="test.action.LoginAction" scope="prototype">
</bean>

 

 

整合Hibernate

 Spring整合Hibernate主要是對hibernateSession進行管理,包含Session的創建、提交、關閉的整個生命周期。Spring對事務的管理應用了AOP的技術

1)        配置sessionFactoryspring來創建Session

我們原來是用HibernateSessionFactory.java來創建Session的,現在刪除即可,交給Spring創建。(這樣就spring來整合hibernate來創建session)

這里,創建了一個Session工廠類的Bean,其ID為“sessionFactory”.

 

applicationContext.xml中增加如下代碼:

 

<!-- 配置sessionFactory -->

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

     <property name="configLocation">

         <value>classpath:hibernate.cfg.xml</value>

     </property>   

 </bean>   

 

還有個例子上的配置:

<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
	                  	<value>sedion/xq/bean/User.hbm.xml</value><!--這里是將映射文件加載-->
			</list>
		</property>

	</bean>

 

2.

然后DAO層匹配一個session工廠,<ref bean="sessionFactory"/>這個sessionFactory是指session工廠的ID。

 

<bean id="userDao" class="sedion.xq.dao.iml.UserDAOImpl" scope="singleton">

		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
</bean>

 

 3.service層注入DAO層

         <bean id="userService" class="sedion.xq.sevice.iml.UserServiceImpl">
		<property name="userDAO" ref="userDao"></property>
	</bean>

 然后就是各個action注入service層:(隨便寫個)

	<bean id="saveUesrAction" class="sedion.xq.action.user.SaveUserAction"
		scope="prototype">
		<property name="userService" ref="userService"></property>
	</bean>

 整合就這樣好

下面是整合下載的例子。

http://pan.baidu.com/share/link?shareid=3154531670&uk=3307409781


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM