TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
springMVC+hibernate4.3+Spring4.1整合案例spring.xml:, n5 B/ A) A5 ~. |% o2 o
& g1 ^3 I/ v2 s! p5 I$ f+ p$ j- <?xml version="1.0" encoding="UTF-8"?> 4 Y1 w F9 b/ e) D* B. \+ V8 P$ h
- <beans xmlns="http://www.springframework.org/schema/beans" 7 l) Y, m3 b ^, W9 V5 A1 y
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ) @% h& S1 x- P% D, X- f" o# Z
- xmlns:p="http://www.springframework.org/schema/p"
' i; X/ x& y7 o4 `5 f9 B - xmlns:aop="http://www.springframework.org/schema/aop"
# Z& b. E2 y. H) u; t- { - xmlns:context="http://www.springframework.org/schema/context"
! g. z. \) Y* T( b4 ]; C* l# Q - xmlns:jee="http://www.springframework.org/schema/jee"
* @' h: ~- C! R9 Q7 i - xmlns:tx="http://www.springframework.org/schema/tx" # S: _9 T( Q* C8 n" |! n) b, l
- xsi:schemaLocation="
4 L% p4 Z4 ?7 K& G- O3 i( n2 v6 @ - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd . j' m, o, V2 f3 g
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 6 {* K9 [. Q; l) V* Q V- u+ Q
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 1 N3 m& }- o; @* ~# ~( i! D
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
1 M" a2 W; F+ K. [. ?: ] - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
! g \' E s3 `8 g$ b - " ]2 Y5 x' G. q& ]
- <!-- <mvc:annotation-driven /> -->
! G6 ]3 e% i# X2 c3 e4 b s9 w - <!-- 【配置视图解析器】 -->
* o4 d; L0 C7 O7 E3 [ - <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 --> : V& K3 q) n! G" d! Z! J2 g
- <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 --> ' ]+ J: r1 ^9 b/ z/ `( i
- <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> ( u: R4 H7 D% h6 I! _# C. M
- <property name="prefix" value="/WEB-INF/jsp/"/> ( Y& S) w, [, _8 G0 e$ M
- <property name="suffix" value=".jsp"/>- [8 `% k* [& r% t1 ^' C& A! n1 n
- </bean> -->
2 `! b) |; z- [: t: `4 l/ i - 0 S& c2 @; ~$ d( g8 Z
- <!-- 导入springMvc.xml配置文件 -->" [7 F1 _& f) I9 K- ~
- <import resource="classpath:config/spring-mvc.xml" />
& G6 P/ t3 O6 k: M2 l% } - <!-- 加载数据库配置 -->
/ |9 N% f& T! X# b) e - <context:property-placeholder location="classpath:config/db.properties" />0 S- S: i+ W ]! h7 [
- <!-- 加载数据源 -->! @( I6 y3 Z/ z7 j2 U; B5 `
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">7 X+ E+ x' m/ h* J9 v7 P/ R7 x
- <property name="driverClassName" value="${db.driverClassName}" />
) W* F' [8 L+ |8 e1 q - <property name="url" value="${db.url}" />3 ~# F$ ]- M! t& X# D
- <property name="username" value="${db.username}" />+ _: Y. Y2 m4 q2 _$ k- j6 C
- <property name="password" value="${db.password}" />
/ i: M T6 l0 X+ u2 t - <property name="maxActive" value="${db.maxActive}" />
: ]+ S1 }4 |& O: K; O6 s - <property name="maxIdle" value="${db.maxIdle}" />+ r1 e( q$ | F! P9 K# \1 _! ]1 v
- <property name="minIdle" value="${db.minIdle}" />
! O% D; z1 u+ v4 T - <property name="maxWait" value="${db.maxWait}" />
* _* ], G* ]; h, c! u9 \4 J - </bean>
) E! s1 i+ `/ ?* I5 t' Q) i& r - : g/ f. i! q# v( ^! ]% G
-
/ Y9 `$ I8 E2 n' ^2 ] - <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
. |1 W* |+ T' X - <property name="dataSource" ref="dataSource" />
1 h- |9 h* l& D9 X - <property name="mappingDirectoryLocations"> 4 j( |$ a6 B8 t$ W0 |
- <list># L& ~- n. u* r( i" n
- <value>classpath:config/hbm/</value>
9 ^+ A7 D& N7 Q7 m - </list>2 F% t; G! d: _* j; g
- </property>
3 W) H) b q2 P - <property name="hibernateProperties">
3 w9 \/ p5 H3 g3 Z' B, x4 f+ ? - <props>* B3 K4 Y# g; [4 _8 m) ^7 l8 }7 p
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>" s% J# C2 g: ] x( s, a
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
- N( T% _& S% L, W - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>- X; L0 _: P5 P0 _5 X' p
- <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->
' O* U3 ]9 f" k2 U - </props> ]) N) P) R1 x" _: w9 g% ^5 c
- </property>
7 a! ?* N1 I' M/ R - </bean>( g: d& {4 p6 r- {( p F7 I
-
! [5 v- t. O! b, _2 ]7 `3 P0 I - <!-- 声明式事务 --> + Z- e: l2 W' U1 X0 n: }
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 8 I! ], m) Q9 J) x
- <property name="sessionFactory" ref="sessionFactory" />
0 U$ I+ U8 r! Z4 Q4 V - </bean> / ~3 q2 Q9 K$ J$ l* H! p
-
# J2 ?: ^4 k4 M# Y - <aop:config>
% N+ ~- R8 }" E1 j7 g - <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />
0 {' `1 Y- T# [7 `% S3 I$ M - <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> ! y8 K4 o# P$ Q' L$ e
- </aop:config>
- }4 }! u: W8 j7 h; \% Y) y( ` - + `+ d e% J: X, {1 ^) m9 I
- <tx:advice id="txAdvice" transaction-manager="transactionManager"> 3 x2 h5 ~- c0 a, W
- <tx:attributes> * y- w. Z! _% ?' g
- <tx:method name="save*" propagation="REQUIRED" />
7 K# Q% K D) M& x4 K! Q/ N5 w - <tx:method name="add*" propagation="REQUIRED" />2 Q% y0 V; u! ^+ Z/ q% s L) O
- <tx:method name="create*" propagation="REQUIRED" />
5 ]+ Z- E! O$ V. F; g: o v4 p - <tx:method name="insert*" propagation="REQUIRED" /># X s+ Z6 D, g. }" W
- <tx:method name="update*" propagation="REQUIRED" />
% \- T9 _( K2 f - <tx:method name="merge*" propagation="REQUIRED" />
3 B( f# z8 v. Q - <tx:method name="del*" propagation="REQUIRED" />4 N- A/ g: U/ k( v* p' F( `3 g
- <tx:method name="remove*" propagation="REQUIRED" />/ n2 M7 u. I" g6 r
- <tx:method name="put*" propagation="REQUIRED" />: U, N4 r: G8 }) [9 g
- <tx:method name="*" propagation="SUPPORTS" read-only="true" />
. @6 B) z" s9 `0 h+ X8 L - </tx:attributes> " q) O P) Z) D1 q3 k0 K' b
- </tx:advice>
5 @( s$ D) r4 D- q0 f" [ - </beans>
复制代码 spring-mvc.xml:5 {8 _- z' C* o& d+ W% _
1 |7 Y: D& w4 C! p, z; g$ v2 b: Z
- <?xml version="1.0" encoding="UTF-8"?>
1 W6 A5 t- o8 P1 G) v7 d* g - <beans xmlns="http://www.springframework.org/schema/beans"
; R1 h$ N- X% [6 F$ H" \8 @4 Z - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"$ e; P. T' I4 T% m! r. H
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- j6 s; R7 A4 i1 ` - xmlns:mvc="http://www.springframework.org/schema/mvc"6 C6 _ N& F# ]
- xsi:schemaLocation="http://www.springframework.org/schema/beans
; ?# n2 Y/ ?+ E3 L$ X* g - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd # q: Q( J8 O# ?" v0 @& J
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
5 U2 o7 {* y/ `4 f0 Y& S - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
I9 N9 }1 k* j4 b+ Q8 T - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd/ M0 I/ D+ Z: g3 E4 N# f
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
3 \+ w# z0 r2 I& I9 Q - <!-- 自动扫描的包注解 -->+ v/ C. z* ~- {& M
- <context:component-scan base-package="com.hoodo.rci" />; a9 a4 O0 E( {
- <!-- 自动注入 -->
w* O1 [& A; h( s - <mvc:annotation-driven />; S, p& \" M3 j1 S: {9 K; K
- <!-- 视图解析器 -->2 z$ \9 y: J+ ^9 K
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
% ^9 F3 ~" b; f' i - <property name="prefix" value="/WEB-INF/jsp/" />2 L$ _* e; P) y. {" y' T
- <property name="suffix" value=".jsp" />
' I8 Y+ m& W/ U5 m - </bean>
0 O% v, ]" ?7 Y0 l& P' o. L; Q - ! @- m! r. o4 `1 F _! j
- <!-- 异常解析器 -->/ e! X! Q3 L4 w. t: v: T
- <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
3 {* d: X8 I) v3 s* \" N - <property name="defaultErrorView" value="common/error" />
3 w! i- t+ R, @) c# f - </bean> -->
& n% f9 ^, t8 X+ y" a9 ~ -
4 u' p4 J Q& L! W( f7 Y% O' J0 m - <!-- 拦截器 -->6 U9 s, C4 I8 o9 ^
- <!-- <mvc:interceptors>! C1 r: D% x% Q( N
- <mvc:interceptor>$ i( a; o4 s( n' B9 n: u$ E
- <mvc:mapping path="/admin/*/*" />
' M) ]; [( q" ^ - <bean class="com.filter.CommonInterceptor"></bean>
- Q8 K2 n, _3 _% a - </mvc:interceptor>: N1 Z o0 ?2 S+ c) X, I
- </mvc:interceptors> -->% U5 p0 T" W3 | s- ^1 C( j% W
`, q5 r$ z: d! A- <!-- 上传文件相关的配置 -->5 h5 |3 Q0 Z; \) I% z# z# N
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
+ p! R8 K W4 r - <property name="defaultEncoding" value="utf-8" />1 r5 J9 W) d' e p
- <property name="maxUploadSize" value="104857600" />: o7 K: K1 }8 w0 b1 {1 ?) e$ B
- <property name="maxInMemorySize" value="4096" />+ }7 S$ _' z, G8 _
- </bean>
% z4 N& q! a: o6 z* E! a3 M$ W - </beans>
复制代码
: H% y! D! n, }& P" U" b$ e8 t9 [1 c1 h5 J. ]7 ~" h' R0 C
, L- _& {% l! ]- W9 hSpringMVC+hibernate4.3+Spring4.1整合案例
- G: W/ J6 f$ N9 N/ f8 ~# j# |. s; ~ J7 F
|
|