TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
springMVC+hibernate4.3+Spring4.1整合案例spring.xml:) |) O# W: H0 z, p- w& l' e) j) F
$ s1 Q5 f: q7 z. K U6 x! R( q8 Y
- <?xml version="1.0" encoding="UTF-8"?>
( P# u) b( K, K4 R7 L+ o - <beans xmlns="http://www.springframework.org/schema/beans"
$ a# n0 r5 d" l$ ?( z - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
; o% t( k, T+ r% p - xmlns:p="http://www.springframework.org/schema/p" & b" C6 X+ [( E) ?2 y x# p. h5 {9 E1 Q
- xmlns:aop="http://www.springframework.org/schema/aop" 7 H& R K6 D5 r9 ?. B1 Y$ f9 |% v
- xmlns:context="http://www.springframework.org/schema/context" 5 Z1 W3 B) b/ g
- xmlns:jee="http://www.springframework.org/schema/jee" ' `7 V5 C. Z* Q0 i; ~
- xmlns:tx="http://www.springframework.org/schema/tx"
; K: c% p$ c- E - xsi:schemaLocation=" 4 k2 {2 @5 X4 @- p- ^7 s0 |. F
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
1 D% u. t7 V6 {/ e - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- b. D3 g7 Z: m- X9 ^5 c - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
: `4 j% [' a( K% d# @# G+ \3 z' Y - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
0 S0 o5 r- Y3 R! ] - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
7 t& |# |7 y& h% `) e" @% @ - u/ v4 |% P/ o8 {
- <!-- <mvc:annotation-driven /> -->
) z+ s5 e' o/ D+ R5 {( l! w - <!-- 【配置视图解析器】 --> + \; G1 R, |; l6 H" d1 ^
- <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->
[. |4 w5 `! a! I& w - <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 --> 6 V2 D9 o" r9 L+ _" E2 _
- <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
: c# k+ y- ^0 c8 ~9 I7 ~# { - <property name="prefix" value="/WEB-INF/jsp/"/>
/ D2 u2 I5 \6 \5 G - <property name="suffix" value=".jsp"/>
* v0 _6 K$ `% W+ P, @+ \% z5 B - </bean> -->& C; q" q$ W+ g5 G" u0 d2 F
- 7 X3 {. r+ W- ~( {! U
- <!-- 导入springMvc.xml配置文件 -->
! k- _1 N7 c3 z! f1 R. R1 x - <import resource="classpath:config/spring-mvc.xml" />
7 ?' }/ T; }7 D( O - <!-- 加载数据库配置 -->
' C" r' y; G# b9 r - <context:property-placeholder location="classpath:config/db.properties" />
% K) x* k/ W4 P - <!-- 加载数据源 -->
7 J$ b" J3 ?8 M8 x - <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">. |: p! T% i! a5 H9 Z3 W7 j
- <property name="driverClassName" value="${db.driverClassName}" />, ]% t- y3 n$ C
- <property name="url" value="${db.url}" />
: |; L1 E( G# s% v - <property name="username" value="${db.username}" />
1 P# d* r! W8 l - <property name="password" value="${db.password}" />" t3 ~1 B* ~8 N! C( `; u/ q
- <property name="maxActive" value="${db.maxActive}" />
U, A& W; F. ~4 K% z - <property name="maxIdle" value="${db.maxIdle}" />
7 g4 s* K3 F3 I9 v N - <property name="minIdle" value="${db.minIdle}" />
3 E: ?1 j3 i& r8 ~, i* H' P - <property name="maxWait" value="${db.maxWait}" />1 C/ O# f; u; E+ g
- </bean>
: I+ `3 [9 d& O0 c2 B: q1 ?+ ^ -
* \' K. m7 S" U, C# P( d) _3 h+ J) o9 h - : \& y+ V1 |# n3 E1 W! X e& I
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> ! l( Z$ ?, |! w( q7 }
- <property name="dataSource" ref="dataSource" /> N+ y4 _% C8 Y
- <property name="mappingDirectoryLocations">
9 m% q( U' Q$ ]+ S: _: N8 e. _ - <list>
9 b& Z" J/ i3 }/ l- J4 @ - <value>classpath:config/hbm/</value>
/ F- I) h, ?' l: h - </list>" c! U: w7 _0 X" z" _7 k' E
- </property>
1 \: }: S! O8 i5 G3 a5 W - <property name="hibernateProperties">
2 a1 b/ y, T [4 M - <props>
# a6 t2 l) o" ^+ _ - <prop key="hibernate.dialect">${hibernate.dialect}</prop>
4 o) u6 I$ T3 z; R" x - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>- G( f5 x$ k) { @- c2 h
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
; u) C) ?+ d8 b& D - <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->
2 ]. q" a, V) m5 f - </props> ) | @% p- E" Z+ f' x1 i& C% ?- Z
- </property> % O& F) H. D7 [, n3 y
- </bean>5 K9 W2 n3 S- o/ m4 \* S. B
-
! @- m: \. \, B0 h) n1 R7 Q+ v$ |- P - <!-- 声明式事务 --> & V5 H( J5 G1 X0 e9 c) Q
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
% v; [2 [5 F3 Y - <property name="sessionFactory" ref="sessionFactory" /> 9 B) N8 y* l9 H6 Z) o+ e- G
- </bean>
; P( k" X0 M3 a' j( i - : H2 F( F$ w- \% H- l
- <aop:config>
* p0 s7 r/ a" S/ t }( ? - <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />
( j- b8 x7 h& r( {. Q* a' O4 _ - <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> 5 m/ c: u' }4 _8 s* z H! B; U
- </aop:config>
2 m: f9 p9 c1 _. z7 a7 v -
& r- i' M& C* z1 p - <tx:advice id="txAdvice" transaction-manager="transactionManager"> # h* [3 ?0 }; \. L# [& {: ^ ]! ^
- <tx:attributes>
" O0 @- h% k ^! U% V - <tx:method name="save*" propagation="REQUIRED" />
1 \- O+ J5 a9 n8 \6 N( B - <tx:method name="add*" propagation="REQUIRED" />( d, S7 u0 d# U/ a) ?! m
- <tx:method name="create*" propagation="REQUIRED" />
$ u7 j/ I, m& _0 D& k3 B1 ] - <tx:method name="insert*" propagation="REQUIRED" />8 M; F8 w5 E' T$ {* Y
- <tx:method name="update*" propagation="REQUIRED" />) d# ?1 Y6 S% B2 c
- <tx:method name="merge*" propagation="REQUIRED" />
9 t; }! B* \, D6 [. a/ F9 [: I - <tx:method name="del*" propagation="REQUIRED" />
( b/ ?* R7 O( ~5 W - <tx:method name="remove*" propagation="REQUIRED" />
: L& A" d# v5 K+ p# x - <tx:method name="put*" propagation="REQUIRED" />
( h2 P1 b2 G, x' j# Q, @ - <tx:method name="*" propagation="SUPPORTS" read-only="true" />
5 x$ v% f4 @" K* [: v - </tx:attributes>
, e* P2 v0 B, J, Q: C - </tx:advice> - G7 K: Q& j9 ]- d
- </beans>
复制代码 spring-mvc.xml:/ u, s- g. P& s. v- B4 j
5 v7 H8 m) a0 Q( p5 }" m- <?xml version="1.0" encoding="UTF-8"?>- a- D% _/ T9 N# Y8 ~3 l5 K
- <beans xmlns="http://www.springframework.org/schema/beans"1 s5 l7 u# H7 |6 @
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" A- y+ _4 ?& E) M0 ?$ v
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
/ p9 |& E8 O% z$ _, q# t' M- S - xmlns:mvc="http://www.springframework.org/schema/mvc"8 y; p) T( _. R! K
- xsi:schemaLocation="http://www.springframework.org/schema/beans
3 O2 w, V/ _1 ^2 }3 L0 }8 w1 L1 D3 _; C - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 2 ~3 b$ y! ]$ [* m0 S
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ! d; C; s# [3 }
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd " |; J& F& F4 B$ G1 ~5 \. P
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
- p; R. s4 W+ N6 @2 s - http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
9 d' a- X8 ^# f* i* X, h - <!-- 自动扫描的包注解 -->" \0 i/ m0 M% u. p
- <context:component-scan base-package="com.hoodo.rci" />
* a: c1 P7 e+ z3 U1 ]3 Y8 f. [ - <!-- 自动注入 -->! S; N* \* k. y
- <mvc:annotation-driven />
; L9 O' Y$ k4 T3 G# b( a& I2 i J; x8 ]# N - <!-- 视图解析器 -->
2 B7 l# J5 T7 H - <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">* u/ E+ M* T& ?" {
- <property name="prefix" value="/WEB-INF/jsp/" />
- \$ C3 s9 D' [8 n4 ?$ X - <property name="suffix" value=".jsp" />5 J) P& W/ `) E5 m1 ^
- </bean>
4 }0 T1 o$ z" I6 C - ( @5 c5 n* x+ J5 X: E$ Q+ ?, }
- <!-- 异常解析器 -->
! ~8 k; @9 L/ Z# e5 y/ q2 x - <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
, d6 Q% j2 `7 L2 h8 C) {# D - <property name="defaultErrorView" value="common/error" />" M7 l+ h& ^" b5 e, o+ _* C
- </bean> -->6 i$ e t# `/ ]" Z4 z D, Q8 S
- $ J& p: H3 y i1 z
- <!-- 拦截器 -->
0 {: f4 _# h% N- P: B, u6 d - <!-- <mvc:interceptors>$ G( v' Z& [- a! ~% ~* R
- <mvc:interceptor>, Y% B) d5 t+ G6 w7 N2 k
- <mvc:mapping path="/admin/*/*" />. E& d; X& E* T6 k
- <bean class="com.filter.CommonInterceptor"></bean>
* Q5 J1 o7 h* c3 P- l F - </mvc:interceptor>
* m; q; x' ^! k( e: y0 \3 ] - </mvc:interceptors> -->
2 ?: Z& ^# M2 o( Y0 \" t( E. M
l3 ]% E1 ~0 s4 T( ?$ {3 l- <!-- 上传文件相关的配置 -->% a1 k# ?+ M t- ? U' ]0 @
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">; R8 Z' K' x0 C$ Y5 C; g
- <property name="defaultEncoding" value="utf-8" />
7 n0 R3 m7 U+ S& w+ c0 Q, B - <property name="maxUploadSize" value="104857600" />3 r, q+ s" H0 k& ]' z6 o
- <property name="maxInMemorySize" value="4096" />0 l- _) Z$ C2 m+ W
- </bean>4 s$ N9 ?/ G: V6 G8 M
- </beans>
复制代码
m6 n- K3 ?/ d! I7 [
4 B' b- h9 d4 U/ E: e" v. `3 G; W# G! j9 A! Y" B; k. u5 ^
SpringMVC+hibernate4.3+Spring4.1整合案例. q! n4 [& m9 L+ x! e6 |2 \+ P
5 a! D) I( o" Y0 ?: u5 W |
|