TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活、易于扩展的多层Web应用程序。; a0 V3 L0 X$ I) e! _5 f
8 F0 }5 V8 B' }" U3 ]" k# {4 ?; e集成SSH框架的系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层(实体层)。
+ x1 i! T* Z; l
8 u/ _' S0 l6 Z7 F* P. u, xStruts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持。Spring一方面作为一个轻量级的IoC容器,负责查找、定位、创建和管理对象及对象之间的依赖关系,另一方面能使Struts和Hibernate更好地工作。
. |$ t6 d+ n# ?0 V8 K2 V* @+ c
6 o- i; L4 Q! C' `0 G项目分层结构:action、 service、dao
# h1 x6 L; I' ^. t5 `+ s项目配置自定义异常错误实现、重写拦截器对用户进行拦截判断、使用过滤器 统一设置字符集。
. A' [' f0 A& m/ B, E: |! J1 \- W- g) p' T3 v
功能实现:基本的登陆、失败或者成功的跳转以及登陆成功后用户数据的新增操作。数据库表自建。) X# w2 s7 X' U- |
" \* R, }6 Z* N! FapplicationContext-common.xml:2 j, Z* W# Z$ O
7 F8 _$ R1 P8 g: ?$ H6 M
- <?xml version="1.0" encoding="UTF-8"?>- Z/ n t( n, ]* p; M* X
- <beans xmlns="http://www.springframework.org/schema/beans"/ l) i6 P& i5 _: _0 _" F- g4 U
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" G& s- n: c; A: b
- xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
' V& M# M5 F/ S! a$ Q( v - xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- S% k4 H3 y, L, Q# G; j - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd4 Z$ Z! K& ^4 |$ B7 q$ g) x
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
! @/ ]" h1 T4 w6 ~7 \) G - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
2 @ G' Y. u3 c! f- t* q Y& P
! J, J5 c, Y0 @ d% Q; O: Z) q- <!-- 启用spring注解支持 -->
: k8 @) S% [" ^2 \" q4 @9 }0 E - <context:annotation-config />
1 S8 L) x/ G- J0 Z4 p
2 E, |. f) @& b- <!--配数据源 -->
( Q7 l2 q: W8 Q# n - <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource". l9 m7 G, z8 t4 i2 u1 ], ?! z
- destroy-method="close">
7 I N) `- Z/ v% Z" R - <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
+ j8 ^- e. ^" f) _' _! Q1 c - <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
6 m' y' O8 H% _' Z+ i' {1 B( }$ I - <property name="username" value="ssh" />
{; O L# C% X, _8 R - <property name="password" value="ssh" />3 u! w' H; [4 d$ a* m- f+ ?
- </bean>
. j+ t2 M7 c) J! B7 I. _3 P+ C - & z; A# V1 E2 b/ V
- <bean id="sessionFactory"$ A' q' n3 q, X/ z" F0 d
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">: q& N2 j3 a7 ~1 J6 V3 @
- <property name="dataSource" ref="dataSource" />
! t, I5 R$ x5 j0 A& @ z' U
. m5 u7 P3 Q7 n& a4 C- <property name="hibernateProperties">
+ N/ m7 h% E# F% s) H7 X" G& b - <props>! V5 @+ n% P0 {& d% A& i
- <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>* a' C8 ]/ ? `
- <prop key="hibernate.show_sql">true</prop>
9 ?* d1 w; [2 k- e - <prop key="hibernate.hbm2ddl.auto">update</prop>" X$ G3 e: Z; S2 _/ D; Q5 B
- <prop key="default_schema">ssh</prop>
4 ?% y6 I: t6 o6 y1 I' Z - 5 Q' T! [; n6 c) z% u" L5 b8 u/ r
- </props>
& ]+ }8 K' W6 u# r - </property>
) I' o$ j. w: V( K - <!-- 如果使用配置文件 -->
- V% V' a* }( O# F - <!-- <property name="mappingLocations"> <list> <value>classpath:com/jialin/entity/User.hbm.xml</value> 1 ^ I4 W3 i8 \4 T Z! n
- </list> </property> -->
% v. X1 F: w1 p+ k - <property name="annotatedClasses">) _* r3 N& a6 X0 y8 Z6 q
- <list>
9 P/ K0 S9 S) j& g* r# c: K - <value>com.jialin.entity.User</value>
/ k# y7 ~) y7 z% t2 X6 A - </list>
2 X6 G b# X6 q1 R. r2 J3 j5 Z$ I - </property>
+ E/ G/ r/ U/ f" S; O1 [4 N - 6 x; p# t' ^1 S, u( m
- </bean>: i# |% U6 Q* x1 n4 D
-
' Q0 g' \, R* R
- S3 A- I: ?0 [2 l$ i8 X' D! q- <!-- 配置事务管理器 -->3 V3 ?$ R; ^. h2 c" B, A
- <bean id="transactionManager"
) v) C# c; X9 Z - class="org.springframework.orm.hibernate4.HibernateTransactionManager">
S1 i- X% _, N - <property name="sessionFactory" ref="sessionFactory" />
9 n3 {5 K8 ?* z - </bean>
e7 f0 q/ d3 Z/ ^; U; j
+ ^' q5 B; M% A- <!-- 事务的传播特性 -->2 r. j; w% f7 R& |" W1 T
- <tx:advice id="txadvice" transaction-manager="transactionManager">- ^ d' t; W g) V
- <tx:attributes>
5 v" h4 S, ]6 y+ f. [& B2 e - <tx:method name="add*" propagation="REQUIRED" />7 s: t# C2 O+ L4 y# P( w# q
- <tx:method name="delete*" propagation="REQUIRED" />3 Z! D5 ^( q1 c% n) J) n
- <tx:method name="modify*" propagation="REQUIRED" />
3 u, ]7 s( |2 f& p7 b* T - * n- Y9 f+ j& I) O5 |$ x
- <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->
! @# ]9 S- T& w' q/ z- t - <tx:method name="*" propagation="REQUIRED" read-only="true" />; A Q6 C1 t5 H2 r2 C6 H: Z
- </tx:attributes>
/ a% N: `; L5 D9 j* K# J - </tx:advice>' m3 [; A2 o0 @& N! o; I
- : @) F* N) e; T5 C
- <!-- 那些类那些方法使用事务 -->6 Q3 }, ?. O0 n
- <aop:config>
$ x6 ^* k# z# A* m - <!-- 只对业务逻辑层实施事务 --> Y( ~. z1 x3 F5 ~* ]8 Y* S- p! g5 f
- <aop:pointcut id="allManagerMethod"5 }7 a* O0 n5 `# v9 M$ ^
- expression="execution(* com.jialin.service.*.*(..))" />
# _$ Q9 W( R7 i2 D1 { - <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txadvice" />
@) k2 |/ x/ o, V5 s# ~" l% C - </aop:config>
4 [$ m' Q" E2 d: k, Z! \0 @, {7 Y - . M( `" r3 Q3 U/ u
- x, O0 V0 v& o- R) j- \
- </beans>' C* U5 v" E4 I! |5 a& C. k3 X' D
复制代码
, @2 z7 O, @5 c; t& c/ p' M0 N; g+ G& E5 a* z
struts2+spring+hibernate整合案例代码
8 Q5 \$ N& x5 w9 m2 K4 f: r7 k9 Y$ X1 x$ M7 R
9 E- i1 Z% x' B- k |
|