TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活、易于扩展的多层Web应用程序。+ t s( g5 z7 `& E/ a
5 o) Y6 Z5 C3 i集成SSH框架的系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层(实体层)。
, y e" i8 b1 ]9 N) I. z6 W" @9 O/ Z& e0 Q
Struts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持。Spring一方面作为一个轻量级的IoC容器,负责查找、定位、创建和管理对象及对象之间的依赖关系,另一方面能使Struts和Hibernate更好地工作。- u# b7 W9 T/ v8 i7 i
; z; f( p$ F4 g5 D项目分层结构:action、 service、dao
9 j5 Z& o# h! k& M- ]- E* V2 r项目配置自定义异常错误实现、重写拦截器对用户进行拦截判断、使用过滤器 统一设置字符集。
+ [2 l( N: ^( b) w3 b9 }" B$ j$ S6 V* ^1 x2 h+ r
功能实现:基本的登陆、失败或者成功的跳转以及登陆成功后用户数据的新增操作。数据库表自建。6 Z$ x, N7 K" r7 o) Z
; c; t( {& @1 j5 s) {7 RapplicationContext-common.xml:
% I! D4 }! S$ d: K1 k% k2 r1 a) ?3 r6 N
- <?xml version="1.0" encoding="UTF-8"?>4 M7 q( C9 Y3 L9 c. ~) u' u, z
- <beans xmlns="http://www.springframework.org/schema/beans"
. c+ H! D- e* m. d' ?9 I& I3 q4 L - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
8 R* y R5 J( Y1 G7 J1 K& H" { - xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
/ p l$ s# V! ^- M2 O# j' y2 O - xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd/ n+ r; L |, B* s& e7 j) i
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
# L) V4 D7 F d6 v - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
1 V; c8 k& H+ s+ h - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
, W, r" C3 {# [" D0 F
1 C, X \3 X$ h. N- <!-- 启用spring注解支持 -->
* z3 @0 J+ I. z+ C' q% l. I - <context:annotation-config />0 U: `4 y9 z; e2 K$ q( v1 z" w4 C" @2 _
- 9 Z1 w+ \" p u, f$ s& T
- <!--配数据源 -->! {3 H6 a2 O0 ?1 N3 }9 l
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
K) ~+ j3 N( {6 |- s8 P5 W - destroy-method="close">
; K4 |6 _% \& e1 a - <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />8 ` Y! Q# t/ U. v; y
- <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
6 @5 v+ w! L$ o, M - <property name="username" value="ssh" />
. }, [/ N: H) y3 W# u$ F8 A - <property name="password" value="ssh" />6 U |" s& b z9 z) i
- </bean>
) p8 a( z0 ~8 g, G0 v2 Q, h1 Y' V
/ e. l) P. w6 H: o: u K. H- <bean id="sessionFactory"2 y6 C$ c9 I, T, G
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">$ R1 i; T, x2 @
- <property name="dataSource" ref="dataSource" />
1 m) C/ L# l$ Q! J/ Q - ' G, V, Q0 X6 B1 c% T
- <property name="hibernateProperties">
P$ s* J& [/ {& p - <props>
0 q: G% B6 J0 `: u: G y9 ` - <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>% x) n' y) f& } [: O: o# R
- <prop key="hibernate.show_sql">true</prop>
; c) ~- @$ L! c& g! y - <prop key="hibernate.hbm2ddl.auto">update</prop>, t' R4 L+ b. ]+ r
- <prop key="default_schema">ssh</prop>+ c' k: W. ?* K6 v
- ( Z# C- M2 \8 F% N# J) L7 L! z
- </props># l2 G- T. B& J5 y
- </property>
9 o5 q" k+ W" s4 ~! K - <!-- 如果使用配置文件 -->) m% ?, Y& Z: l! P3 l
- <!-- <property name="mappingLocations"> <list> <value>classpath:com/jialin/entity/User.hbm.xml</value> 2 T5 X. O' w7 ?' R( U$ n
- </list> </property> -->
) s! @& w7 t4 S2 n - <property name="annotatedClasses">) P" ~! y9 H% Z2 s% S
- <list>
& Z h& E) ~: H$ @9 q6 o - <value>com.jialin.entity.User</value>( V7 V. U9 F4 v% G/ L- K
- </list>
5 ^; u# F, R- u8 I% i - </property>
; {* | Y7 E' o
3 L- x! F5 A* I7 B- </bean>+ r: I; @1 u0 ^8 b* e
- * q& z" N1 ^( ^9 V+ w# K
& U) M/ K! G/ r8 s1 B5 G7 s, o6 r- <!-- 配置事务管理器 -->+ P+ p) i+ B3 l
- <bean id="transactionManager"
$ o& P3 `3 G$ y; u - class="org.springframework.orm.hibernate4.HibernateTransactionManager">( Z6 R+ }% O- g @
- <property name="sessionFactory" ref="sessionFactory" />
4 k6 K# _, k# P, u7 y - </bean>
) {6 d8 C8 D, j% p$ f$ G. A8 J
. ?6 u$ \( ]" d; z% d- <!-- 事务的传播特性 -->/ u* X5 |- q6 E8 D5 p
- <tx:advice id="txadvice" transaction-manager="transactionManager">; ], x7 H2 ~4 G: v4 N5 ?" c
- <tx:attributes>- |2 M! T' O' S9 X: F8 H; z
- <tx:method name="add*" propagation="REQUIRED" />& f3 B1 C1 E, r/ o6 k+ Y
- <tx:method name="delete*" propagation="REQUIRED" />
5 }, @+ }7 n) j9 r - <tx:method name="modify*" propagation="REQUIRED" />. G: s# i8 g: I+ C$ F3 n
- 2 x" M# I5 [" Y
- <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->: T1 H. @- N( [
- <tx:method name="*" propagation="REQUIRED" read-only="true" />
3 B4 T3 F# a4 K- S+ p* U# u; g - </tx:attributes>
8 u/ F, h0 i+ U7 N4 ? - </tx:advice>
% W& i0 N8 a o3 b, y; t( q/ T - + R. K. m) d: I* r6 l
- <!-- 那些类那些方法使用事务 -->
# G* a) U' K& Q, p - <aop:config>% @: v! F- z, t& r5 U
- <!-- 只对业务逻辑层实施事务 -->0 A" l2 ]# M# V
- <aop:pointcut id="allManagerMethod" ^+ h- B. r) i5 q9 E8 [2 P! b3 n. X1 i
- expression="execution(* com.jialin.service.*.*(..))" /># }& e0 z. K3 ]* y9 H
- <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txadvice" />( |" y; j% p6 Z. f) \
- </aop:config>
/ E- R) c% k- q+ v! P
( k- c3 a! e* o* _" ^' v- 9 L1 w/ |4 }4 W4 v: h. c
- </beans>2 j9 T$ L9 K) _0 S* S
复制代码
( P' f. u! X9 ^, S/ }1 }5 p
2 E* J. t. R2 W: h$ X7 W6 Z1 hstruts2+spring+hibernate整合案例代码
/ J" J5 |0 }) W9 ~7 H: N8 K. J5 n- o9 [* @( A
; p. Z: p5 f% H3 p+ r: i( ~: j |
|