TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
springMVC+hibernate4.3+Spring4.1整合案例spring.xml:' k, j( N- W& I3 s M
. b& U# \+ n3 i+ H- I
- <?xml version="1.0" encoding="UTF-8"?>
7 h c* R' Q2 b) s$ u - <beans xmlns="http://www.springframework.org/schema/beans"
* t9 X% B' k, L' V+ w - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ~5 g& x/ d/ K$ ]! ?+ P
- xmlns:p="http://www.springframework.org/schema/p" ; M. A. r( S. u8 G3 F
- xmlns:aop="http://www.springframework.org/schema/aop"
/ e, I% U$ \9 I$ R& K3 z0 y$ S - xmlns:context="http://www.springframework.org/schema/context"
" P8 |- a/ M- ~ - xmlns:jee="http://www.springframework.org/schema/jee"
$ l, F$ {7 I, Z- l# A - xmlns:tx="http://www.springframework.org/schema/tx"
( D6 E1 v8 ]5 t4 u - xsi:schemaLocation="
/ ?3 F- g* k, C3 c5 \6 } - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
8 T$ L2 R' r. w6 L Z7 r i& z* | - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd : S# T7 b4 p4 H# p6 Z
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ; o* v8 w8 b/ w" h
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 0 G% Y; n0 l, G" r
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> : c Y. K6 P( c$ _) b7 I, V
-
6 o8 W/ c3 f4 v3 N4 r% V0 z - <!-- <mvc:annotation-driven /> -->
/ s3 H& [' V- B. B - <!-- 【配置视图解析器】 --> + g; l. |- G! Q0 g4 _# b$ n. t
- <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 --> 2 b. E3 ~& U [0 l0 A
- <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 --> $ z* p: N1 G8 r! L) f) @& M, E
- <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> / Y. {7 W4 r8 |7 E& P0 q- `5 N
- <property name="prefix" value="/WEB-INF/jsp/"/>
) u1 K" g7 N# U5 x. T - <property name="suffix" value=".jsp"/>
& W0 i* Q+ `$ G9 i0 u - </bean> -->
$ J) |# C- j+ ?7 O3 N' P3 ] - 8 n* {1 K5 l: Q, u( u+ a- m: J2 P& P
- <!-- 导入springMvc.xml配置文件 -->) L) F4 Q+ @7 {' V4 u" c2 ^% D0 p
- <import resource="classpath:config/spring-mvc.xml" />
# G+ _8 W; n" g' j$ ?' b3 g. O - <!-- 加载数据库配置 -->
& i; G8 ~- g: L8 ?+ A/ W o - <context:property-placeholder location="classpath:config/db.properties" />
' |/ S: e+ N9 O# K: V - <!-- 加载数据源 -->& T: b- ~5 r7 p6 B
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
$ y9 i% ?. h% r4 Z0 Z, _ - <property name="driverClassName" value="${db.driverClassName}" />
$ e( N* [7 Y8 ?+ I - <property name="url" value="${db.url}" />2 i6 y! R5 M9 e% t! Z/ A
- <property name="username" value="${db.username}" />
0 w' n2 j- b% F) I X: l - <property name="password" value="${db.password}" />; Z& ]$ r' a1 V. s) \
- <property name="maxActive" value="${db.maxActive}" />
. ~* c, J6 _4 G - <property name="maxIdle" value="${db.maxIdle}" />
8 Z6 D# U! ]5 ]* N9 e# I8 d9 W: t - <property name="minIdle" value="${db.minIdle}" />% V) h/ x9 `( W& q7 M! Q: t8 u
- <property name="maxWait" value="${db.maxWait}" />. n9 h4 B* H- O+ x; B3 A
- </bean>, r. G1 r4 T; z
- * ^5 i" j0 C3 w& \6 e! F2 V
- 9 W% Y% K0 M0 t! t8 ?8 @6 g! u' ?$ t
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
5 R, i; p! H$ c7 O4 e* a9 X - <property name="dataSource" ref="dataSource" />
) f* h, l0 J2 E, l - <property name="mappingDirectoryLocations"> 8 [7 O3 ?, p) o1 ?, |7 ~& U
- <list>
' [% N/ J( o; L. P8 D$ j' u. j% l - <value>classpath:config/hbm/</value>
/ v/ c5 v- n8 N - </list>5 A5 \. ^( |& }
- </property> % ]8 e8 S/ L; g- o& H u
- <property name="hibernateProperties"> ' r9 P4 e) ?+ j8 {( |/ z
- <props>7 V9 `# ~; Y- | p9 d2 _9 ]3 `
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>' h' q! L7 @2 D9 A
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>% Y* n4 F; |3 I8 x
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
, z- r( B! K p+ v# D1 C+ _( V - <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->
3 L' ~ i: V \! S - </props> ) y0 b3 ]7 x& p* `: }/ e3 s
- </property> & k/ ]5 V( ]# n4 W/ F, R
- </bean>
+ b; H7 ?2 Q1 c1 b - 2 W. T3 S$ K) S( ^. o
- <!-- 声明式事务 -->
/ P; M ]3 Y; x5 F& u - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
) r- r! X. P2 w' M! g - <property name="sessionFactory" ref="sessionFactory" /> 5 B5 J. a2 D5 Y8 q
- </bean> 5 o4 E, `) Z) W3 k% T9 C
- * o. F7 R& w/ n0 s: B! T7 [1 J9 s# Q
- <aop:config>
v) O* h- F6 y* L8 y7 p4 l - <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" /> * W2 E) b/ w% l) w, Y2 u
- <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> : }4 ~7 _3 P" V3 P7 Z& j
- </aop:config> 7 y; b) Y7 d0 g8 o
-
7 Z8 u" @) l7 {: ~ - <tx:advice id="txAdvice" transaction-manager="transactionManager"> . Y/ j, E) o3 X9 W
- <tx:attributes> " K* }- Z' M1 Y- r2 {4 x* C7 k1 c
- <tx:method name="save*" propagation="REQUIRED" />8 G5 X* U7 v9 n9 a1 [+ f/ X6 I
- <tx:method name="add*" propagation="REQUIRED" />
5 z& f: o7 p8 P% v& z/ B$ ~ - <tx:method name="create*" propagation="REQUIRED" />
+ f0 r/ M% Q; q- F5 H - <tx:method name="insert*" propagation="REQUIRED" />
! c, C0 `' f# ^% L - <tx:method name="update*" propagation="REQUIRED" />
; H* s5 S1 `+ _& X) u, L/ W8 g - <tx:method name="merge*" propagation="REQUIRED" />0 i- o6 I: k! D9 n5 R+ k
- <tx:method name="del*" propagation="REQUIRED" />2 V( `) A4 M2 r' I" z$ ]4 ^+ Q9 a
- <tx:method name="remove*" propagation="REQUIRED" />: Z0 w- |) S0 a1 t: I' ~' m
- <tx:method name="put*" propagation="REQUIRED" />
) K3 T( Y( N9 y# S8 W) J' \ - <tx:method name="*" propagation="SUPPORTS" read-only="true" /> ; F1 v) ^) p+ G2 r, ~0 B8 X
- </tx:attributes>
/ }: K6 Y7 l1 @ T) X F - </tx:advice> ) }' t% ^2 [0 h6 U
- </beans>
复制代码 spring-mvc.xml:
/ C% |; z) H0 \- L, M) R8 T
% {/ D( \ e, A+ c6 F/ V- <?xml version="1.0" encoding="UTF-8"?>
$ C9 _. }, h3 L u' K - <beans xmlns="http://www.springframework.org/schema/beans"
; x5 x- n. x, p' a! U - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"* ]/ {" `% T2 [4 |: T
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
' Q! |2 f7 N% L& P: U* Z3 @/ }% h - xmlns:mvc="http://www.springframework.org/schema/mvc"/ m; D: R7 S; b2 y- D
- xsi:schemaLocation="http://www.springframework.org/schema/beans
2 o6 c$ P: I- g. U) n* O* ] - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
' R H! K+ o' @3 `9 }# }7 d n! | - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
' V+ s% I2 k* e \- W! ]+ j/ p - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd S4 A8 l, J @) O5 r; j
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd- h8 c' P+ K/ ?* ~8 x. g/ t
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
" R: G& C Y& I [' P6 v - <!-- 自动扫描的包注解 -->) y" x) v; ?- O/ n0 e' g7 V& p
- <context:component-scan base-package="com.hoodo.rci" />
, x2 p* o6 ?1 F% R8 u( Z y5 |% ?+ u# ~ - <!-- 自动注入 -->/ O7 _8 j! E8 f2 o- _
- <mvc:annotation-driven />. y& ~9 q k& f7 E1 B" H6 S7 P9 h
- <!-- 视图解析器 --> l! _: U4 Q: u1 i( `
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">9 A+ J, X) b6 S7 @; N
- <property name="prefix" value="/WEB-INF/jsp/" />/ D. \. p% O2 I8 r) T9 i# A: p" g
- <property name="suffix" value=".jsp" />' G# e5 ?! M- x9 w4 y! c) e' {8 Q
- </bean>
5 C% M7 o$ u' t7 w - # E6 y a$ Q9 L
- <!-- 异常解析器 -->% h, k2 o M3 `# S
- <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
" }9 [- r9 ^, h5 ^6 Q4 l7 H& z0 T( ^ - <property name="defaultErrorView" value="common/error" />4 w- l3 _% @ @& A% Y
- </bean> -->
0 A& N. T8 k1 {" B -
2 j3 F2 k1 G% v1 T8 \0 E - <!-- 拦截器 -->! t% @5 {3 |% K( F ]7 ^4 k, V
- <!-- <mvc:interceptors>
" Q% Y2 t" v2 P, l - <mvc:interceptor>3 E: x" R* t v5 {+ _
- <mvc:mapping path="/admin/*/*" />" P, ^2 W- i( X
- <bean class="com.filter.CommonInterceptor"></bean>8 ?; y; {0 f5 L- z4 w3 @ [( U
- </mvc:interceptor>
- t$ S% t! u4 W2 N D. n/ ^- b - </mvc:interceptors> -->! i2 w$ g( @% q$ a; s" b4 c
- ; K6 t3 L& f# `
- <!-- 上传文件相关的配置 -->" y/ S4 O# f+ b8 F0 C
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
$ l3 E+ U% R( g9 b, f' N - <property name="defaultEncoding" value="utf-8" />
2 |+ j3 T8 ^$ _2 Z$ k$ M( |9 k - <property name="maxUploadSize" value="104857600" />
4 n3 l9 N$ s; n, `' y9 { - <property name="maxInMemorySize" value="4096" />
. k. w8 D% b! H# D/ D7 k( S - </bean>
6 O/ ^/ S1 I" u - </beans>
复制代码
9 {! z* ^! y( K, ?( B" z D! p$ \4 \1 o8 ^. V# D
* @% x: p7 x5 y
SpringMVC+hibernate4.3+Spring4.1整合案例
! J& f2 ~7 [7 p0 d+ H4 [8 u: Z& j
|
|