TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
springMVC+hibernate4.3+Spring4.1整合案例spring.xml:
2 F. ?( a8 D( {5 z9 C
2 l- |( q: z W- <?xml version="1.0" encoding="UTF-8"?>
' |7 t0 W( n/ u4 c - <beans xmlns="http://www.springframework.org/schema/beans"
* }# a9 i+ h( z+ S: t9 f3 a; M2 O - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" # I4 L, I8 }: e7 a% v: `, @
- xmlns:p="http://www.springframework.org/schema/p"
6 w0 I0 C$ {) X# C6 z6 d* W$ q7 ^ - xmlns:aop="http://www.springframework.org/schema/aop"
5 c' u, R& K0 N- f; M( |1 S - xmlns:context="http://www.springframework.org/schema/context" & K0 {% N4 m$ i
- xmlns:jee="http://www.springframework.org/schema/jee" 9 s& z. J T5 Q3 Y4 i g
- xmlns:tx="http://www.springframework.org/schema/tx" 1 Q2 U3 c4 v2 \2 q
- xsi:schemaLocation=" * ^- _- d% p: r$ o! G3 W
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
4 y. t' Q) U8 R9 w - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
* s- [ X' Q9 B* q+ S8 i# f - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
# T8 Z* z/ ~" c - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
' ]( Q5 r7 F* [$ b- A - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> ( T O( S6 c0 B& M1 _1 b4 p
- 4 k- k7 O4 R0 g
- <!-- <mvc:annotation-driven /> -->
( c" Z9 R0 _2 o" j# @* h" ^7 m - <!-- 【配置视图解析器】 --> : b) j# ], T0 B+ Q5 d% r+ ]
- <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 --> - R- v, U h8 g
- <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 --> 8 L$ L) J: U. F$ a1 X
- <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
9 }5 `. j i$ y - <property name="prefix" value="/WEB-INF/jsp/"/>
' Y) G# S5 {: G8 y5 i - <property name="suffix" value=".jsp"/>9 _$ ]. v# A H+ a$ A
- </bean> -->3 l/ ]! b \: G1 P6 m, Q7 _0 E
- 4 i: e8 ` r. T4 f
- <!-- 导入springMvc.xml配置文件 --># i9 ], ~" ~" T" `
- <import resource="classpath:config/spring-mvc.xml" />
: I$ \; A- j- l8 ]" G4 B5 [$ B - <!-- 加载数据库配置 -->
: n8 E( T8 A/ u$ C& G5 A - <context:property-placeholder location="classpath:config/db.properties" />3 i$ @! m0 k, E9 u6 w3 f; i+ O+ G8 |4 V
- <!-- 加载数据源 -->' K+ U1 b& s# k/ x1 X9 U
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
6 U8 J+ ~0 I' g% z0 d+ c" K5 e9 G' Y - <property name="driverClassName" value="${db.driverClassName}" />& o7 ?) D. v" V9 e9 `
- <property name="url" value="${db.url}" />
- }+ I' s4 M N9 a/ z( l+ Z: l - <property name="username" value="${db.username}" />1 _' S, X X6 m8 e$ e2 ^
- <property name="password" value="${db.password}" />6 W: x6 E9 ^7 Q; n; c6 W
- <property name="maxActive" value="${db.maxActive}" />
4 o+ p% Y# `, Z- y6 S7 x - <property name="maxIdle" value="${db.maxIdle}" />9 S' H6 g9 H, ~* X; T4 I
- <property name="minIdle" value="${db.minIdle}" />
. m0 Z/ k# s' ]0 Q5 V& h; R - <property name="maxWait" value="${db.maxWait}" />
$ |% y' ^4 w% n. u5 N) H9 A& D, Z - </bean>! u ~1 r7 O. { l
-
% C" ]6 g1 N3 O2 n! M% v5 | -
5 c8 H ? i" Q+ s4 m - <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
, j: T# T5 b" K: t3 \( _1 O - <property name="dataSource" ref="dataSource" />
5 w- j: a# E# e. x5 p N - <property name="mappingDirectoryLocations"> : C, V. z, U" d& J! n: q
- <list>8 s: b3 S- Z3 D! W" Y& D
- <value>classpath:config/hbm/</value>
3 k! }( y9 E! u F( X, t - </list>% m$ H, y3 O( r' @6 y) n* n' w Q
- </property>
6 n9 m, z2 f/ F9 g* J2 O - <property name="hibernateProperties"> ' s( m! O2 k) U- L4 w8 J
- <props>! M" E" J7 I6 X
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>
6 R, i" G; _, t; S$ h - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
* o8 u! p1 Y8 X1 k/ T/ d% p* ] - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
3 s# _5 u1 F( y0 c - <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->! ^1 ~" e$ M9 W6 T$ @$ M/ ]
- </props> 2 Q2 P7 H( t3 c% \
- </property>
7 Z7 X- d2 X1 ^' i. l$ D3 S - </bean>
& O1 ^" ^% L/ z. ^ -
+ O ~/ V% t0 G- {6 H& ] - <!-- 声明式事务 --> ( Z9 p8 D/ D |7 c6 U
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
& O4 S" m$ l4 t - <property name="sessionFactory" ref="sessionFactory" /> ( W, Q& a% F+ T3 q
- </bean>
, o" f3 L5 z! ]9 ]- x - ( k" F: `& \; n( T& k
- <aop:config> % i6 {* H; F( l6 G
- <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />
9 G; d% l- r9 s7 F - <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> / A/ I! }& k2 n! E) X4 S/ ^
- </aop:config> ) z$ K7 R- ` | u# Q! x
- 8 G. y5 c8 ]& y$ s' m4 Y
- <tx:advice id="txAdvice" transaction-manager="transactionManager"> $ E' }# V7 w" R, K2 y. w
- <tx:attributes>
. E& P- v6 ?9 w6 o9 ^! A - <tx:method name="save*" propagation="REQUIRED" />7 U- ?% A0 u# E% B0 F" v
- <tx:method name="add*" propagation="REQUIRED" />
' o" n6 P4 l8 i& R% } - <tx:method name="create*" propagation="REQUIRED" />- s, U/ _0 h w ~( s) W' J4 D
- <tx:method name="insert*" propagation="REQUIRED" />( l7 v9 h- o" ~9 {$ P
- <tx:method name="update*" propagation="REQUIRED" />
- w- ?8 ?2 D$ h k3 k - <tx:method name="merge*" propagation="REQUIRED" />
, s( Y: T- @% R& L: { ` - <tx:method name="del*" propagation="REQUIRED" />/ Q! Y! M" a5 K! _' U3 E; e
- <tx:method name="remove*" propagation="REQUIRED" /> o4 E! ^* A8 ?) a
- <tx:method name="put*" propagation="REQUIRED" />6 K# U" s2 U* K# O- ]- S
- <tx:method name="*" propagation="SUPPORTS" read-only="true" />
9 Q( \4 M& e M5 R# n' {: \ - </tx:attributes> 3 q- p8 V5 U1 P- o) o5 G0 V
- </tx:advice> 8 p8 L0 S3 } f$ ~- r7 A! Z
- </beans>
复制代码 spring-mvc.xml:
8 F1 m6 E4 }- u. U4 n: j/ N- r" I/ n1 x4 _: E, @
- <?xml version="1.0" encoding="UTF-8"?>
6 n) s/ }" i4 K; H2 Z- T - <beans xmlns="http://www.springframework.org/schema/beans"
" \! Q. j' Q2 W4 k. ? - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" ]. C+ b0 B$ l0 m' N
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx". ?8 Q* v) K- s/ ~5 r Q" L: ~' a
- xmlns:mvc="http://www.springframework.org/schema/mvc"" B, X+ T4 t2 H9 j* ~
- xsi:schemaLocation="http://www.springframework.org/schema/beans : v" Q* f1 z1 V6 u! G
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 4 s+ y3 m( l Z6 B! A# `
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
$ O: ?& F0 F$ z; b5 h - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
* ], A+ J' Z1 u/ c1 V7 |) d - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
' F* s9 }: Z. ~) g7 s+ s% |, N+ y - http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
- r3 l8 a( D5 n( i# A - <!-- 自动扫描的包注解 -->
% W P- `# W0 u3 L! p. Y - <context:component-scan base-package="com.hoodo.rci" />
- f R2 }$ E! g/ x. X: l3 L - <!-- 自动注入 -->
& c7 \& Y+ H6 J8 o* i! {" J+ Q" p2 T - <mvc:annotation-driven />
p) V$ D5 V2 q8 q8 C- }( z* a5 ~ - <!-- 视图解析器 -->
& K' `+ A! r8 J7 Q - <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">8 S5 q+ b' u; |! H/ d
- <property name="prefix" value="/WEB-INF/jsp/" />
/ X! g: B- P8 H" [ - <property name="suffix" value=".jsp" />1 n& e2 m( @: L
- </bean>' j6 p7 [4 F& q, L( ~' c7 H
- - i( ]/ X* m. P5 |7 y
- <!-- 异常解析器 -->
: o& w0 Q' E. D: N O5 X - <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
2 d2 \3 L/ { f+ \ - <property name="defaultErrorView" value="common/error" />
* j- R* b/ ?" Y. M4 ~ O - </bean> -->& }5 s: s, K) U5 E: T- K( b1 m& K
- 6 t2 ^ q& }7 W3 m; V
- <!-- 拦截器 -->; _7 H+ ^9 O* S" |# f; r
- <!-- <mvc:interceptors>
# F$ q( R( k; k6 m - <mvc:interceptor>
4 Y# o5 O9 E2 B - <mvc:mapping path="/admin/*/*" />
( C4 T" f/ B+ p! y R - <bean class="com.filter.CommonInterceptor"></bean>3 a: P# B" Z6 H4 A% Z, y& ?
- </mvc:interceptor>( U5 I( @- d2 K+ h, N W9 y
- </mvc:interceptors> -->
2 `1 z" } B6 ~! s6 E
- q4 R/ Y3 R5 V) u' Q9 ^' y) R- <!-- 上传文件相关的配置 -->
/ T+ A4 |! \" C& n: g0 X- L z0 G3 F - <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
# ` I& I7 U8 R - <property name="defaultEncoding" value="utf-8" />$ ]1 b- L, z( K- O% y% o
- <property name="maxUploadSize" value="104857600" />. X! W( P; L/ Q- Q9 B
- <property name="maxInMemorySize" value="4096" />
" B8 m. _; K5 l c% ?9 x/ W$ f. k - </bean>$ ~- |, n3 M! R! f, Z" w' ^/ i& b
- </beans>
复制代码
& o2 e l7 g# l, k! e
* M$ d+ p6 V( ]" k" e( @
! \5 c. _3 F6 V6 qSpringMVC+hibernate4.3+Spring4.1整合案例
5 d! r; u6 t9 m- ?; n2 K# `
# q/ h6 U% G7 V% ?3 Z |
|