SSH 整合方案1

SSH(方案一)

1,导入spring,包含(ormDAO包)(web包)

2,导入hibernate,将hibernate的信息到写到Spring的applicationContext.xml中,使hibernate受spring管理,

       配置如:

       <?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

 

<beans>

 

<bean id="run" class="com.sk.run.RunHiber"><!—RunHiber类继承了HibernateDAOSupport抽象类,此类中有HibernateTemplate属性,so注入 -->

<property name="hibernateTemplate">

    <ref bean="myHT"/>

</property>

</bean>

<bean id="myHT" class="org.springframework.orm.hibernate3.HibernateTemplate">

<property name="sessionFactory" >

    <ref bean="mySessionFactory"/>

</property>

</bean>

    <bean id="myDatasource"

       class="org.apache.commons.dbcp.BasicDataSource">

       <property name="driverClassName">

           <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>

       </property>

       <property name="url">

           <value>jdbc:microsoft:sqlserver://127.0.0.1:1433</value>

       </property>

       <property name="username">

           <value>sa</value>

       </property>

    </bean>

    <bean id="mySessionFactory"

       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       <property name="dataSource">

           <ref bean="myDatasource" />

       </property>

       <property name="hibernateProperties">

           <props>

              <prop key="hibernate.dialect">

                  org.hibernate.dialect.SQLServerDialect

              </prop>

           </props>

       </property>

       <property name="mappingResources">

           <list>

              <value>com/sk/bean/Student.hbm.xml</value>

           </list>

       </property>

    </bean>

</beans>

 

3,导入struts()修改web.xml,添加spring的路径和监听

    配置如:

    <?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <servlet>

    <servlet-name>action</servlet-name>

    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

    <init-param>

      <param-name>config</param-name>

      <param-value>/WEB-INF/struts-config.xml</param-value>

    </init-param>

    <init-param>

      <param-name>debug</param-name>

      <param-value>3</param-value>

    </init-param>

    <init-param>

      <param-name>detail</param-name>

      <param-value>3</param-value>

    </init-param>

    <load-on-startup>0</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>action</servlet-name>

    <url-pattern>*.do</url-pattern>

  </servlet-mapping>

  <!-- 指定applicationContext的位置其必须的监听器 -->

  <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>

</web-app>

 

4.所有的action都继承 ActionSupport类(此类是action的扩展类)

    用WebApplicationContext 获取 spring管理的bean

    代码如:

    public class LoginAction extends ActionSupport {

   

 

   

    public ActionForward execute(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response) {

       LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub

       String name=loginForm.getName();

       String password=loginForm.getPassword();

       WebApplicationContext context=this.getWebApplicationContext();

        RunHiber r=(RunHiber)context.getBean("run");

        if(r.getStudent(name, password)!=null){

            return mapping.findForward("succ");

        }

       return null;

    }

}

Run类的内容:

package com.sk.run;

 

import java.util.Iterator;

import java.util.List;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.orm.hibernate3.HibernateTemplate;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

 

import com.sk.bean.Student;

 

public class RunHiber extends HibernateDaoSupport{

    public Student getStudent(String name,String password){

      

       HibernateTemplate t= this.getHibernateTemplate();

       String hql="from Student as s where s.username=?";

       List list=t.find(hql, name);

       Iterator i=list.iterator();

       while(i.hasNext()){

           Student s=(Student)i.next();

           if(s.getPassword().equals(password)){

              return s;

           }

       }

      

       return null;

    }

   

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

      

       RunHiber r=(RunHiber)context.getBean("run");

       r.getStudent("aaa", "aaa");

    }

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gamebox1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值