SSH整合笔记

来源:互联网 发布:python采集图片教程 编辑:程序博客网 时间:2024/06/09 14:49

SSH整合笔记

1.加入 struts2的环境

    1.1加入必须jar包

       Commons-fileupload-*.jar

       Freemarker-*.jar

       Ognl-*.jar

       Struts1-core-*.jar

       Xwork-core-*.jar

    1.2 配置web.xml,将请求交给struts来处理

       <filter>

          <filter-name>s</filter-name>

         <filter-class>org.apache.struts2.dispatcher.ng.

filter.StrutsPrepareAndExecuteFilter</filter-class>

       </filter>

       <filter-mapping>

          <filter-name>s</filter-name>

          <url-pattern>/*</url-pattern>

       </filter-mapping>

   1.3 加入struts.xml文件

      ----加入struts的tld标签库引入

                |---在已经加入的struts2-core-*.jarstruts-2.0.dtd中有

   1.4 写一个Action,并配置到struts.xml中

      ----com.xcz.action.UserAction

      ------------------------------------------------------------------

<packagename="struts"extends="struts-default">

         <action name="userdo"class="com.xcz.action.UserAction">

            <result name="index">index.jsp</result>

         </action>

      </package>

      ------------------------------------------------------------------

 

2.加入Hibernate的环境

    2.1 加入必须的jar包

       antlr-*.jar//其他工具辅助包

commons-collection-*.jar//必须的集合类工具包

dom4j-*.jar//hibernate靠其读写xml配置文件

hibernate*.jar//核心包

jta-*.jar//jta规范包

db-connection-java-*.jar//数据库连接包

 

//以下的包hibernate版本不一样,存在差异

javassist-*.jar//字节码增强包(更高版本已经换成[cglib.jar]+[asm.jar]+[asm-attrs.jar]

slf4j-api-*.jar//

slf4j-nop-*.jar//

    2.2 加入hibernate.cfg.xml

       ----加入hibernate的tld标签库引入(注意配置文件引入的dtd头,这里是HibernateConfiguration DTD)

                |---在已经加入的hibernate3.jar包中/org/hibernate/hibernate-configuration-3.0.dtd中有

    ----------------------------------------------------------------------------------------------

           <!DOCTYPEhibernate-configuration PUBLIC

         "-//Hibernate/HibernateConfiguration DTD 3.0//EN"

         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

    ----------------------------------------------------------------------------------------------

----写入数据库连接属性

    -----------------------------------------------------------------------------------------------

       <hibernate-configuration>

         <session-factory>

            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db_ssh</property>

            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

            <property name="hibernate.connection.username">root</property>

            <property name="hibernate.connection.password">mysql</property>

            <property name="show_sql">true</property>  

     

         </session-factory>

</hibernate-configuration>

-----------------------------------------------------------------------------------------------

    2.3 写实体类User(多个对应一个Derpartment)、Derpartment(对应一个Set<User>)

    2.4 在实体类的包下分别写xml映射文件Xxxx.hbm.xml(注意配置文件引入的dtd头,这里是Hibernate Mapping DTD)

       User.hbm.xml

    ----------------------------------------------------------------------------------------------

       <!DOCTYPEhibernate-mappingPUBLIC

      "-//Hibernate/HibernateMapping DTD 3.0//EN"

      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping>

         <class name="com.xcz.domain.User"table="t_user">

            <id name="id"column="id"length="36">

               <generator class="uuid.hex"/>

            </id>

     

            <property name="username"length="20"/>

            <property name="password"length="20"/>

            <!—- 多对一指向deptid (外键)-->

            <many-to-one name="dept"column="deptid"class="com.xcz.domain.Department"/>

         </class>

</hibernate-mapping>

    ----------------------------------------------------------------------------------------------

       Derpartment.hbm.xml

    ----------------------------------------------------------------------------------------------

       <!DOCTYPEhibernate-mappingPUBLIC

      "-//Hibernate/HibernateMapping DTD 3.0//EN"

      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping>

         <class name="com.xcz.domain.Department"table="t_dept">

            <id name="id"column="id"length="36">

               <generator class="uuid.hex"/>

            </id>

     

            <property name="dname"length="50"/>

     

            <set name="users"inverse="true"><!--一对多Set集合属性配置 -->

               <key column="deptid"/>   <!—column就是多对一指向的属性 -->

               <one-to-many class="com.xcz.domain.User"/>

            </set>

         </class>

</hibernate-mapping>

    ----------------------------------------------------------------------------------------------

 

3.加入Spring

    3.1 加入Spring的必须jar包以及整合的jar包

       Aspectjrt.jar

       Aspectjweaver.jar

       Commons-loggin.jar

       Log4j-*.jar

       Spring.jar

       Struts2-spring-plugin-*.jar

    3.2 配置 web.xml(文件加载的时候)

注:在SSH中,接收请求的Servlet是由Struts的ActionServlet来配置的,因此此时就不能够使用DispatchServlet来接受请求。为了能在此时能过加载Spring的Bean配置,可以在Web.xml中配置一个监听器,并通过<context-param>指定xml文件名。

      --------------------------------------------------------------------------------------------

<context-param>

          <param-name>contextConfigLocation</param-name>

          <!-- ,classpath*:applicationContext-*.xml,/WEB-INF/applicationContext*.xml-->

          <param-value>classpath*:applicationContext*.xml</param-value>

       </context-param>

 

       <listener>

       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

       </listener>

--------------------------------------------------------------------------------------------

    3.3 添加spring 的配置文件applicationContext.xml

       注:加入配置文件引入的dtd头

--------------------------------------------------------------------------------------------

       <beansxmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      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/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd

         http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

     

      <bean>

--------------------------------------------------------------------------------------------

    3.4 在applicationContext*.xml中SessionFactory的配置

       注:配置SessionFactory对象,为DAO层提供Hibernate的数据库连接对象,其中需要注入2.2中配置的DataSource。

--------------------------------------------------------------------------------------------

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

         <property name="configLocation"value="class:hibernate.cfg.xml"/>

</bean>

--------------------------------------------------------------------------------------------

    3.5 在applicationContext*.xml中配置事务

       注:为SessionFactory对象添加事务配置组件,并注入3.3中配置的SessionFactory对象。

--------------------------------------------------------------------------------------------

<beanname="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">

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

</bean>

--------------------------------------------------------------------------------------------

    3.6 编写DAO

       注:Dao需要继承HibernateDaoSupport

--------------------------------------------------------------------------------------------

        publicclass UserDaoImpl extends HibernateDaoSupport implements UserDao {

 

           publicvoid addUser(User user) {

              this.getHibernateTemplate().save(user);

           }

 

           publicUser getUser(String id) {

              return(User) this.getHibernateTemplate().get(User.class, id);

           }

 

           publicvoid updateUser(User user) {

              this.getHibernateTemplate().update(user);

           }

   

           publicUser getByUsername(String username) {

              return(User) this.getSession().createQuery(

"from User where username =?").setParameter(0, username).uniqueResult();

           }

}

--------------------------------------------------------------------------------------------

    3.7 在applicationContext*.xml中配置DAO中的Bean,并注入SessionFactory

--------------------------------------------------------------------------------------------

       <beanid="userDao"class="com.chinasofti.dao.impl.UserDaoImpl">

         <property name="sessionFactory">

            <ref bean="sessionFactory"/>

         </property>

      </bean>

--------------------------------------------------------------------------------------------

    3.8 写一个Action,并配置到applicationContext*.xml,并注入需要的类

--------------------------------------------------------------------------------------------

<beanid="userAction"class="com.chinasofti.action.UserActoin"scope="prototype">

         <property name="userService"ref="userService"/>

         <property name="departmentService"ref="departmentService"/>

      </bean>

--------------------------------------------------------------------------------------------


原创粉丝点击