TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
springMVC+hibernate4.3+Spring4.1整合案例spring.xml:
! Q' b8 a! A; o. h9 ?! \' o* j9 s( q" y) t* _1 |* z/ b5 W
- <?xml version="1.0" encoding="UTF-8"?>
7 f: t- K i( j5 f - <beans xmlns="http://www.springframework.org/schema/beans"
8 t2 d9 v0 m+ ]9 B! ? - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
0 \. {0 p2 t2 X/ B - xmlns:p="http://www.springframework.org/schema/p"
! c: ?. v% X5 ~7 v/ W; L3 @- k" C1 J - xmlns:aop="http://www.springframework.org/schema/aop"
# a' B$ n* z1 {- P: `5 e# M6 y$ k - xmlns:context="http://www.springframework.org/schema/context" " o& x8 O" q# q& {/ X
- xmlns:jee="http://www.springframework.org/schema/jee"
/ Y0 i b N6 l# S* ] - xmlns:tx="http://www.springframework.org/schema/tx" 3 ]4 p' e& ~. n% ?( M* n' ?- G8 A
- xsi:schemaLocation="
! k3 Z% c$ e, R+ H - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd % ?( X: `8 a7 N5 t7 t
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd . W) `7 K g& k% k
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
2 k0 b4 F- O& N - http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
$ c) s& v9 @. m3 Y$ P# g6 W - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
3 |1 |" V# U; D( F; ~, n - ' _9 P' f, c4 m
- <!-- <mvc:annotation-driven /> -->
z* F' f% {1 j* g& w9 z - <!-- 【配置视图解析器】 --> . h4 G' z) N" D
- <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 --> / L. F$ U" ?6 e7 D5 a5 H
- <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 --> " J$ W* J* q* P# A
- <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
9 h# Z! y, n/ W - <property name="prefix" value="/WEB-INF/jsp/"/> . L3 v' {% ?9 U5 a$ ~) l/ |
- <property name="suffix" value=".jsp"/>
: s7 h4 }1 G" Q- R" A: _- u - </bean> -->
$ t8 Y, g! q3 g; | P! D: o; D -
% Q1 e* Z- p4 b - <!-- 导入springMvc.xml配置文件 -->4 t- B" K6 d7 x' P/ x
- <import resource="classpath:config/spring-mvc.xml" />
9 g" e, o6 @& S" t6 } - <!-- 加载数据库配置 -->
2 b# l+ z8 B% y) [3 J# [ - <context:property-placeholder location="classpath:config/db.properties" />
6 p6 U$ W* y' g% G- E - <!-- 加载数据源 -->" s5 a9 R' `2 q) `: q* u6 \, W
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">1 T3 s+ y$ t5 j; V
- <property name="driverClassName" value="${db.driverClassName}" /># r9 i' j x, _4 u' |' L
- <property name="url" value="${db.url}" />. d5 S# v. Z9 h" N
- <property name="username" value="${db.username}" />4 M+ i9 J0 g2 h
- <property name="password" value="${db.password}" />5 Q0 H& X. W1 D8 f
- <property name="maxActive" value="${db.maxActive}" />1 _3 C- v' p. F
- <property name="maxIdle" value="${db.maxIdle}" />" z6 b7 u d7 ^/ h" F t2 }* \5 J
- <property name="minIdle" value="${db.minIdle}" />
5 u5 b4 r: o( Z ~; P2 k6 t4 t - <property name="maxWait" value="${db.maxWait}" />
8 b6 M9 r8 {$ W8 t - </bean> [8 ]7 {9 J, Y. \7 h7 |# y3 F6 r
- * S' \0 v7 q' l5 J
-
4 g: Q0 Z0 J, {; Z- V - <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> ' X' p/ ~0 @7 J! T \+ i; i
- <property name="dataSource" ref="dataSource" />
1 ]$ U) W. n) }1 e9 T& f - <property name="mappingDirectoryLocations"> 6 i. i' ^! n2 |& Y8 b$ v/ H8 ]
- <list>
+ p$ G* _* t' j# u - <value>classpath:config/hbm/</value>
c8 k3 P1 g+ C5 I4 x - </list>. y) H0 T7 K d
- </property>
( i2 p. g* s% ?/ k: \& j - <property name="hibernateProperties"> s1 P7 c8 X0 ~6 a& N
- <props>
0 g: b" Z: m3 M - <prop key="hibernate.dialect">${hibernate.dialect}</prop>
5 E& M3 `* \/ R; \4 @ - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
& F& L6 d. v% f7 W2 P* s - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
0 `% B! Z/ i% N" O6 ` - <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->
3 k* R7 t# E! j1 _1 t - </props>
6 X2 N H0 f8 r3 O1 V/ V# w9 o, U# R - </property>
1 v: y: |% T& @" D' D1 e9 n - </bean>
' c F" U( |3 C" a2 I/ r, j9 ? - " J( W" h- T& @% q" a4 @) a
- <!-- 声明式事务 --> / E0 z- q- c9 e9 }1 o& D
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
/ D; V) v: w \# E* o* S - <property name="sessionFactory" ref="sessionFactory" />
1 K* A( h& D8 K' }6 [+ ~. ^ - </bean> " t+ s- c, K2 b* E7 B
- 6 J9 p: v/ f5 S3 P) _# d
- <aop:config>
" Z' N F) D5 {% e" ~$ u' n) b" c - <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" /> # P) ^: Z) v" s
- <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
7 W' @6 x. \: f3 a - </aop:config> ! T& G' B! Z% S; m. L/ g' q
-
) ~( Z! @+ U. f$ z, j1 B - <tx:advice id="txAdvice" transaction-manager="transactionManager"> $ f" c( k" ^+ w! j$ N) z
- <tx:attributes> : D4 C8 u1 v5 o, j
- <tx:method name="save*" propagation="REQUIRED" />
5 I7 m# N: o2 b! K& M$ T7 H& P1 k7 _ - <tx:method name="add*" propagation="REQUIRED" />% z1 L4 k6 V( f. m
- <tx:method name="create*" propagation="REQUIRED" />; I1 ^. P6 k0 `+ Y3 O: b
- <tx:method name="insert*" propagation="REQUIRED" />
: a# `4 v# x$ D) Q; F/ S! l( G: U - <tx:method name="update*" propagation="REQUIRED" />: D$ N* Z9 f: B5 k8 T) F" F* X/ P
- <tx:method name="merge*" propagation="REQUIRED" />( N- p8 g6 s( d6 Y/ S; |% f9 [3 i- f# [
- <tx:method name="del*" propagation="REQUIRED" />) [4 Q/ `, _( m$ _: p
- <tx:method name="remove*" propagation="REQUIRED" />
# z1 v8 U8 c4 v3 F8 h2 _ - <tx:method name="put*" propagation="REQUIRED" />0 Y- k8 U* }! F5 Y$ E% E
- <tx:method name="*" propagation="SUPPORTS" read-only="true" /> 5 Q o9 \2 n+ p* p' ^( ~
- </tx:attributes> ! G& J; |* R8 `! b+ N- f/ M
- </tx:advice>
* s" I( X3 U# c# J - </beans>
复制代码 spring-mvc.xml:/ H; O7 C( A5 v$ C
6 G# K- {. K& z! w
- <?xml version="1.0" encoding="UTF-8"?>
) P& n% x; Y& P6 M! q8 o1 ~3 S - <beans xmlns="http://www.springframework.org/schema/beans"9 Q4 X) s6 I1 Q" Y! Y1 X6 n
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"/ f! ~7 m4 ?& j, |+ R
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"/ s1 f5 ` ~$ |: V7 {- ~
- xmlns:mvc="http://www.springframework.org/schema/mvc", P) P( D+ |) p! k, t4 e- w
- xsi:schemaLocation="http://www.springframework.org/schema/beans / L& C; r; W7 s7 O6 ^) r
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
* Y6 q9 y0 O8 b" S5 V - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd / v: V. e) P! P: ~. E2 e
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
" O. ^9 R& [. g0 X - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
9 ?" a: J* J1 a) Y2 A - http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
8 l+ n# S' n+ D; C1 F9 L. o. X6 j - <!-- 自动扫描的包注解 -->, F' W1 w! @& X( ?. M+ }
- <context:component-scan base-package="com.hoodo.rci" />. M; h1 P7 P! ?3 R1 ?/ E$ D/ L+ }* f
- <!-- 自动注入 -->5 ]2 h4 F, Y) |9 Q3 w% g
- <mvc:annotation-driven />4 X+ k; u8 ~/ K' {' T" {! p: F
- <!-- 视图解析器 -->
K. S" G: Q' c) W/ h2 W: a+ A - <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
Z' ~0 {! z; z' C1 q! e - <property name="prefix" value="/WEB-INF/jsp/" />; {# w3 G, b% S. y0 j
- <property name="suffix" value=".jsp" />. J6 |0 D d+ f, z1 v6 {
- </bean>! ?* e0 m9 b0 a; P& K, ]# _2 T
- 1 K& Q( i; l% |( G! G2 a7 J5 H
- <!-- 异常解析器 --># Y2 ^0 t9 |$ [2 c8 H' h
- <!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">9 @, R6 s" U, j3 F' ?+ Q
- <property name="defaultErrorView" value="common/error" />+ s# ?# ~) [5 ?& t: d# D3 W
- </bean> -->
3 d; k! c) X6 e( W* c9 j1 x, O -
6 |2 j/ ^7 k* k- t - <!-- 拦截器 -->
# x) M8 h5 F! s. e1 v a6 _) g - <!-- <mvc:interceptors>
- g' G% F1 Q' \, ^- j& h' p/ u/ _ - <mvc:interceptor>! A! J2 h4 D6 W7 s, w K! V
- <mvc:mapping path="/admin/*/*" />
8 [; E# Q* G5 V" j" o! U$ `1 p o - <bean class="com.filter.CommonInterceptor"></bean>0 Q& g1 j+ q8 y" q$ ]; `
- </mvc:interceptor>( q$ e* W$ b* S" F; w$ v
- </mvc:interceptors> -->
( Q, X4 L2 @! W8 d% }2 Q - 1 C+ Q1 Y0 F6 U% J9 R# d
- <!-- 上传文件相关的配置 -->' N. F3 x& d: E! l* I
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
2 p$ n& ~' n- P9 t/ T- H - <property name="defaultEncoding" value="utf-8" />
: K9 t5 P( ~. Y, \0 f8 p - <property name="maxUploadSize" value="104857600" />
! P3 [3 U6 N: N" m2 L7 j* L - <property name="maxInMemorySize" value="4096" />" o6 l& u$ Q) E+ V/ _+ a; O
- </bean>
% s+ e# J" T' a; ^9 Y) _2 Y9 Q S - </beans>
复制代码
' \5 z) t \$ \) {
a+ F2 ]8 `; t2 u- _% c7 Z% q' c/ h/ S% s( W" h, F n
SpringMVC+hibernate4.3+Spring4.1整合案例5 }7 c! R6 z A! J
5 w- W4 ^7 G, Z$ J5 t
|
|