TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
springMVC+hibernate4.3+Spring4.1整合案例spring.xml:5 T& }' C( x @' S
1 v1 x" l. y* A" c5 X0 R1 L0 [- <?xml version="1.0" encoding="UTF-8"?> . b6 `4 m/ e6 o( w9 [
- <beans xmlns="http://www.springframework.org/schema/beans"
4 K$ i! t6 u3 I W3 c5 V. I. C& W - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 w5 e) A5 V. h- N& U% V
- xmlns:p="http://www.springframework.org/schema/p"
: ^' ]) d ?# @( ?" p9 u0 n/ x - xmlns:aop="http://www.springframework.org/schema/aop"
4 L, ^5 ?* X4 \ - xmlns:context="http://www.springframework.org/schema/context"
6 j/ X, r4 V8 Z9 T - xmlns:jee="http://www.springframework.org/schema/jee" / S: }1 ]3 F+ `4 p) k a6 q, w9 K
- xmlns:tx="http://www.springframework.org/schema/tx"
! G3 i8 z; u0 o7 U - xsi:schemaLocation="
7 W3 S. g! D$ Z - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
# w3 K1 h; w* Q* G - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 5 P+ n7 Q6 d2 G& c
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
7 ]; A2 R. s9 q5 o a3 B! D3 H( I - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
N- m2 P1 y S. L! q - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
5 g' K8 k( j8 `" K, \+ ]# d -
: e* j( Y# o2 H& f9 @6 g - <!-- <mvc:annotation-driven /> -->" L7 Y: S v. d- b5 H( t5 @
- <!-- 【配置视图解析器】 -->
6 z0 u' _& l4 [, Y& r - <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 --> * a @* v7 D7 P% Y: B
- <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->
1 W1 U9 ]/ Y2 s; {" Q - <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
5 t: g2 ]% m {# W, f - <property name="prefix" value="/WEB-INF/jsp/"/>
+ a6 k% G, N ^! W - <property name="suffix" value=".jsp"/>9 ], u1 Q* C2 B8 \1 a
- </bean> -->
% y* S$ s. d0 @" h1 c* ~/ x9 V - i8 {, N1 ?9 j$ B# b8 I G8 j9 U
- <!-- 导入springMvc.xml配置文件 -->/ Y$ z3 H* M4 M7 y. }9 t
- <import resource="classpath:config/spring-mvc.xml" />/ k- ^& ]/ @8 C+ Y
- <!-- 加载数据库配置 -->
& S1 N) {: M$ R% D - <context:property-placeholder location="classpath:config/db.properties" />: g y( S R. g' J" B, `- ?
- <!-- 加载数据源 -->, L$ J+ Z4 x1 ? Y
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
" s$ Z4 N8 {0 F9 t - <property name="driverClassName" value="${db.driverClassName}" />! |- O! j7 E0 e2 P
- <property name="url" value="${db.url}" />3 e: W; a m# g# u8 T/ n
- <property name="username" value="${db.username}" /> |3 n6 L; o5 o4 ]& s5 R" F
- <property name="password" value="${db.password}" />
/ t: }0 }1 n. [0 _! U* n) X - <property name="maxActive" value="${db.maxActive}" />* S. ?! ^2 Y# e
- <property name="maxIdle" value="${db.maxIdle}" />- z% h+ C- Z& T3 V/ B
- <property name="minIdle" value="${db.minIdle}" />
3 G7 b# l. @+ V" s ~+ E# y - <property name="maxWait" value="${db.maxWait}" />, {% o. i" j( X+ `
- </bean>+ H* ]+ \! Z, B+ ^( h) X4 u
- * C5 s3 I' Y( z7 U
- 0 k2 U& }1 U3 W& D1 E
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
* x: j) `- `+ p5 Q; v3 M - <property name="dataSource" ref="dataSource" />
. ]: G# g) V1 ] - <property name="mappingDirectoryLocations"> - U. J7 y( l5 S. y; |
- <list>
3 O! R1 f8 I' t - <value>classpath:config/hbm/</value>
* T# b9 m" [- @& D: V+ _ - </list>, X$ a2 T6 f, M% J! U8 v
- </property> 2 f% N1 Z) [0 g, X8 X8 u; m% ^
- <property name="hibernateProperties">
' R. H( u( l; B8 y" l% @ - <props>) |' _* `$ Q: c' C; y
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>' t! {7 {9 S4 ~; F" E2 e& F# l
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
/ P, r0 M' ~) l# G9 B - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
! V. i3 }9 F1 ^- `) Z( P0 }& U1 h1 { - <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->2 t) e. X6 f4 z
- </props> % @1 Y' z4 G7 Q( K
- </property>
. h5 m! S" ]" P1 F - </bean>
$ j( m4 {* p) p h: A - ( S3 C: w5 Q+ s5 [4 B6 [5 Z& B8 `; \. F
- <!-- 声明式事务 -->
" g2 z; {) J+ I& J& w) F+ H6 L - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> / G4 l& s! u0 n
- <property name="sessionFactory" ref="sessionFactory" />
' B7 P; ^$ O: p" p - </bean>
" z( S9 Y0 p- y& G- W b, P9 S' v -
- t: A2 [) p4 F! W2 m* J - <aop:config> # ^; C+ B. a" ~$ p, I; ?+ U
- <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />
" T/ [0 a5 r7 \% T5 V. y - <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> * i k+ q3 P3 W5 ?# S( Y
- </aop:config> % c+ v- J# }/ r/ z2 Z
-
( ^6 ~( ]7 F ~* Q! r1 V2 o- r H8 l - <tx:advice id="txAdvice" transaction-manager="transactionManager">
7 k! a, ~' z: T3 P6 l4 M1 S - <tx:attributes>
- w \8 U4 L3 q8 O; f. @# t - <tx:method name="save*" propagation="REQUIRED" />2 o% @! u- D2 j
- <tx:method name="add*" propagation="REQUIRED" />. B) @4 l2 l- s7 D4 t
- <tx:method name="create*" propagation="REQUIRED" />/ r8 c% G# W! _# L
- <tx:method name="insert*" propagation="REQUIRED" />& O0 d' ^, |7 G1 G
- <tx:method name="update*" propagation="REQUIRED" />
* G. Q% F L. p# p! P8 N" Y - <tx:method name="merge*" propagation="REQUIRED" />1 v4 Z! n5 \3 t( S7 c+ ~- \7 F
- <tx:method name="del*" propagation="REQUIRED" />
% O. v/ K7 M- @9 ~, v. { - <tx:method name="remove*" propagation="REQUIRED" />
6 z7 Y8 B& T! _: T* k- P5 i2 K - <tx:method name="put*" propagation="REQUIRED" />
/ f% |6 E) v3 ~8 B" ^ - <tx:method name="*" propagation="SUPPORTS" read-only="true" /> & W% R' G: w7 o/ p
- </tx:attributes> ) M9 o! t) Q1 w1 V6 e
- </tx:advice> ; e: l+ w+ o- P8 [! `/ ~
- </beans>
复制代码 spring-mvc.xml:
. X I9 `" G! R, u& k. C
$ c- q! E/ ]0 v6 H- <?xml version="1.0" encoding="UTF-8"?>
! }1 H- r+ F7 t. N2 W" I - <beans xmlns="http://www.springframework.org/schema/beans"0 H$ y% G" c# L6 P
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
9 T. \1 @3 p& }9 U8 L2 W - xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
( E( h% L- x+ ^& Z9 ^ - xmlns:mvc="http://www.springframework.org/schema/mvc"
" X/ X: l+ Y# z9 \( F! E - xsi:schemaLocation="http://www.springframework.org/schema/beans z8 N+ r4 d7 r; ]5 T, e9 }( v
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 4 J" ^% k, V% o7 e% C3 P7 t
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ; Z4 A+ }- b& x" S! P. D
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 8 n( C) v: P V7 \: W) `
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
& n2 Q3 y6 L. z' r. F - http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
! @; N6 O, l! d - <!-- 自动扫描的包注解 -->) N: v2 S5 j9 ]! {# }7 F* ^. B
- <context:component-scan base-package="com.hoodo.rci" />8 o `8 r s- x D6 D+ H
- <!-- 自动注入 -->0 o1 v2 I: U! U3 H B2 R M
- <mvc:annotation-driven />
( B( J. K3 M% n# N" K" X - <!-- 视图解析器 -->( Y9 p9 U0 m' q6 t
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
& f; g1 `, @$ S' x/ [ x) z ] - <property name="prefix" value="/WEB-INF/jsp/" />. v8 `# h" H3 i. F* U5 u5 n5 p
- <property name="suffix" value=".jsp" />
+ A) j* A. D9 o" Z ?# W - </bean># y% z6 b- D1 q8 J* M+ R
-
9 {* h" N, K( \2 t7 p" w9 Z' `+ F - <!-- 异常解析器 -->6 `8 o E. K9 O7 X; p" X ~9 P
- <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
8 b7 f6 D( u$ C& s - <property name="defaultErrorView" value="common/error" />* f/ v& L% A1 x# z( [
- </bean> -->5 U5 y5 c7 ~9 W: y( o) X3 b
- 3 ~! k& p6 H; ~ @
- <!-- 拦截器 -->
9 n8 f' G# ~/ d7 J% | - <!-- <mvc:interceptors>" C8 s9 u7 m& X. @
- <mvc:interceptor>
( v0 L" E8 Z2 h3 r - <mvc:mapping path="/admin/*/*" />5 w4 o0 c% w, n1 X# q- B
- <bean class="com.filter.CommonInterceptor"></bean>
" J% |9 {4 W5 v. _% R( N2 h6 z( ?9 Z7 B - </mvc:interceptor>% Q0 e% ]/ W. ^% X7 {1 J
- </mvc:interceptors> --># l* }" L7 V! H2 \6 q* x0 o
: w+ z, j. d8 p; }$ B- <!-- 上传文件相关的配置 -->3 T' R: F! j$ b. k
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
' u8 u9 N7 \$ a" S - <property name="defaultEncoding" value="utf-8" />4 m) m/ b( u* K, O7 S
- <property name="maxUploadSize" value="104857600" />
: c! o& d/ o: \0 w - <property name="maxInMemorySize" value="4096" />( t9 k+ ~: E6 l6 X q- z0 I
- </bean>- T" I3 r6 s1 Q0 |( g
- </beans>
复制代码 ) I) \) _0 d b. l
5 i. L" L! C0 o1 k' M
! Q4 S3 v: H9 z2 [0 gSpringMVC+hibernate4.3+Spring4.1整合案例. R( |# R, I+ K
& U6 h s4 d) { |
|