TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活、易于扩展的多层Web应用程序。: Q: N% I$ Z( i. E: S7 ^
1 N0 `: y- J% S: M
集成SSH框架的系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层(实体层)。
/ H h3 I+ b3 `7 T, h& s
2 l4 d9 p3 x u& B2 M8 wStruts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持。Spring一方面作为一个轻量级的IoC容器,负责查找、定位、创建和管理对象及对象之间的依赖关系,另一方面能使Struts和Hibernate更好地工作。
6 _' ^" J4 T8 t ^2 X( n$ y1 e; h5 t) l1 Z0 R
项目分层结构:action、 service、dao
0 z9 T$ ]/ {' R项目配置自定义异常错误实现、重写拦截器对用户进行拦截判断、使用过滤器 统一设置字符集。
) U' a v0 ?1 ]4 F) i) E, g
) V( c# p) B5 V( @& Y3 `" {5 }功能实现:基本的登陆、失败或者成功的跳转以及登陆成功后用户数据的新增操作。数据库表自建。
8 V. j+ T! m( ]6 e [3 C& ^$ D+ W9 f2 l5 m& o
applicationContext-common.xml:
. {+ Y4 [+ o- @. e: v! G* s
8 {2 r3 D' g! Q7 x- <?xml version="1.0" encoding="UTF-8"?>/ v N$ h- |6 V( B7 G( {: s! z& |& l
- <beans xmlns="http://www.springframework.org/schema/beans"; Q; `; z$ W- Z) h; `. L; P
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
% m# `5 d- N) c# T* ~ \ - xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
# T* F8 A% V' f+ n$ Y) p - xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd% _ B9 x; s* [8 m
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd! d+ |; t/ p! N, Q. u: v; S2 b
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd# n1 i m7 H8 a9 A/ s
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">: w1 Q& `. A2 Z2 A1 M
- # B6 F( B1 ^ L. Q
- <!-- 启用spring注解支持 -->( I& L( o9 D B1 j/ m
- <context:annotation-config />) p9 _$ G, d; _" X8 O# ?7 f
- 6 I, \& t4 y t1 b: z
- <!--配数据源 -->) j( k' q2 N# O% N7 X
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"/ x& E( `! n, l M" J
- destroy-method="close">
* ^/ t- R Y+ ?# H- c8 b& B - <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
/ z7 l9 m0 h) i3 [! g0 w8 J6 R - <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
5 L4 v8 x' l+ { - <property name="username" value="ssh" />- K; _5 Z5 F% q% u( l. N4 b
- <property name="password" value="ssh" />
' Y# _; K/ p" P& O& ^4 n% d - </bean>
& w4 h" y" d$ G, V* B
$ |) X; M. w* ], }) A- <bean id="sessionFactory"" J9 \5 Y! S! e6 \8 i8 r
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
L4 ?5 @6 H/ \1 F2 M& ^' X8 X - <property name="dataSource" ref="dataSource" /># A6 m3 B* ^! h9 L/ p
- * x5 W0 j, o$ ?8 T. b# L
- <property name="hibernateProperties">
1 b" ?5 V! I7 W @ - <props>
. d* I' B) V8 V) B3 |; p: a/ v( K - <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
0 H7 _7 c$ T) E7 u& Z; Q3 H - <prop key="hibernate.show_sql">true</prop>
) ~* H) x2 A( i - <prop key="hibernate.hbm2ddl.auto">update</prop>4 j% p( @: i4 @- X9 M3 a! y
- <prop key="default_schema">ssh</prop>
( D: g' t" | G( o8 d - ; Q- I8 D g6 Q# o* ^
- </props>9 X2 g2 @: q: S. ?3 U
- </property>
9 {9 k* C9 X, F9 a- U/ ]) U; v/ U+ o - <!-- 如果使用配置文件 -->
O! k, T, u3 H" g8 _" [& }2 M - <!-- <property name="mappingLocations"> <list> <value>classpath:com/jialin/entity/User.hbm.xml</value> ) ]) }, ?* m' I+ U1 X$ n* l# s% l. _
- </list> </property> -->" }9 P& f+ M# j' l5 \/ Q' {# A
- <property name="annotatedClasses">2 x9 c% y9 i: Z6 L
- <list>. k$ G/ ?7 {& [0 `; c2 o
- <value>com.jialin.entity.User</value>0 D6 J7 N1 S8 ~$ [" e: P
- </list>/ d ]# W3 [; p8 B6 ?2 T# f6 j7 u
- </property>
# j! T' W; v( g h' l$ B - ( Y# f) `1 U3 ?" x! D+ J0 z
- </bean>6 Y `* P- |2 t+ H8 U
- ; M# r m3 I' ?6 r5 x; ^
- ) x& O. e8 `- G4 {$ U
- <!-- 配置事务管理器 -->3 ?' {; E: l% N
- <bean id="transactionManager"
8 U/ @8 y- p p! I - class="org.springframework.orm.hibernate4.HibernateTransactionManager">
. x u9 o" v0 Y' ?% v9 O - <property name="sessionFactory" ref="sessionFactory" />& F+ [) T" h& Z% L W7 r0 X
- </bean>0 e+ m# d O) l' j# g' ^ N9 q
- 0 }; N1 u. I5 ?2 E
- <!-- 事务的传播特性 --> Z& n9 v9 E, a( Z, }
- <tx:advice id="txadvice" transaction-manager="transactionManager">
( [- ^ K" s3 _8 `: R# W8 F - <tx:attributes>
& f& W& z# h) m$ C f( b - <tx:method name="add*" propagation="REQUIRED" />5 R0 v4 P1 u) a) N6 n4 C$ [
- <tx:method name="delete*" propagation="REQUIRED" />
9 y! f& X h! \1 g' Y+ q4 K9 s - <tx:method name="modify*" propagation="REQUIRED" />
, Y# l. L; G0 I& W, e& D -
& e1 m' @& ^$ Z, w2 a n - <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->
' s- }2 b6 L% a2 p - <tx:method name="*" propagation="REQUIRED" read-only="true" />7 t2 I4 C7 h* g
- </tx:attributes>
. W0 e, H/ x+ q- z - </tx:advice>
% b- \# O( G5 p. S9 ]
+ R `( i- t8 E0 N8 G- <!-- 那些类那些方法使用事务 -->$ s6 f b( R( u/ ^4 b% s
- <aop:config>
+ s' V2 V6 ?5 n8 a W% R - <!-- 只对业务逻辑层实施事务 -->, k( Q: r' A+ y1 B$ O; E/ r0 m
- <aop:pointcut id="allManagerMethod"- y5 Q8 X9 t$ i/ o5 ?4 t
- expression="execution(* com.jialin.service.*.*(..))" />& h3 s; ]6 s5 b% B5 J, K
- <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txadvice" />5 R; Y& B" I9 A3 H4 d: y
- </aop:config>- a: R% W2 m$ l( ^
- , }/ h8 F; r5 F( ~: U/ m: @
- 0 L: j Q5 m0 a
- </beans>
3 I6 Z7 U) H' a6 O( V! g K+ ] a
复制代码
) ~0 H6 ?7 m. M3 Q/ B4 ?4 F/ ?. x
: @ z3 e+ R8 A' R6 @- nstruts2+spring+hibernate整合案例代码" ?5 @' z1 ~0 d
# E1 N' P' K6 l( G( ~7 J7 u* a0 {0 A h5 s3 S
|
|