SSH spring+struts2+hibernate整合步骤

来源:互联网 发布:淘宝女装店背景图 编辑:程序博客网 时间:2024/06/08 12:35

说明:这里是使用eclipse来编写SSH,初次整理,请多关照!

具体整合步骤如下:

1.添加相对应的jar包(36个jar包)

2.添加hibernate配置文件 hibernate.cfg.xml

3.添加spring配置文件applicationContex.xml(如不想修改配置,请将文件放置WEB-INF目录下)

4.配置applicationContex.xml:

   4.1 配置DataSource

   <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="username" value="root"></property>//用户名
        <property name="password" value="0000"></property>//密码
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>//驱动
        <property name="url" value="jdbc:mysql:///test"></property>//url 其中test为数据库名
    </bean>

   4.2 配置sessionFactory

   <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="dataSource" ref="dataSource"></property>
    </bean>

   4.3配置hibernateTemplate

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

5.创建bean类(相当于数据库中的表),定义对应属性(相当于表中的字段),生成spring映射文件。

    在hibernate.cfg.xml中添加mapping 即:<mapping resource="org/mc/bean/BeanTest.hbm.xml"/>

6.创建Struts配置文件.xml文件 配置如下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

/*

*具体内容

*/

</struts>

7.配置web.xml文件

   7.1 Struts核心过滤器

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

  7.2 spring监听

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

8.创建action类

9.在applicationContext.xml中配置bean来管理action类

10.配置Struts.xml 其中class引用applicatxionContext.xml中相对应的bean的id

11.在applicationContext.xml中添加相应的bean来管理对应的类。例如dao,service,action等。为这些类的有set方法的属性注入值。

阅读全文
0 0