整合SSH框架步骤(纯手打,有错勿喷,帮纠正!)

来源:互联网 发布:华为机顶盒安装软件 编辑:程序博客网 时间:2024/06/05 03:47
                                                                            第一步:
    
     整合hibernate

     1. 导包:pom.xml   <maven repository>  <hibernate.org>

********************************************************

              
    <!-- json -->
    <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.2.1</version>
        <classifier>jdk15</classifier>
    </dependency>
    
       <!-- 引入Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.0-b07</version>
        <scope>provided</scope>
    </dependency>
    
    <!-- 引入Hibernate依赖 -->
    <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-core</artifactId>
       <version>5.2.10.Final</version>
    </dependency>
    
    <!-- 引入Mysql依赖 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.43</version>
    </dependency>
    
    <!-- 引入Spring依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>
    
    <!-- 引入c3p0数据库连接池 -->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.2</version>
    </dependency>
    
    <!-- 引入Hibernate整合Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>
    
    <!-- 引入spring-aspects:解析事务的表达式 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>
    
    <!-- 引入Struts2依赖 -->
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.3.33</version>
    </dependency>
    
    <!-- struts2整合Spring的 插件包 -->
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-spring-plugin</artifactId>
        <version>2.5.12</version>
    </dependency>
    
    <!-- log4J -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.8.2</version>
    </dependency>
    
    <!-- 引入shiro依赖  START-->
    <dependency>
          <groupId>org.apache.shiro</groupId>
          <artifactId>shiro-core</artifactId>
          <version>${shiro.version}</version>
    </dependency>
    
    <dependency>
          <groupId>org.apache.shiro</groupId>
          <artifactId>shiro-web</artifactId>
          <version>${shiro.version}</version>
    </dependency>
    
    <dependency>
          <groupId>org.apache.shiro</groupId>
          <artifactId>shiro-spring</artifactId>
          <version>${shiro.version}</version>
    </dependency>
    <!-- 引入shiro依赖  END-->
    
**********************************************************
    
    2.新建实体类 +  xxx.hbm.xml映射文件

    3.hibernate配置文件 + hibernate.cfg.xml +  引入数据源

-----------------------------------------------------
        <!-- 显示sql -->
        <property name="show_sql">true</property>
        <!-- 格式化sql -->
        <property name="format_sql">true</property>
------------------------------------------------------

  测试over{

    Configuration configuration=new Configuration().configure();
    SessionFactory sessionFactory=         configuration.buildSessionFactory();
    Session session=sessionFactory.openSession();
    Transaction transaction=session.beginTransaction();
    session.save(new Person("7788",22));
    transaction.commit();
    session.close();
    sessionFactory.close();

   }





第二步:
        整合hibernate+Spring

    一. 新建spring配置文件:

        applicationContext-public.xml:

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

           <!--1. 引入db.properties -->
     <context:property-placeholder location="classpath:db.properties"/>
     
     <!--2. 配置数据源 -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
         <property name="user" value="${uname}"></property>
         <property name="password" value="${upass}"></property>
         <property name="jdbcUrl" value="${url}"></property>
         <property name="driverClass" value="${driverclass}"></property>
         
         
         <property name="initialPoolSize" value="${initPoolSize}"></property>
         <property name="maxPoolSize" value="${maxPoolSize}"></property>
         
     </bean>
     
       <!-- 3.配置sessionFactory -->
       <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
          <!-- 3_1:引入数据源 -->
          <property name="dataSource" ref="dataSource"></property>
          <!-- 3_2:关联hibernate配置文件 -->
          <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
          <!-- 3_3:关联hibernate映射文件 -->
          <property name="mappingLocations" value="classpath:com/entity/*.hbm.xml"></property>
       </bean>  



  2.  sessionFactory 测试 over{
             public void test() {
        //即时加载
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationCentext-public.xml");
        ac.getBean("sessionFactory");
    }

      }
-----------------------------------------------------------------------------




       
     3.  <!-- 配置事务 -->
       <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory"></property>
       </bean>
       
     4.  <!-- 配置事务的属性 -->
       <tx:advice id="myAdvice" transaction-manager="transactionManager">
          <tx:attributes>
              <tx:method name="add*" propagation="REQUIRED"/>
              <tx:method name="update*" propagation="REQUIRED"/>
              <tx:method name="delete*" propagation="REQUIRED"/>
              <tx:method name="*" />
          </tx:attributes>
       </tx:advice>
       
       
      
    5.   写 xxxDao /xxxBiz 接口

        增删改查
        public void addPerson(Person person);
    public void delPerson(int pid);
    public void updatePerson(Person person);
    public List<Person> getPerson();

    6.  写 xxxDaoImpl实现类 implements 接口
------------------------------------------------------------------------
    public class PersonDaoImpl implements PersonDao{

    private SessionFactory sessionFactory;
    
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    public Session getSession() {
        //获取数据库连接池当前事务
        return sessionFactory.getCurrentSession();
    }
        //增加
    public void addPerson(Person person) {
        getSession().save(person);
    }
    
    public void delPerson(int pid) {
        
    }
    
    public void updatePerson(Person person) {
        
    }
    
    public List<Person> getPerson() {
        return null;
    }
    
}
------------------------------------------------------------------------------



        6.  写 xxxDaoImpl实现类 implements 接口
------------------------------------------------------------------------------
          public class PersonBizImpl implements PersonBiz{

    private PersonDao personDao;
    public PersonDao getPersonDao() {
        return personDao;
    }

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    /*****************************************************/
    
    public void addPerson(Person person) {
        personDao.addPerson(person);        
    }
    
    public void delPerson(int pid) {
        // TODO Auto-generated method stub
        
    }
    
    public void updatePerson(Person person) {
        // TODO Auto-generated method stub
        
    }
    
    public List<Person> getPerson() {
        // TODO Auto-generated method stub
        return null;
    }
    
}

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


     7.  <!-- 配置事务的切点 -->
       <aop:config>
           <aop:pointcut expression="execution(* com.dao.*.*(..))" id="myCut"/>
           <aop:advisor advice-ref="myAdvice" pointcut-ref="myCut"/>
       </aop:config>



         applicationContext-dao.xml:  
     
    <!-- 配置PersonDaoImpl-->
          <bean id="PersonDaoImpl" class="com.dao.PersonDaoImpl">
      * //ref引用public中的“sessionFactory”
          <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>



    applicationContext-biz.xml:

      <!-- 配置 PersonBizImpl-->
      <bean id="personBizImpl" class="com.biz.PersonBizImpl">
      * //ref引用Dao中的“PersonDaoImpl”
         <property name="personDao" ref="PersonDaoImpl"></property>
      </bean>
-----------------------------------------------------------------------------



    二.新建一个db.properties文件  (配置c3p0数据库连接池需要的数据)
--------------------------------------------
(同时在 hibernate.cfg.xml 文件中注释此段数据 )
uname=root
upass=123
url=jdbc:mysql://localhost:3306/test
driverclass=com.mysql.jdbc.Driver

initPoolSize=5
maxPoolSize=10

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

    测试{
                   public void test() {
        //即时加载
        ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"applicationContext-public.xml","applicationContext-dao.xml","applicationContext-biz.xml"});
             //getBean中取值来自于 Biz中的“PersonBizImpl”
        PersonBiz personBiz=(PersonBiz) ac.getBean("PersonBizImpl");
        personBiz.addPerson(new Person("廖雅琳", 19));
    }


        }



*********************************************************************************************
      
   第二步:
        整合hibernate+Spring+struts2

      一:新建struts.xml文件
---------------------------------------------------------------------
         <struts>
            <package name="myPackage" extends="struts-default">
               <action name="personAction*" class="personAction" method="{1}">
                  <result name="success">/success.jsp</result>
               </action>
            </package>
       </struts>

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

       二: web.xml
---------------------------------------------------------------------
         <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-*.xml</param-value>
    </context-param>
 
 
  <!-- 加载Struts2配置文件 -->
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
 
  <filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
 
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>*.action</url-pattern>
  </filter-mapping>
 
 
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
 
 
  <!-- 加载spring配置文件 -->
   <!-- needed for ContextLoaderListener -->
    
    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
----------------------------------------------------------------------

     

       二:1.新建Action类:
----------------------------------------------------------------------
      public class PersonAction extends ActionSupport{

    private Person person;
    private PersonBiz personBiz;
    
    public Person getPerson() {
        return person;
    }
    public void setPerson(Person person) {
        this.person = person;
    }
    public PersonBiz getPersonBiz() {
        return personBiz;
    }
    public void setPersonBiz(PersonBiz personBiz) {
        this.personBiz = personBiz;
    }
    //增加
    public String add() throws Exception {
        System.out.println(person.getPname()+"---"+person.getPage());
        personBiz.addPerson(person);
        
        return "success";
    }
}
    

         2. applicationContext-action.xml:

              <bean id="personAction" class="com.action.PersonAction" scope="prototype">
             <property name="personBiz" ref="personBizImpl"></property>
        </bean>
-----------------------------------------------------------------------




原创粉丝点击