TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
springMVC+hibernate4.3+Spring4.1整合案例spring.xml:% b) I9 Q/ W) ^: e3 s0 G; h
5 o6 {7 E' u- M. B
- <?xml version="1.0" encoding="UTF-8"?> 2 l f. a" w' }3 B; _& V/ b& ^3 q
- <beans xmlns="http://www.springframework.org/schema/beans" - G7 ~9 i( H/ ^6 i) x2 o# Z
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7 S) K- i3 X5 u$ f& {/ E. ]% a' n. ~ - xmlns:p="http://www.springframework.org/schema/p" 4 V$ z4 u- E: U
- xmlns:aop="http://www.springframework.org/schema/aop" & ]0 ^( W! p4 m! h1 E
- xmlns:context="http://www.springframework.org/schema/context"
0 V# p0 M% |+ F: |' t/ `: t - xmlns:jee="http://www.springframework.org/schema/jee"
3 [/ H2 W# a/ Z7 T% d: @. @: |, K& i - xmlns:tx="http://www.springframework.org/schema/tx" 4 k1 \8 w' l. K, G5 M2 a
- xsi:schemaLocation=" & B/ x2 G, t) m- \7 ^+ T" p$ n1 U
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
1 q- S" l: k' R - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 8 a- \/ U. Q) N( R6 z5 O+ g
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
% d; V# M0 C/ X. P3 @ - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 3 P h& B- q8 F# W& w) P; G
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> , l+ T: H, [1 }: a* N4 n
-
; t$ d6 J# J! P' `3 N - <!-- <mvc:annotation-driven /> -->
: y' W2 \6 X' Z' |4 k - <!-- 【配置视图解析器】 -->
# t4 j }8 `7 j F. {, e) ? H: I - <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->
k5 l }7 V) A7 W - <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->
# n# t+ U* o' R* j$ G% Z1 y - <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> + v0 }! C/ N6 e7 [
- <property name="prefix" value="/WEB-INF/jsp/"/>
- Z4 f8 H* a$ T+ @ ^ - <property name="suffix" value=".jsp"/>
( l6 `& z. n, t - </bean> -->
2 {: N/ O/ ?, j6 m6 U -
& v# @2 Z( [) X6 A/ X - <!-- 导入springMvc.xml配置文件 -->; F$ U8 U8 ^$ X- x6 a% `! Z( b
- <import resource="classpath:config/spring-mvc.xml" />
' B+ B% w( _+ [# d* u - <!-- 加载数据库配置 -->+ d4 u) A. l3 e+ ?
- <context:property-placeholder location="classpath:config/db.properties" />
/ K4 n& X' l7 d - <!-- 加载数据源 --> q l# D" D, o7 X' x, J+ \1 C
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> q: Z% q2 f6 l9 b/ m
- <property name="driverClassName" value="${db.driverClassName}" />
; o4 Y& a* { K$ A6 Q) s - <property name="url" value="${db.url}" />
! ?6 r" A+ T2 M - <property name="username" value="${db.username}" />
0 V/ B0 {( r0 _) B/ L3 k0 N2 E, D - <property name="password" value="${db.password}" />
j- X5 [2 K5 u# b: J3 C - <property name="maxActive" value="${db.maxActive}" />
& Q2 }) G8 U$ R& C( H. L$ i) \6 z - <property name="maxIdle" value="${db.maxIdle}" />
9 y, |3 n& K/ b; V - <property name="minIdle" value="${db.minIdle}" />
' @' l' B f# z+ c - <property name="maxWait" value="${db.maxWait}" /> F, c$ P; r7 r8 p7 J& y, N
- </bean>( J+ j9 ]1 ]' R7 L
- 5 K! X$ t% M, Z& y4 L2 t, n
- & \7 t" N+ s8 z: T& h+ c+ C4 [
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> # D7 B- `5 J5 P8 t% I% V# H
- <property name="dataSource" ref="dataSource" /> ) I3 S9 M) a0 [( v: j5 [ ?
- <property name="mappingDirectoryLocations"> % W& q9 d% \! `6 o/ s
- <list>. W7 u% V7 f8 {! ]6 R
- <value>classpath:config/hbm/</value>/ c! B2 y9 v1 d$ [& H% e- g e; v
- </list>: p; S+ r, r& r F5 c8 L, \
- </property> ) U4 U( T6 P; {: W. k, S2 F1 T: j; B
- <property name="hibernateProperties"> , G! ~) `; P, ~! C
- <props>
2 r7 A" J2 n9 G: H2 Z4 ~ - <prop key="hibernate.dialect">${hibernate.dialect}</prop>( I* @2 H- `; z
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
# R; L( ^( b0 A& c1 [ - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
! S5 }# m+ L6 | - <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>--># G8 C, w- ~1 \' @# Y. A
- </props>
" i" X. C# e w: g3 W) s6 l - </property> - M# }1 ]. k$ y3 J! J* l, c! D/ i
- </bean>. f2 r- c$ d7 R% I* i
-
; |+ U1 T7 }6 p4 ? - <!-- 声明式事务 -->
3 }# c# \: E4 z+ o" S - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 5 e; o6 m% l! G/ B2 o
- <property name="sessionFactory" ref="sessionFactory" /> 3 z8 |- |1 ~" M6 S
- </bean> L5 S# S9 n7 f8 Y. N
-
1 T# Q V, c1 |/ o( d - <aop:config> % C# K1 u: v% C4 u8 s$ R
- <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />
$ L3 W5 q. F7 \! C; k - <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
2 m5 T8 V7 N4 x - </aop:config>
4 x- p# M8 G( Q' {) J" q- V3 o - ( E0 x) z# V% d- l
- <tx:advice id="txAdvice" transaction-manager="transactionManager"> # J+ C7 ~" B& \
- <tx:attributes>
) q) O/ I: c7 d" B4 u5 [, b L! Y - <tx:method name="save*" propagation="REQUIRED" />* P, f9 a, g) o
- <tx:method name="add*" propagation="REQUIRED" />' J$ [: Q& S# B, [ l _1 Z/ g0 r
- <tx:method name="create*" propagation="REQUIRED" />+ o- A* x; b$ x/ A8 y
- <tx:method name="insert*" propagation="REQUIRED" />
, d" U7 o* }5 i) }* \& p: H - <tx:method name="update*" propagation="REQUIRED" />
; K H9 {$ C, l( S# F! G - <tx:method name="merge*" propagation="REQUIRED" /> r3 ]' N) M3 T! d# B, |
- <tx:method name="del*" propagation="REQUIRED" />
* f( d4 _0 K1 w Z - <tx:method name="remove*" propagation="REQUIRED" />
% Y$ C6 l) k1 @0 D& z) F/ Y: A - <tx:method name="put*" propagation="REQUIRED" />$ {- g# n) d# p, s+ x/ c& X
- <tx:method name="*" propagation="SUPPORTS" read-only="true" /> % j/ Y% E% r& v8 R
- </tx:attributes>
" P y2 ?9 [ p! |4 O9 A - </tx:advice> , D7 q- B+ a) U' d, x, r
- </beans>
复制代码 spring-mvc.xml:
5 H6 W4 u7 g2 N8 B0 g, K N' U, l! f4 y+ n; p7 g" Q! L1 R0 e
- <?xml version="1.0" encoding="UTF-8"?>
9 w6 `9 w" h& Q2 f& ] - <beans xmlns="http://www.springframework.org/schema/beans"
) K9 n8 |$ N- _+ T" ` - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
% H( M) C( N4 ]' Z r% ~+ C - xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx") x, N' ]! t* z5 Y
- xmlns:mvc="http://www.springframework.org/schema/mvc"/ C& a; ~& H4 z3 X
- xsi:schemaLocation="http://www.springframework.org/schema/beans
& m2 ]* d6 Z! B# ? n( F1 M- z - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 c. h* g0 u- u. }- k4 A: a0 c
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
* ]( @/ |0 a8 r S' D - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 5 z! b7 K) C* W& l
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd2 k4 D, ~- D1 z1 _
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">; o( Z+ h1 O) B2 k
- <!-- 自动扫描的包注解 -->
( {6 s d7 b2 l6 T5 T8 s J3 ~+ R9 e - <context:component-scan base-package="com.hoodo.rci" />
- h" g8 Z& C8 f* w- y - <!-- 自动注入 -->
) S) |3 U: \* ^) I- U4 }9 _ - <mvc:annotation-driven />
) R) \0 B/ l; `) }5 C - <!-- 视图解析器 -->4 ` ]4 }1 _- C2 M+ B
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
! F! Q1 T! l* v: t% a - <property name="prefix" value="/WEB-INF/jsp/" />
' m' W2 h/ n4 p, F( x - <property name="suffix" value=".jsp" />* j: h4 m9 ^. o3 {7 B1 A& m4 O$ \
- </bean>
( s. s' x( n: a# @" [/ i$ O) I -
# Q0 a. b- E9 R3 p& d - <!-- 异常解析器 -->7 [' s D# J8 V6 z8 X4 b# o
- <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">$ M _7 E) r4 u3 c: z
- <property name="defaultErrorView" value="common/error" />* i0 ^6 p; V* _
- </bean> -->, `+ H8 n! ? [6 u1 u6 M
-
0 s7 F- P9 h) Z& r& g& P - <!-- 拦截器 -->
& X5 f; b2 i# j1 C - <!-- <mvc:interceptors>
; \: L" Q3 a. }1 j4 e - <mvc:interceptor>
6 H4 {2 R1 y% u8 i! J - <mvc:mapping path="/admin/*/*" />* `' ]) G8 u3 E2 ]
- <bean class="com.filter.CommonInterceptor"></bean>% ~7 Z* r, k/ R% {4 M: l+ a4 i* ]
- </mvc:interceptor>, x' _+ J; X! F
- </mvc:interceptors> -->0 j- p8 R6 n1 c5 d' ]' B& F% n# i
( k [. G% W; c) n* v- <!-- 上传文件相关的配置 -->
% W, O: {. L N1 Y ] - <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">9 _" N( a( ~* n- L9 v
- <property name="defaultEncoding" value="utf-8" />; N4 T4 Z$ \+ A6 o) J' R6 J& X
- <property name="maxUploadSize" value="104857600" />- l2 m5 x! d( z% W1 @2 ?5 x
- <property name="maxInMemorySize" value="4096" />
( H0 M% A7 {' F5 c0 E% y1 I - </bean>* x u, A6 L6 n) l
- </beans>
复制代码
9 U7 t- ~& e8 H
- o4 S& r Q% G) P' @/ ^! C) K- m# u2 F; z! V, R6 g
SpringMVC+hibernate4.3+Spring4.1整合案例9 ]- g+ a9 W# R0 w
- W3 C3 U+ b8 P } |
|