手動設定三大架構整合,Spring+Struts2+Hibernate

來源:互聯網
上載者:User

運用三大架構,使用MyEclipse幫我們匯入jar包會發現有些包衝突了或者有些包是多餘的,讓我們來手動設定三大架構吧。

1、下載三大架構的必備的jar包,下載地址:http://download.csdn.net/detail/harderxin/4420066

2、建立一個web工程SSHTest,將jar包匯入到工程中,注意匯入進去後在WEB-INFO目錄的lib檔案夾中要有相應的jar包,jar包如下以及我的目錄結構

 

3、開啟web.xml,在裡面添加如下代碼,配置如下資訊是在web項目啟動的時候可以載入那些spring和struts設定檔中的資訊:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!-- 預設載入: /WEB-INF/applicationContext.xml --></listener><context-param><param-name>contextConfigLocation</param-name><!-- 載入Spring設定檔:applicationContext-*.xml --><param-value>classpath:applicationContext-*.xml</param-value></context-param><!-- 配置Struts2過濾器,表示啟動web項目的時候進行載入 --><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><welcome-file-list>    <welcome-file>index.jsp</welcome-file> </welcome-file-list>  </web-app>

 

4、開啟Struts.xml,配置Struts一些常量,並將struts交給Spring容器來管理

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- 將struts2交給spring管理--><constant name="struts.configuration.xml.reload" value="true"/><constant name="struts.action.extension" value="action,do,webwork" /><constant name="struts.enable.DynamicMethodInvocation" value="true" />    <constant name="struts.devMode" value="true" /><constant name="struts.multipart.maxSize" value="100971520"></constant><constant name="struts.i18n.encoding" value="UTF-8"></constant><constant name="struts.objectFactory.spring.autoWire" value="name"></constant><constant name="struts.objectFactory" value="spring"></constant><constant name="struts.custom.i18n.resources" value="messages"></constant><package name="test" namespace="" extends="struts-default">  <action name="testAction" class="testAction">  <result name="test">index.jsp</result>  </action></package></struts>

  5、開啟Spring,配置串連池和事務

<?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:context="http://www.springframework.org/schema/context"             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-3.0.xsd                     http://www.springframework.org/schema/context                     http://www.springframework.org/schema/context/spring-context-3.0.xsd                     http://www.springframework.org/schema/aop                     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd                     http://www.springframework.org/schema/tx                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><context:annotation-config/><context:component-scan base-package="com.ssh" />  <!--      如果不使用設定檔,可以使用如下方式配置資料來源       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">       <property name="driverClassName" value="com.mysql.jdbc.Driver" />        <property name="url" value="jdbc:mysql://localhost:3306/lundui?useUnicode=true&characterEncoding=utf8" />        <property name="username" value="root" />         <property name="password" value="123456" />        </bean>      -->     <!-- 讀取設定檔 --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:jdbc.properties"/></bean><!--  資料庫連接池 --><bean id="dataSource" destroy-method="close"  class="org.apache.commons.dbcp.BasicDataSource">  <property name="driverClassName" value="${jdbc_driver}" />  <property name="url" value="${jdbc_url}" />  <property name="username" value="${jdbc_user}" />  <property name="password" value="${jdbc_password}" />  </bean>        <bean id="sessionFactory"  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  <property name="dataSource" ref="dataSource" />   <!--   單個實體類配置       <property name="annotatedClasses">              <list>                  <value>com.ssh.common.model.User</value>                 <value>com.ssh.common.model.Log</value>              </list>          </property>          -->   <property name="packagesToScan">  <list>   <value>com.ssh.common.model</value>  </list>  </property>  <property name="hibernateProperties">  <props>   <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  <prop key="hibernate.show_sql">true</prop>  </props>   </property>  </bean>  <!-- 配置交易管理員 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <!-- 配置事務的傳播特性 --> <tx:advice id="txAdvice" transaction-manager="txManager" > <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="saveOrUpdate*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 配置哪些類哪些方法使用事務 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.repair.*.service.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.repair.*.service..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/> </aop:config><!-- JdbcTemplate --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource"><ref bean="dataSource" /></property></bean><bean id="testAction" class="com.ssh.test.action.TestAction" scope="prototype"></bean><bean id="testService" class="com.ssh.test.service.impl.TestServiceImpl"></bean><bean id="testDao" class="com.ssh.test.dao.impl.TestDaoImpl">  <property name="sessionFactory" ref="sessionFactory"/></bean></beans>

jdbc.propertie資料庫設定檔資訊:


6、定義添加相應實體類和類的XXX.hbm.xml檔案對應

package com.ssh.common.model;import java.io.Serializable;public class User implements Serializable {private static final long serialVersionUID = -2699030436204817211L;private Long id;//使用者名稱稱private String username;//使用者密碼private String password;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}

 

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.ssh.common.model"><class name="User" table="tb_user"><id name="id"><generator class="native" /></id><property name="username" column="username"/><property name="password" column="password"/></class></hibernate-mapping>


7、在上面struts和spring中,我們給相應的實體類進行了註冊,下面來編寫我們的類,在這裡,我使用了三層架構,大家可以根據實情進行相應的配置和處理,裡面的方法可以參考我配置好的項目包
8、將項目部署到Tomcat伺服器,如果啟動過程中沒有報錯,說明ssh三大架構配置OK。大家試一試吧

我自己配置好了一個三大架構的demo項目,大家可以在我的資產庫中進行免費下載學習:

注意:使用資料庫:mysql,在本地建立一個名稱為lundui的資料庫,然後在裡面建立一張tb_user的表,裡麵包含資訊:id、username、password,也可以進行jdbc自己配置

Demo下載地址:http://download.csdn.net/detail/harderxin/7054831

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.