TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活、易于扩展的多层Web应用程序。. Y3 F! y. r; y L. @' z
* D( p4 }7 L7 x1 R& j集成SSH框架的系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层(实体层)。
, n& v) A% V @0 {* }
. n9 c# |/ [) x+ z. _! E/ @Struts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持。Spring一方面作为一个轻量级的IoC容器,负责查找、定位、创建和管理对象及对象之间的依赖关系,另一方面能使Struts和Hibernate更好地工作。" {& g4 z8 Z" j/ k
9 Q7 L2 [) P, i) V. ^7 T! ?8 k
项目分层结构:action、 service、dao
+ C" ]) N! S" q& S7 n项目配置自定义异常错误实现、重写拦截器对用户进行拦截判断、使用过滤器 统一设置字符集。1 V7 Y6 B3 e8 r/ }& Z @3 C
6 D, v9 `- f/ H8 v3 o2 ~; \; Z
功能实现:基本的登陆、失败或者成功的跳转以及登陆成功后用户数据的新增操作。数据库表自建。6 K) i; y8 G, v5 j/ u" D
4 u. @. y- |) N e5 s9 j* Y, Q& papplicationContext-common.xml:
. O- G% _2 w4 O' v4 y+ S$ g
5 [* b/ N( S2 P- B; X7 i- <?xml version="1.0" encoding="UTF-8"?>- [+ ?. e) ^6 y, u7 R. h
- <beans xmlns="http://www.springframework.org/schema/beans", Q) D# D( R+ I4 y5 m+ k
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
3 ?2 d) k1 k! x8 U - xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"# E' Z1 Y ~2 y
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd# S, V5 z! _# X% t/ \
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
8 s) I; t( f* T8 N3 R - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
0 g, f# t! Q& @$ q - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
. ^4 l7 C, l0 w+ l" }
0 S1 ^. }4 L6 C" l$ q1 h. o- <!-- 启用spring注解支持 -->) ]7 q8 Y( b' J! k8 V$ V
- <context:annotation-config />; O+ Y4 m8 U6 k% U
. }$ ~$ T- ^ ?( w- <!--配数据源 -->
) R3 r- x' i4 d - <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
$ X* G, h1 ?% i" Z - destroy-method="close">
. P6 _" k- A z3 d - <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
5 n+ d& y4 f# r9 a - <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />/ G! ]6 c; U# R
- <property name="username" value="ssh" />4 e, p6 f" _7 p4 P
- <property name="password" value="ssh" />
3 y+ ]1 g# ?' R9 ~- p0 L - </bean>6 e% g. e4 `3 H# K2 L
" Q5 s9 ?2 o8 y* e) e5 n- <bean id="sessionFactory"0 q: c+ u- [+ A# T% b7 w
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> V3 H" W7 e9 n1 A9 e* S
- <property name="dataSource" ref="dataSource" />& a) l% E+ s# ?+ _( F
- 4 q/ \3 l; W3 `5 n: D
- <property name="hibernateProperties">
& }+ X; n' Y1 M8 H7 e - <props>
& f% h3 y& ^8 _4 _ - <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>& k" K9 ~5 Z X7 _: F" l, {
- <prop key="hibernate.show_sql">true</prop>- o" [6 R$ G1 I- H
- <prop key="hibernate.hbm2ddl.auto">update</prop>
e0 Z, I0 n7 ? K5 R, q8 f% ~ - <prop key="default_schema">ssh</prop>
6 ^, [* M5 h& J" v: w* x7 K
\4 V: `3 A- K, K* I2 M) g- </props>2 ?) P4 ]! M, n. t. Q: `4 O: M
- </property>
7 A& ]. |& B* X - <!-- 如果使用配置文件 -->0 c1 S2 T3 {- y J h: Z
- <!-- <property name="mappingLocations"> <list> <value>classpath:com/jialin/entity/User.hbm.xml</value>
, V3 y1 U! @6 A3 l0 l - </list> </property> -->
5 d- v% M% V1 K! E- o8 v; ^! K4 i8 ~9 q - <property name="annotatedClasses">
6 v) n, E* D5 o ? - <list>
7 L6 U/ G3 G3 g - <value>com.jialin.entity.User</value>: m8 Z* h- O% i7 q( t7 A# ^
- </list>
0 y, p8 b: K% w( ~ - </property>
9 S. v. u# W0 U5 v
( \1 ~2 G3 O) G6 f1 e% w- </bean>
) v* a {" g+ J -
) y5 x5 o: _/ q - # m2 M: a6 E- k; ]2 G# ^
- <!-- 配置事务管理器 -->3 d- `) O/ T$ b2 }2 H3 j8 l3 f
- <bean id="transactionManager"3 @; E/ e/ q0 p) @% u# D; H! e
- class="org.springframework.orm.hibernate4.HibernateTransactionManager">( |4 h3 ~6 n1 R6 W
- <property name="sessionFactory" ref="sessionFactory" />. r1 ]8 Q+ K% @/ q4 g
- </bean>- N& \$ x' K5 S1 N
- 8 R$ L# o7 o: R' V' L5 Q. y
- <!-- 事务的传播特性 -->. |! h% l3 Z9 z$ Y7 U
- <tx:advice id="txadvice" transaction-manager="transactionManager">) N9 @* W1 H- n* s7 N. n
- <tx:attributes>
3 M& L9 {; x: W# a- i7 O$ L$ W - <tx:method name="add*" propagation="REQUIRED" />- S2 z* g8 u' q' ]& \2 ~
- <tx:method name="delete*" propagation="REQUIRED" />0 W' @* d: h. g9 I
- <tx:method name="modify*" propagation="REQUIRED" />7 Q1 K7 I& C1 o
- 9 p# b; I' l3 @$ F: D. u
- <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->' r) e* a& g5 _5 b8 N
- <tx:method name="*" propagation="REQUIRED" read-only="true" />! U" V6 {: r5 D: y' V
- </tx:attributes>
3 B: ], O) R: }5 ]! P2 S5 t - </tx:advice>6 f# v0 t+ l% r- v9 A
$ ] I$ {2 w, @8 U$ \# @( ~; k$ [ {- <!-- 那些类那些方法使用事务 -->
9 _$ y2 ~; X2 [; e7 e" s# Z8 | - <aop:config>
% x% c5 @" G5 j - <!-- 只对业务逻辑层实施事务 -->* O+ E5 i1 z' y4 D
- <aop:pointcut id="allManagerMethod"" C9 f9 a J8 p2 `! ]
- expression="execution(* com.jialin.service.*.*(..))" />( h' W/ u# W0 ~& e
- <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txadvice" />3 z: P, ]# O% U- ?8 T. ^
- </aop:config>
: j. m, @- z0 q0 }/ i. q2 q
9 P7 e% Y1 T! z/ J9 {$ h
3 n5 t: ?0 b% q1 P- </beans>2 q) y \- x( y3 D$ j0 @
复制代码
Y% D4 p g5 v5 ^7 p1 y: X( B7 w! h( y' L5 \
struts2+spring+hibernate整合案例代码+ C& w1 n6 b8 T+ l: \
+ y! `* M8 b: Q/ G5 l) O+ H
, ]9 y0 C' F$ ]! I
|
|