TA的每日心情![](source/plugin/dsu_paulsign/img/emot/shuai.gif) | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
springMVC+hibernate4.3+Spring4.1整合案例spring.xml:* a$ g$ W! ~( O2 U) } J4 N% D* O& w
# j Z- J1 _3 C, j
- <?xml version="1.0" encoding="UTF-8"?>
4 Q: L6 E) n" [: S- o' D n8 U - <beans xmlns="http://www.springframework.org/schema/beans"
$ I* a* [' ] B( o. Q - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 g$ q( S7 E1 w4 \' h - xmlns:p="http://www.springframework.org/schema/p"
7 u0 U! u) _% x6 T3 K - xmlns:aop="http://www.springframework.org/schema/aop"
' p* P/ T) }* K1 f. e - xmlns:context="http://www.springframework.org/schema/context"
8 g! L+ ]9 w( N; _7 K& o - xmlns:jee="http://www.springframework.org/schema/jee"
3 k% i+ ?5 |* m, n) i' a- P - xmlns:tx="http://www.springframework.org/schema/tx" $ g' ~6 G# ~4 P' k
- xsi:schemaLocation="
8 ?- B9 [$ R3 ]% _0 { - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 6 ~! @8 q0 t8 X+ u" `0 H1 ^; S
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd ' V1 Q0 C3 h- W* A( K) d
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
# f. L+ F; k4 @1 _ - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
( ?* x; G/ I ~0 J - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> ; Q& E) I/ l ~, C+ ]
-
2 ~$ P# u2 k( {& Y' Z& Z+ ` - <!-- <mvc:annotation-driven /> -->
8 S+ A( S- B0 Y; Y2 d. ]) Y( l" L- f - <!-- 【配置视图解析器】 --> ) o" _1 u, M7 G8 I! R) B
- <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->
. G& O/ n- m, @$ S# `0 [: ?5 A( r - <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->
$ W5 T I6 E# p - <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 2 E9 G2 _8 b( G5 i2 |. {" R) D
- <property name="prefix" value="/WEB-INF/jsp/"/> ' F6 U$ D; B' [4 Y% c: N
- <property name="suffix" value=".jsp"/>' P! U1 {( ?3 W+ ^
- </bean> -->( d# c6 a7 @) q6 V
-
% z" w6 |8 q* c' W2 ~% I: Q4 L+ x% j1 i - <!-- 导入springMvc.xml配置文件 -->! ~9 S8 s2 ~' O |
- <import resource="classpath:config/spring-mvc.xml" />
" F8 A7 ^$ T) |5 y& W' M/ U2 A - <!-- 加载数据库配置 -->! U: _- r# f- H* q9 t! w4 d
- <context:property-placeholder location="classpath:config/db.properties" />
8 y1 j8 G2 [/ c - <!-- 加载数据源 -->
4 _1 [- X' C% w+ B3 ?% ?( M - <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">$ V; I4 R1 e* ]2 a* ]9 S1 b
- <property name="driverClassName" value="${db.driverClassName}" />
* ?3 A8 a. H$ o! W- T. L, g - <property name="url" value="${db.url}" />, A4 C) [) V/ u$ @ K
- <property name="username" value="${db.username}" />
, S/ |7 p1 i% Y8 [+ J, v - <property name="password" value="${db.password}" />' n. y' X# Q+ K: ~9 b+ l
- <property name="maxActive" value="${db.maxActive}" />
" D2 \" ~+ ^& r4 ]; I - <property name="maxIdle" value="${db.maxIdle}" />7 B8 s3 X$ ]' V, {: R( ?
- <property name="minIdle" value="${db.minIdle}" />
/ T+ p( n4 L- S) d0 K3 f* x - <property name="maxWait" value="${db.maxWait}" /> v) ^6 h. L4 C7 x1 S# x
- </bean>
1 l; G( ?2 |0 m5 g9 ]% E4 _+ a, ~4 c - 4 R: }: z. I$ ]2 e8 Z F
- 5 a, L( L, {$ g3 G4 b
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> $ @) \% H$ ^+ N( ], q
- <property name="dataSource" ref="dataSource" />
& H: i E2 R8 [3 o - <property name="mappingDirectoryLocations"> : L- Q$ s' Z! o. M5 }* {" e( |
- <list>
+ N8 P c; I4 E8 S; Y - <value>classpath:config/hbm/</value>
' ] m. w+ x% h$ V - </list>
9 t$ N% r/ k# U( G- f - </property>
+ s8 `2 B- V b8 E; P- m% X) I: o - <property name="hibernateProperties">
1 Y* g* [& Q1 ^) [) { - <props>
7 B7 i. X8 l% \& H* S1 R - <prop key="hibernate.dialect">${hibernate.dialect}</prop>
' X5 V; \/ W: E' H) U2 `# L - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
$ {3 q. x$ z( `4 }, l* S - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
5 v2 |% Q! n2 @; z' l, A* d" X- Z' \ - <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->
2 ~- ?. i# j1 m - </props> ; f, j9 |- B* k1 d5 o6 n* L( r
- </property>
3 p7 \0 g+ p9 S" a" y& v - </bean>$ F9 d0 m9 ?: e$ E7 B1 j5 Q! Y
- ! x( i& R. t( R, Y) P
- <!-- 声明式事务 --> 4 _. _+ X, z- ?* h y
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- V. T/ e+ J* [8 o3 x8 X - <property name="sessionFactory" ref="sessionFactory" />
" @0 l4 w1 ^4 ^) U. E# }# o - </bean> ( w/ k5 n- v v2 U- S6 i& Q
-
* Q0 v# Q5 B" f' Z. I# y - <aop:config> % v7 I5 A) P0 n( _( s( z
- <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />
# A6 u' F/ C4 z5 c, n# T) W - <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
' k4 O8 X2 s5 A$ z b# l. E - </aop:config> / @: K+ _6 J9 }* ]6 M2 H6 H: a
-
8 s! I% Z1 I1 X! H. `9 u* q- P/ b - <tx:advice id="txAdvice" transaction-manager="transactionManager">
5 u% G; R) s5 a! I) v# q9 e - <tx:attributes>
1 j l5 y9 K/ q- x) H) o2 I - <tx:method name="save*" propagation="REQUIRED" />
9 M( S. Q/ d# N; H `$ b - <tx:method name="add*" propagation="REQUIRED" />
8 `7 z1 ] e- }0 _! g - <tx:method name="create*" propagation="REQUIRED" />6 n5 Y7 U: w$ ]% Z" V, V
- <tx:method name="insert*" propagation="REQUIRED" />
! J9 X& N7 u5 S1 Z - <tx:method name="update*" propagation="REQUIRED" />
: ?* F1 c; u! p$ V$ }9 X9 w - <tx:method name="merge*" propagation="REQUIRED" />
: f. G1 |, e. c - <tx:method name="del*" propagation="REQUIRED" />
, q1 Y8 t* w% t) f - <tx:method name="remove*" propagation="REQUIRED" />
# @8 v6 u; g) j% k4 l - <tx:method name="put*" propagation="REQUIRED" />6 W5 y2 k( K# o v3 ~9 H8 A
- <tx:method name="*" propagation="SUPPORTS" read-only="true" /> 6 h8 e, Z3 H2 _
- </tx:attributes> 5 G) V6 O' ~9 W
- </tx:advice>
# h- v/ D% j/ R - </beans>
复制代码 spring-mvc.xml:/ A% l, T4 J, y/ _8 n* H1 e, G0 U
4 H& E1 D4 n" G" s; i
- <?xml version="1.0" encoding="UTF-8"?>
* y' H) I, Z: F; L" ?9 O - <beans xmlns="http://www.springframework.org/schema/beans"
6 c! D! f. E+ a$ S4 Q - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"5 Z; x* S$ W8 t! J, ?
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"; {' z' F8 P; l1 r; p4 R P2 M
- xmlns:mvc="http://www.springframework.org/schema/mvc"
& I$ |8 U; _' L! G6 o, P - xsi:schemaLocation="http://www.springframework.org/schema/beans
1 [2 F2 ]1 `! a( i5 x* | - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 J% J: w7 S6 Q1 l) \6 S. R
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
4 x+ ?# p, t3 v; { - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
, K/ l" W( q; A5 } - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd; r! p. }' O$ e$ { ~* _2 d9 K
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
- D4 O1 I+ h# {6 K - <!-- 自动扫描的包注解 -->' ]4 ]- z( h) G( B# k: q( \
- <context:component-scan base-package="com.hoodo.rci" /> Z) ^1 I- V# y* `
- <!-- 自动注入 -->3 C1 ^7 p& K& ]$ a# R8 O! c
- <mvc:annotation-driven />
" G! X0 u$ m& s8 M$ t. ^ - <!-- 视图解析器 -->
^, F6 E. W3 o8 \5 t! c N; G - <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
# N- S( s( J% z6 X# @9 P - <property name="prefix" value="/WEB-INF/jsp/" />0 M. f! Y) E) H
- <property name="suffix" value=".jsp" />
8 X' z2 b+ q. d- p" c$ W - </bean>" s' H8 ~3 j; p
- 8 {; l8 O8 h% m) s) i
- <!-- 异常解析器 -->5 c; h' d2 a! Z# T! C T
- <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">5 J7 V/ }2 b# l2 M6 z, C$ U# I/ x
- <property name="defaultErrorView" value="common/error" />% N+ @6 x4 x9 D$ P X
- </bean> -->
* V+ u& X. x* R/ {5 v& P2 n" C -
' L- |) C: |" N1 l; x4 N3 X - <!-- 拦截器 --> E t0 D4 Y. l' H7 L/ I) c
- <!-- <mvc:interceptors>
3 y( s, `/ f6 R' s4 g# P S( f+ O* n& l - <mvc:interceptor>
, I+ ]+ W* P1 b* U+ i - <mvc:mapping path="/admin/*/*" />
, J N' v& H& w/ `% O3 B, U5 G! X - <bean class="com.filter.CommonInterceptor"></bean>; }8 `. G* O- i- e% W# L% p
- </mvc:interceptor>, N! o+ o5 ~/ ^0 g. G6 c
- </mvc:interceptors> -->
* x* k, v4 L1 ]& A9 A$ @$ B, z
2 y$ A! \& ^- H5 R' i% D- <!-- 上传文件相关的配置 -->
4 ?3 c3 }. i2 r/ I. L9 } - <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">$ H9 W. k( f i* u7 v/ L
- <property name="defaultEncoding" value="utf-8" />
. ^6 h- X0 Z4 X4 w+ z - <property name="maxUploadSize" value="104857600" />
, l1 o! X4 C0 e: M, f+ y7 j - <property name="maxInMemorySize" value="4096" />
" v8 {! U1 s# u* @5 @, w( `% k; ] - </bean>- b5 B# `6 Y4 L# Y. J2 w3 p* D
- </beans>
复制代码
# ~. ^* _$ e5 y! t; a9 A$ P, Y& Z4 X# U% [
3 y; d5 k5 G+ {5 f4 ~/ [1 L8 e
SpringMVC+hibernate4.3+Spring4.1整合案例. Z6 ]4 a& y5 L# S
( _# e7 X5 l1 ]$ H |
|