TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
springMVC+hibernate4.3+Spring4.1整合案例spring.xml:7 |3 A6 {* ]7 Z: Z& c$ \4 B
- p0 H5 T8 C% K2 m6 ?
- <?xml version="1.0" encoding="UTF-8"?> # F0 V q8 |& G2 S/ L, A
- <beans xmlns="http://www.springframework.org/schema/beans"
' D: w: L) z0 t t - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - W5 D a0 M i L: C b( w7 ^4 m
- xmlns:p="http://www.springframework.org/schema/p"
- R+ d/ j' W) Z - xmlns:aop="http://www.springframework.org/schema/aop"
+ G( `! R$ o5 z" [. u7 @ - xmlns:context="http://www.springframework.org/schema/context"
( G$ O. S( `1 w5 U# Z4 r5 K - xmlns:jee="http://www.springframework.org/schema/jee" 5 P0 |) ]% ?2 j" y/ p; j; M
- xmlns:tx="http://www.springframework.org/schema/tx" 2 R9 E- A: v7 h! n( I9 [6 m: W+ M
- xsi:schemaLocation=" . n2 V6 ]5 P7 h2 C3 m) Z- `$ J
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
; u, H3 J/ W, p! Z# D9 A- V - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
1 s( z1 W! T3 P8 F! S: k( a8 w3 e6 [ - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 1 A8 G' s; D% ^) g! G0 H0 r
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 9 E U `, O: p! H
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> % Q4 O& k/ W. F" y$ D! T
- 1 \$ C2 k! w" K4 S" R) Z
- <!-- <mvc:annotation-driven /> -->3 C: F5 C" N5 _7 R
- <!-- 【配置视图解析器】 -->
( M" e6 d ]1 c0 M( T* \ - <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 --> 2 p, j b/ z" y) ?
- <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 --> * }9 N4 e1 B! u* b# F9 k S
- <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> " ~, d+ Z1 C/ ~" M+ u) W, M9 Z
- <property name="prefix" value="/WEB-INF/jsp/"/>
9 t7 \* r; E; g) Q - <property name="suffix" value=".jsp"/>
3 k+ q- Q9 C/ f+ x; p& z. u1 e! f - </bean> -->; x% U. h7 s% g4 G( O# m
-
* [- ?+ M; c- K1 _# N5 `+ i5 ] - <!-- 导入springMvc.xml配置文件 -->. I! Q7 n8 ~# ?# v) D
- <import resource="classpath:config/spring-mvc.xml" />
- r' M$ _! G1 M! W - <!-- 加载数据库配置 -->
3 R5 n% w/ k" {: C+ c7 F - <context:property-placeholder location="classpath:config/db.properties" />
( [3 T: b$ t3 A! \/ D2 P8 l) p - <!-- 加载数据源 -->9 Y) u, N* [& J/ {
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">% q# w0 ? S- @( E8 }
- <property name="driverClassName" value="${db.driverClassName}" />) j) Z& j, }. F8 Q7 P: R- R0 @
- <property name="url" value="${db.url}" />& s; Q6 `- i D8 S
- <property name="username" value="${db.username}" />+ T+ n; p& W; K: S- b C6 t
- <property name="password" value="${db.password}" /># }( u# d' _' z' x5 I
- <property name="maxActive" value="${db.maxActive}" />) ?6 Q7 E/ {1 F2 k; @4 L. X2 o0 {
- <property name="maxIdle" value="${db.maxIdle}" />
1 {& r6 G; q p9 _" K" r+ P. w - <property name="minIdle" value="${db.minIdle}" />. I3 U) g* F/ t" R" t
- <property name="maxWait" value="${db.maxWait}" />1 f7 Q+ @' |9 {1 g$ I
- </bean>+ J! W) N- [' A# [: \6 \$ Y8 p. o
- 5 B- t& R/ ^" P6 O' @$ G
-
2 Q& T8 [! U5 T2 d; S: L+ V - <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
' A' d! m" y; |6 J - <property name="dataSource" ref="dataSource" />
4 d; Y: Y8 l; N! Z - <property name="mappingDirectoryLocations"> 3 M6 q, H2 a' I' U9 F
- <list>; m3 y' r j5 o9 e8 S! S0 A* h
- <value>classpath:config/hbm/</value>& A7 m+ a0 e5 c" S8 Z# q
- </list>
4 Z r, O9 N+ c - </property> ' M5 z, A" k* |& J9 Q y
- <property name="hibernateProperties">
2 H* d; } p1 e. g - <props>
5 l# G u+ d7 g | - <prop key="hibernate.dialect">${hibernate.dialect}</prop>
N8 ]; S/ W+ H8 g4 T - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>2 `8 Q Q: C" A* @4 U. K" e
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
, v& O9 b1 z, s2 X+ v, d - <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->9 J7 z4 E A; x+ L/ x$ C8 F2 c
- </props>
4 d' z! @& p: c& G3 h% B) { - </property> / x: h+ `4 e/ q0 X l
- </bean>6 D9 l v ` C' m/ J
-
; h$ a' Q- j& ]; `4 i% y - <!-- 声明式事务 --> 7 b1 c( s4 {3 o( d" c% [3 _
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 2 X. R8 M# @' }& l4 d; Z6 ~# C
- <property name="sessionFactory" ref="sessionFactory" /> ( g+ y& I0 x9 m+ j
- </bean> 7 E5 Y! B; c1 {
- ' U0 ~8 ~; k; h# l
- <aop:config>
/ [1 k5 f6 t6 S, J+ d( v' N3 J3 _ - <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" /> 4 R, t3 w/ k2 q* D- W
- <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> ! E+ N" K8 k# \2 @& u3 w& m
- </aop:config>
& F5 o! I1 z, u$ P' h -
1 N+ x9 n: ]) ]+ r* i, b* F9 O4 Q2 K% ? - <tx:advice id="txAdvice" transaction-manager="transactionManager"> " |6 T4 S) N$ s f
- <tx:attributes>
; y: x, B& f4 y/ E2 [3 o& B - <tx:method name="save*" propagation="REQUIRED" />0 ? b& O0 Q- Y3 B2 J
- <tx:method name="add*" propagation="REQUIRED" />
4 f8 ]) a$ e: Y* p! c, t5 c - <tx:method name="create*" propagation="REQUIRED" />2 k7 O. W% s( V& s9 r# o- Q
- <tx:method name="insert*" propagation="REQUIRED" />
" @/ i& I: g* ~) x - <tx:method name="update*" propagation="REQUIRED" />
# J( [0 q' d% m5 ]+ m - <tx:method name="merge*" propagation="REQUIRED" />
4 i, S6 y/ R$ i8 w1 ^ - <tx:method name="del*" propagation="REQUIRED" />7 o1 R: \( p+ X* I0 n; Q
- <tx:method name="remove*" propagation="REQUIRED" />
$ X o7 K5 C# n* k2 j$ ^ - <tx:method name="put*" propagation="REQUIRED" />
9 U9 u! I! |/ ^9 k/ i) ?7 o( T - <tx:method name="*" propagation="SUPPORTS" read-only="true" /> / W G8 q Z& K1 j* p
- </tx:attributes>
9 Y5 R; i$ c8 S; Q - </tx:advice> 5 b3 S; o2 I& m% D" L5 [
- </beans>
复制代码 spring-mvc.xml:
( V+ |- {: I! l3 {5 t2 h
X& s8 u; g1 g* G- \- <?xml version="1.0" encoding="UTF-8"?>
7 Z e# M, u# M7 h$ W; w A# f" d - <beans xmlns="http://www.springframework.org/schema/beans"' D5 z# l5 n. {0 ^2 c X6 C
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"' v1 [2 T- `/ P
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"; {4 r9 ] T7 [3 t- C4 J
- xmlns:mvc="http://www.springframework.org/schema/mvc"
% w9 {* @2 o' _. ^8 ~6 t - xsi:schemaLocation="http://www.springframework.org/schema/beans
9 z( b: b$ W' K; K: H0 r6 @" i# X - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
* ` l, u, R) C! `" Z! l+ f, [0 j - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
: A5 g7 R$ O. \+ P! P/ m - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
" Y8 ?* I8 |9 f+ O - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd# m9 |. N7 e- M# m
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">+ {& F3 X, Y, a! }# G0 J3 B
- <!-- 自动扫描的包注解 -->
6 |1 Y5 Z! o8 e0 u( u - <context:component-scan base-package="com.hoodo.rci" /> A; {) i- M2 W& Y/ e, I8 e4 r/ R* k3 K
- <!-- 自动注入 -->
( C! @% R1 K6 ^1 X - <mvc:annotation-driven />9 Y7 Z0 k8 R3 A/ W x
- <!-- 视图解析器 -->
* D( x+ K& u" w9 ? - <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">$ P4 ~, _; z t; _2 Q7 U
- <property name="prefix" value="/WEB-INF/jsp/" />
: `* P* F! L& X1 P1 k: e2 u$ R: b - <property name="suffix" value=".jsp" />
i3 E& w! H) _8 W* r - </bean>
* I% S) l. r, ? - & x1 F1 ^5 j4 a/ F' V, P" p( p
- <!-- 异常解析器 -->7 d5 N) d# C- d' I; i
- <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">5 j- K" E* P+ F# W/ N
- <property name="defaultErrorView" value="common/error" />$ P, E3 H- o( }2 x
- </bean> -->% f: j5 O' N; ~% `; w: ~+ F3 [
- % G! u: G7 d' B, _
- <!-- 拦截器 -->
. d4 e2 `2 s7 q& H- C# R - <!-- <mvc:interceptors>
2 Z5 r( M8 r6 q" e; Z - <mvc:interceptor>
Z d4 i6 Z6 |" `" D) v# E - <mvc:mapping path="/admin/*/*" />2 M* ~6 O: c2 N3 D1 q+ k. T
- <bean class="com.filter.CommonInterceptor"></bean>
& s9 C& N" x) c d% D - </mvc:interceptor>
6 v0 ~! J& o2 ? - </mvc:interceptors> -->
/ X7 x. I8 j- y/ z8 A
. ^# y$ ^" \+ I% s$ x) q- <!-- 上传文件相关的配置 -->, o/ v4 W& s; u. d2 s- t0 W
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
! p4 g/ ^9 n8 J: ] - <property name="defaultEncoding" value="utf-8" />
) H" q; M! k3 L. [4 O$ U- W5 N - <property name="maxUploadSize" value="104857600" />
: ]* S8 s2 G s) }2 _+ s1 | - <property name="maxInMemorySize" value="4096" />8 G8 N7 W; ~2 z- c9 a/ |
- </bean>
+ e% `) h' \. z+ c9 |' e: z6 H- \ - </beans>
复制代码
3 ]; @" `- R9 t `6 ]0 K- G( M5 X3 y/ W$ u8 w" [5 Z$ Q. L
" k+ n6 k; d) M) v' C% `! \
SpringMVC+hibernate4.3+Spring4.1整合案例' |* X4 J# l6 E8 h' f( N) V: ?% I+ E; b! E
- d+ d1 ]( F/ _2 t$ Z$ g$ i8 w) K |
|