TA的每日心情 衰 2021-2-2 11:21
签到天数: 36 天
[LV.5]常住居民I
spring MVC+hibernate 4.3+Spring4.1整合案例spring.xml:4 u% H3 j$ E, L. `5 p& D
! Y6 S% u7 y$ Z4 f+ H& F
<?xml version="1.0" encoding="UTF-8"?>
) x( ]9 t; b8 W: ]5 J <beans xmlns="http://www.springframework.org/schema/beans" ( n) p7 v# L" u( R; Z- B, ~
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
# u, c6 Y+ Y& E" N S7 W xmlns:p="http://www.springframework.org/schema/p"
8 w$ {. \* l, e s xmlns:aop="http://www.springframework.org/schema/aop" ; W- _& `3 X+ S+ l5 w6 s2 ]! K
xmlns:context="http://www.springframework.org/schema/context"
+ Y/ B. k4 h- {7 W! a xmlns:jee="http://www.springframework.org/schema/jee"
2 ]5 F& D6 i7 V" N0 A' ^ xmlns:tx="http://www.springframework.org/schema/tx" t& j* R4 L2 O, k3 H4 h% S
xsi:schemaLocation="
, G; r9 L+ p6 h' D' u http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
9 g; x6 d6 V- g! E$ P3 K http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
& V2 ^3 _; U1 t; R1 L http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd % ^4 E0 k$ {& m- D0 x
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
, W, p2 T* h8 ^* {+ `4 I# ` http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
% F2 p, t# H) P4 h9 Q8 _4 G
5 ?& z* X; C7 L0 |. B% A$ t <!-- <mvc:annotation-driven /> -->' e `2 Y% ]( ^- q& a% ?
<!-- 【配置视图解析器】 --> 1 _6 \& e2 v$ L& y
<!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->
/ f& p' [' Z$ I& I% u <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->
& b0 P; L# x8 I6 ?$ ` <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
9 }) N$ f8 i/ M" o: Y$ o <property name="prefix" value="/WEB-INF/jsp/"/>
: d' x6 @+ v5 T' ?, ^2 Z9 i <property name="suffix" value=".jsp"/>
( ? l9 G$ A* h/ v& Z2 a" f </bean> -->
: c+ b2 F7 m+ o
* D; h' P- w/ x" p r <!-- 导入springMvc.xml配置文件 -->: F6 c) y( U8 U$ K* S
<import resource="classpath:config/spring-mvc.xml" />* ~, _; g0 H2 I/ _* v7 c* u
<!-- 加载数据库配置 -->& R. R! F9 l8 B; D
<context:property-placeholder location="classpath:config/db.properties" />
' _2 Q* _. u" z' M; y( _7 |, C <!-- 加载数据源 -->
3 |1 R) F- N2 q) `0 e5 e* q. ^1 q <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">& y3 k8 {, N7 }! [2 ]$ e
<property name="driverClassName" value="${db.driverClassName}" />
3 _( [6 o+ n# Z( r% e: _; ?% f9 ^ <property name="url" value="${db.url}" />
1 ~$ ~' u* ~, f; v# Q/ R <property name="username" value="${db.username}" />
! A) Z2 n' K" j8 Q: G& K <property name="password" value="${db.password}" />9 R- Y. T: _1 }6 ^
<property name="maxActive" value="${db.maxActive}" />- {0 L& l& a0 r6 A! ]; s
<property name="maxIdle" value="${db.maxIdle}" />
& g. L/ \. A1 M- g <property name="minIdle" value="${db.minIdle}" />3 N+ v: p( o8 ?% Q8 W, z& C3 B
<property name="maxWait" value="${db.maxWait}" />
" P8 L3 \- L- s7 U7 K: f& C </bean>
2 ?1 B2 R5 i. J3 y& O4 U4 h , |1 k7 N: J& p+ X! M
7 V0 y `7 @- z7 ` <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
+ c2 R: w0 G3 B- L& u <property name="dataSource" ref="dataSource" />
4 ~) W; V$ ]/ I2 u <property name="mappingDirectoryLocations"> 0 a& H2 Y* Z l, E' ?
<list>
7 N8 P0 m. G" d1 a; `% R' ] <value>classpath:config/hbm/</value>0 O4 a- W w+ B$ A$ [
</list>0 P' x4 K Q0 H4 t6 J b: |
</property> 9 ^, T8 _3 l9 N' H+ d$ c+ Y
<property name="hibernateProperties"> w2 E7 X! p: C0 h+ ?
<props>
6 o9 a) K$ ]3 V+ m <prop key="hibernate.dialect">${hibernate.dialect}</prop>3 g* O1 W4 U3 s0 v" c3 G5 h e
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>- C( a/ i! n6 L0 R4 ~3 O
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
) D* x- V+ _* c2 t8 v# w <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->" y: S a+ T6 ?1 i/ l8 _
</props> ) S+ C7 q9 h& T. K
</property> + Y5 L3 G' Z3 r `- j& ^; X/ n
</bean>
$ ^/ ^0 E t6 X! V! o$ i: F
4 D3 J9 c/ `+ b) l, w4 G. I! i <!-- 声明式事务 -->
! |* s. y @ c <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
' \7 ~1 ]+ l& q) ^( s5 \ <property name="sessionFactory" ref="sessionFactory" />
3 W! |- I5 V& z* p9 Q </bean> ! l+ M3 y" V9 \9 E6 S
8 j1 Q5 ~$ z5 [" \7 O' B- w) ^ <aop:config>
n+ g) i. n' J <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" /> # q! u$ F* b( m: Y: V
<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> ! ~' x& f* K" S( ` s# J
</aop:config>
6 m, V) B; R( b$ ?
. C: [8 X" D& {3 r <tx:advice id="txAdvice" transaction-manager="transactionManager"> - A6 f4 E6 b: q+ B
<tx:attributes>
- n+ F) ^7 [6 M7 x7 a' u <tx:method name="save*" propagation="REQUIRED" />
! ~ ^& X! k! h/ A8 X' O <tx:method name="add*" propagation="REQUIRED" />
. d( S; ?. Z4 `- b; D# Q) b <tx:method name="create*" propagation="REQUIRED" />* _9 z6 r- G, T. M1 S3 q$ S
<tx:method name="insert*" propagation="REQUIRED" />5 R4 M; n, _/ T
<tx:method name="update*" propagation="REQUIRED" /># }4 E. d. C0 u$ a8 f, U; k* S0 e
<tx:method name="merge*" propagation="REQUIRED" />
# H" W2 f# h& |- B2 F2 W <tx:method name="del*" propagation="REQUIRED" />
! w8 @3 b% j4 Y <tx:method name="remove*" propagation="REQUIRED" />8 V' r1 k9 a6 ]0 [* O
<tx:method name="put*" propagation="REQUIRED" />7 M4 o0 Q {" P
<tx:method name="*" propagation="SUPPORTS" read-only="true" /> / \& V0 E5 G0 Z. L/ {6 S/ H) e4 s5 t
</tx:attributes> $ K! ^- @! Q! \ a7 G
</tx:advice> 9 O7 Y: Z1 m! A
</beans> 复制代码 spring-mvc.xml:
+ {6 b6 F: p2 _; a9 H# k% \
2 m" |6 M' Y, i- W0 R6 n0 g" m7 {. }/ b <?xml version="1.0" encoding="UTF-8"?>
8 f- b6 o' O# ~$ e8 R7 K <beans xmlns="http://www.springframework.org/schema/beans"
9 t! _, q; O) ]& |0 {! f* b- }: E$ ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
% T8 F6 B8 B% \/ S( [8 I4 s7 T6 ` xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
& ^$ |2 @0 y( F$ g+ _ xmlns:mvc="http://www.springframework.org/schema/mvc"" f1 J, O) s5 u* a) |& |
xsi:schemaLocation="http://www.springframework.org/schema/beans
* N/ Y( w' u. @% W. ]6 v- d9 l http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
. K+ D8 P- R ]' h) H) C3 l) f( O http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
: i- W( n+ H, A1 K( o1 L3 D* v http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
+ _- g6 Y1 ]# \' z3 {% D5 X5 t' K7 q http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd8 N" h: t7 }; N! G( W; o, ~7 ^
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">& }7 ?+ k: q& X4 {: n/ d
<!-- 自动扫描的包注解 -->7 P# R' [5 w6 Q) S: X
<context:component-scan base-package="com.hoodo.rci" />4 W2 l. D) C& J/ `
<!-- 自动注入 -->/ ~4 h0 \7 G; b2 V. m% u
<mvc:annotation-driven />
0 u; W1 k+ N+ O. c8 H <!-- 视图解析器 -->3 F! Q4 n7 Z9 a, }# Q
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
: _7 ?' R: n% ` <property name="prefix" value="/WEB-INF/jsp/" />
2 Q3 y$ E( [; h S0 |4 q <property name="suffix" value=".jsp" />0 A; l T! F$ D0 e
</bean># N5 P# x: f( ]- j! G" s* Y" J0 T+ |
- O6 v3 _" I: }: V, D <!-- 异常解析器 -->8 j) ]4 F0 M7 q, Z7 Q
<!-- <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">9 p. R8 u+ x3 ^
<property name="defaultErrorView" value="common/error" />$ V7 G# t/ M' k l( k8 w3 a+ J
</bean> -->
4 e! g" p, {2 a( j% C6 o
+ ~) M- K3 p: x <!-- 拦截器 -->
8 `3 M, g1 e% x& f5 q) g" ~% J <!-- <mvc:interceptors>
( ]5 U M7 `1 t* n9 X <mvc:interceptor>
0 g+ b5 V5 c6 E1 D, A9 |" p R8 v <mvc:mapping path="/admin/*/*" />/ b% O- X- N, j8 S" t
<bean class="com.filter.CommonInterceptor"></bean>! Y( w. C5 |5 ^) z. b5 g' V; ?
</mvc:interceptor>2 ]& q7 L0 G: b! r$ O2 a
</mvc:interceptors> -->
7 L1 C$ Q* E& _, A' {# U
, p% ~; X" V/ v <!-- 上传文件相关的配置 -->. W/ [0 a+ k L
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
8 a' l v5 s/ V <property name="defaultEncoding" value="utf-8" />7 [+ W/ ^# E* W; `
<property name="maxUploadSize" value="104857600" />6 G$ d- h. V% [ s9 G
<property name="maxInMemorySize" value="4096" />
0 ?- G/ v# L! Z2 q$ w. K+ D" ?" B </bean>" i( w) `% [ \8 ], Q: S) J
</beans> 复制代码 # P* z0 U0 E: N' e' z! D
& U' z" A! a( R- y) F. d
7 r2 H i& [4 n5 h6 c9 {! {; f _# d
SpringMVC+hibernate4.3+Spring4.1整合案例 0 @2 a+ L0 }& K" B1 @/ Q, M
. v; J* L0 ~; J4 r; K0 E E* k
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :