初學者易上手的SSH-整合

来源:http://www.cnblogs.com/lzx2509254166/archive/2017/12/05/7989881.html
-Advertisement-
Play Games

許久沒更新博客了! spring還有一章aop(面向切麵),我就沒講述了,你們可以去看下代理模式。 那麼我們開始整合:struts2 2.3.4 ,hibernate 5.2.10 ,spring 4.3.10 ,一直以來用到的xml式,那麼整合也不例外,就是有些麻煩。另外註解式想瞭解請留言(雖然s ...


許久沒更新博客了!

spring還有一章aop(面向切麵),我就沒講述了,你們可以去看下代理模式。

那麼我們開始整合:struts2  2.3.4 ,hibernate 5.2.10 ,spring 4.3.10 ,一直以來用到的xml式,那麼整合也不例外,就是有些麻煩。另外註解式想瞭解請留言(雖然ssh已過時)。

首先建立一個maven項目,導入以下依賴:

    <!-- javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.10.Final</version>
        </dependency>

        <!-- spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>

        <!-- spring-orm -->
        <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-core -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.24</version>
        </dependency>

        <!-- struts2-spring-plugin -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.24</version>
        </dependency>

        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.41</version>
        </dependency>

        <!-- c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

由於整個用xml文件,所以分開配置,比較容易理解:

在resources文件夾下建立以下xml

1.applicationContext-public.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 配置資料庫連接池 c3p0 -->
    <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="root"></property>
        <property name="password" value="sasa"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="20"></property>
        
    </bean>

    <!-- 配置sessionFactory -->
    <bean id="sessionFactoryBean"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="datasource"></property>
<!--可以不用--> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <property name="mappingLocations" value="classpath:com/entity/*.hbm.xml"></property> </bean> <!-- 配置事務 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactoryBean"></property> </bean> <!-- 配置事務屬性 --> <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> <!-- 配置事務切點 --> <aop:config> <aop:pointcut expression="execution(* com.dao.*.*(..))" id="myPointcut" /> <aop:advisor advice-ref="myAdvice" pointcut-ref="myPointcut" /> </aop:config> </beans>

 建立basedao,用戶dao層獲取seesion來獲取數據

package com.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class BaseDao {

	private static SessionFactory sessionFactory;

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static void setSessionFactory(SessionFactory sessionFactory) {
		BaseDao.sessionFactory = sessionFactory;
	}

	public static Session getSession() {
		return sessionFactory.getCurrentSession();

	}

}

  建立dao類繼承basedao

package com.dao.impl;

import java.util.List;

import com.dao.IMenuDao;
import com.entity.Menu;

public class MenuDao extends BaseDao implements IMenuDao {

    
    public List<Menu> getAll(String rid, String title) {
    //引用basedao getSession()方法
getSession().createQuery(sql) .....
      return null;
}
}

 

2.applicationContext-dao.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<!--此為公共類 獲取session--> <bean id="baseDao" class="com.dao.impl.BaseDao">
<!--name為basedao中的引用對象名 其實是set方法名 因為對象的set方法一般與對象名一致 ref為applicationContext-public.xml中配置sessionFactory 的id-->
<property name="sessionFactory" ref="sessionFactoryBean"></property> </bean>

<!--id 自己寫 class為類全路徑 parent意為父類,即繼承了上面的basedao-->

</bean>

<bean id="menuDao" class="com.dao.impl.MenuDao" parent="baseDao">
</bean>



</beans>

建立biz類引用dao類

 

package com.biz.impl;

import java.util.List;

import com.biz.IMenuBiz;
import com.dao.IMenuDao;
import com.entity.Menu;

public class MenuBiz implements IMenuBiz {

//引用dao 提供getter and setter
private IMenuDao iMenuDao; public IMenuDao getiMenuDao() { return iMenuDao; } public void setiMenuDao(IMenuDao iMenuDao) { this.iMenuDao = iMenuDao; } public List<Menu> getAll(String rid,String title) { return iMenuDao.getAll(rid,title); } }

 

3.applicationContext-biz.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

  

<bean id="menuBiz" class="com.biz.impl.MenuBiz">

<!--name biz類中引用對象名  ref為applicationContext-dao.xml中對象dao的id-->

<property name="iMenuDao" ref="menuDao"></property>
</bean>

</beans>

建立Action類引用biz

package com.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.biz.IMenuBiz;
import com.entity.Menu;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import net.sf.json.JSONArray;

public class MenuAction extends ActionSupport implements ModelDriven<Menu> {

    private static final long serialVersionUID = 1L;

    private Menu menu = new Menu();


//引用的biz
private IMenuBiz iMenuBiz; public IMenuBiz getiMenuBiz() { return iMenuBiz; } public void setiMenuBiz(IMenuBiz iMenuBiz) { this.iMenuBiz = iMenuBiz; } public Menu getModel() { return menu; } }

4.建立applicationContext-action.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    
    
    <bean id="menuAction" class="com.action.MenuAction" scope="prototype">
//配置與dao,biz意思一樣 <property name="iMenuBiz" ref="menuBiz"></property> </bean> </beans>

最後建立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>
    <package name="mypackage" extends="struts-default">
        <!--calss為applicationContext-action.xml對應的id-->    
        <action name="menu*" class="menuAction" method="{1}">
            <result name="success">/index.jsp</result>
            <result name="error">/login.jsp</result>
        </action>
    </package>
</struts>

web.xml配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

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

    <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>*.action</url-pattern>
    </filter-mapping>

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


</web-app>

整個下來很容易配錯,要細心。關於在其中的許多解釋就沒說了,附圖一張:  ssh完


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 面完成了ssm的整合, 整個過程可以說很繁雜, 各種配置, 很容易讓人暈掉. 這裡使用spring boot 的方式來實現 ssm(1) 中的功能. 一. 建項目 1. 使用 idea 來創建 spring boot 項目, 是比較簡單的, 如果使用eclipse的話, 要裝插件, 比較麻煩, 那這 ...
  • 看到這樣的問題,五年後我們能幹啥,其實說吧簡單一些就是對這個行業的一個規劃,或者是對自己的一個未來的規劃,規劃對於個人或者企業都是一個未雨綢繆的一個方式,那我就說說我的設計這些年在做什麼。 ...
  • ...
  • qt學習教程1.qt開發環境搭建 首先下載qt 下載地址:http://download.qt.io/archive/qt/ 此教程使用的版本為5.1.1 下載好後,打開安裝包,然後點下一步 選擇一個位置來安裝qt,系統盤不夠用的就裝在其他盤 選擇同意 然後接下來一路確定下一步。就安裝好了。 第一個 ...
  • 凡是可以由自己命名的地方都稱為修飾符. 例: 項目名 ,包名 ,類名 .方法名 2. 命名規範. ① 不可使用java關鍵字和保留字,但是可以包含關鍵字和保留字. ② 可以使用26個字母大小寫,數字0-9,$和_. ③ 可以使用數字,但不可放在首位. ④ 長度理論上沒有限制,但命名最好能反映出其作用 ...
  • 1 s = input("輸入:") 2 result = '' 3 for i in range(len(s)): 4 result += chr(ord(s[i])^2000) 5 print(result) ord(char) #把字元轉換成unicode編碼(整型) chr(unicode) ...
  • inspect模塊用於收集python對象的信息,可以獲取類或函數的參數的信息,源碼,解析堆棧,對對象進行類型檢查等等,有幾個好用的方法: getargspec(func) 返回一個命名元組ArgSpect(args, varargs, keywords, defaults),args是函數位置參數 ...
  • python數據轉換json 將json轉換為pathon數據 repr 和 eval用法 json讀取和寫入 總結: 數據轉換 第一步: 引入json包: import json 第二步: 使用 json.dumps(pythonObj) 把python數據轉換json數據 第三步: 使用json ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...