我的日常

登录/注册
您现在的位置:论坛 盖世程序员(我猜到了开头 却没有猜到结局) 项目源码 > SpringMVC+hibernate4.3+Spring4.1整合案例
总共48086条微博

动态微博

查看: 8320|回复: 10

SpringMVC+hibernate4.3+Spring4.1整合案例

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

    2021-2-2 11:21
  • 签到天数: 36 天

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2015-04-15 20:59:36 |只看该作者 |倒序浏览
    springMVC+hibernate4.3+Spring4.1整合案例spring.xml:, n5 B/ A) A5 ~. |% o2 o

    & g1 ^3 I/ v2 s! p5 I$ f+ p$ j
    1. <?xml version="1.0" encoding="UTF-8"?>  4 Y1 w  F9 b/ e) D* B. \+ V8 P$ h
    2. <beans xmlns="http://www.springframework.org/schema/beans"    7 l) Y, m3 b  ^, W9 V5 A1 y
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   ) @% h& S1 x- P% D, X- f" o# Z
    4.     xmlns:p="http://www.springframework.org/schema/p"  
      ' i; X/ x& y7 o4 `5 f9 B
    5.     xmlns:aop="http://www.springframework.org/schema/aop"   
      # Z& b. E2 y. H) u; t- {
    6.     xmlns:context="http://www.springframework.org/schema/context"  
      ! g. z. \) Y* T( b4 ]; C* l# Q
    7.     xmlns:jee="http://www.springframework.org/schema/jee"  
      * @' h: ~- C! R9 Q7 i
    8.     xmlns:tx="http://www.springframework.org/schema/tx"  # S: _9 T( Q* C8 n" |! n) b, l
    9.     xsi:schemaLocation="   
      4 L% p4 Z4 ?7 K& G- O3 i( n2 v6 @
    10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  . j' m, o, V2 f3 g
    11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  6 {* K9 [. Q; l) V* Q  V- u+ Q
    12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  1 N3 m& }- o; @* ~# ~( i! D
    13.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
      1 M" a2 W; F+ K. [. ?: ]
    14.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
      ! g  \' E  s3 `8 g$ b
    15.     " ]2 Y5 x' G. q& ]
    16.     <!-- <mvc:annotation-driven /> -->
      ! G6 ]3 e% i# X2 c3 e4 b  s9 w
    17.     <!-- 【配置视图解析器】 -->  
      * o4 d; L0 C7 O7 E3 [
    18.     <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->  : V& K3 q) n! G" d! Z! J2 g
    19.     <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->  ' ]+ J: r1 ^9 b/ z/ `( i
    20. <!--    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  ( u: R4 H7 D% h6 I! _# C. M
    21.         <property name="prefix" value="/WEB-INF/jsp/"/>  ( Y& S) w, [, _8 G0 e$ M
    22.         <property name="suffix" value=".jsp"/>- [8 `% k* [& r% t1 ^' C& A! n1 n
    23.     </bean> -->
      2 `! b) |; z- [: t: `4 l/ i
    24.     0 S& c2 @; ~$ d( g8 Z
    25.     <!--  导入springMvc.xml配置文件 -->" [7 F1 _& f) I9 K- ~
    26.     <import resource="classpath:config/spring-mvc.xml" />
      & G6 P/ t3 O6 k: M2 l% }
    27.     <!-- 加载数据库配置 -->
      / |9 N% f& T! X# b) e
    28.     <context:property-placeholder location="classpath:config/db.properties" />0 S- S: i+ W  ]! h7 [
    29.         <!-- 加载数据源 -->! @( I6 y3 Z/ z7 j2 U; B5 `
    30.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">7 X+ E+ x' m/ h* J9 v7 P/ R7 x
    31.                 <property name="driverClassName" value="${db.driverClassName}" />
      ) W* F' [8 L+ |8 e1 q
    32.                 <property name="url" value="${db.url}" />3 ~# F$ ]- M! t& X# D
    33.                 <property name="username" value="${db.username}" />+ _: Y. Y2 m4 q2 _$ k- j6 C
    34.                 <property name="password" value="${db.password}" />
      / i: M  T6 l0 X+ u2 t
    35.                 <property name="maxActive" value="${db.maxActive}" />
      : ]+ S1 }4 |& O: K; O6 s
    36.                 <property name="maxIdle" value="${db.maxIdle}" />+ r1 e( q$ |  F! P9 K# \1 _! ]1 v
    37.                 <property name="minIdle" value="${db.minIdle}" />
      ! O% D; z1 u+ v4 T
    38.                 <property name="maxWait" value="${db.maxWait}" />
      * _* ], G* ]; h, c! u9 \4 J
    39.         </bean>
      ) E! s1 i+ `/ ?* I5 t' Q) i& r
    40.     : g/ f. i! q# v( ^! ]% G
    41.    
      / Y9 `$ I8 E2 n' ^2 ]
    42.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
      . |1 W* |+ T' X
    43.         <property name="dataSource" ref="dataSource" />  
      1 h- |9 h* l& D9 X
    44.         <property name="mappingDirectoryLocations">  4 j( |$ a6 B8 t$ W0 |
    45.                   <list># L& ~- n. u* r( i" n
    46.                                 <value>classpath:config/hbm/</value>
      9 ^+ A7 D& N7 Q7 m
    47.                         </list>2 F% t; G! d: _* j; g
    48.         </property>  
      3 W) H) b  q2 P
    49.         <property name="hibernateProperties">  
      3 w9 \/ p5 H3 g3 Z' B, x4 f+ ?
    50.             <props>* B3 K4 Y# g; [4 _8 m) ^7 l8 }7 p
    51.                                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>" s% J# C2 g: ]  x( s, a
    52.                                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
      - N( T% _& S% L, W
    53.                                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>- X; L0 _: P5 P0 _5 X' p
    54.                                 <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->
      ' O* U3 ]9 f" k2 U
    55.                                 </props>   ]) N) P) R1 x" _: w9 g% ^5 c
    56.         </property>  
      7 a! ?* N1 I' M/ R
    57.     </bean>( g: d& {4 p6 r- {( p  F7 I
    58.    
      ! [5 v- t. O! b, _2 ]7 `3 P0 I
    59.     <!-- 声明式事务 -->  + Z- e: l2 W' U1 X0 n: }
    60.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  8 I! ], m) Q9 J) x
    61.         <property name="sessionFactory" ref="sessionFactory" />  
      0 U$ I+ U8 r! Z4 Q4 V
    62.     </bean>  / ~3 q2 Q9 K$ J$ l* H! p
    63.          
      # J2 ?: ^4 k4 M# Y
    64.     <aop:config>  
      % N+ ~- R8 }" E1 j7 g
    65.         <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />  
      0 {' `1 Y- T# [7 `% S3 I$ M
    66.         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />  ! y8 K4 o# P$ Q' L$ e
    67.     </aop:config>  
      - }4 }! u: W8 j7 h; \% Y) y( `
    68.       + `+ d  e% J: X, {1 ^) m9 I
    69.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  3 x2 h5 ~- c0 a, W
    70.         <tx:attributes>  * y- w. Z! _% ?' g
    71.                 <tx:method name="save*"   propagation="REQUIRED" />
      7 K# Q% K  D) M& x4 K! Q/ N5 w
    72.                         <tx:method name="add*"    propagation="REQUIRED" />2 Q% y0 V; u! ^+ Z/ q% s  L) O
    73.                         <tx:method name="create*" propagation="REQUIRED" />
      5 ]+ Z- E! O$ V. F; g: o  v4 p
    74.                         <tx:method name="insert*" propagation="REQUIRED" /># X  s+ Z6 D, g. }" W
    75.                         <tx:method name="update*" propagation="REQUIRED" />
      % \- T9 _( K2 f
    76.                         <tx:method name="merge*"  propagation="REQUIRED" />
      3 B( f# z8 v. Q
    77.                         <tx:method name="del*"    propagation="REQUIRED" />4 N- A/ g: U/ k( v* p' F( `3 g
    78.                         <tx:method name="remove*" propagation="REQUIRED" />/ n2 M7 u. I" g6 r
    79.                         <tx:method name="put*"    propagation="REQUIRED" />: U, N4 r: G8 }) [9 g
    80.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />  
      . @6 B) z" s9 `0 h+ X8 L
    81.         </tx:attributes>  " q) O  P) Z) D1 q3 k0 K' b
    82.     </tx:advice>
      5 @( s$ D) r4 D- q0 f" [
    83. </beans>  
    复制代码
    spring-mvc.xml:5 {8 _- z' C* o& d+ W% _
    1 |7 Y: D& w4 C! p, z; g$ v2 b: Z
    1. <?xml version="1.0" encoding="UTF-8"?>
      1 W6 A5 t- o8 P1 G) v7 d* g
    2. <beans xmlns="http://www.springframework.org/schema/beans"
      ; R1 h$ N- X% [6 F$ H" \8 @4 Z
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"$ e; P. T' I4 T% m! r. H
    4.         xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
      - j6 s; R7 A4 i1 `
    5.         xmlns:mvc="http://www.springframework.org/schema/mvc"6 C6 _  N& F# ]
    6.         xsi:schemaLocation="http://www.springframework.org/schema/beans  
      ; ?# n2 Y/ ?+ E3 L$ X* g
    7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  # q: Q( J8 O# ?" v0 @& J
    8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
      5 U2 o7 {* y/ `4 f0 Y& S
    9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        I9 N9 }1 k* j4 b+ Q8 T
    10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd/ M0 I/ D+ Z: g3 E4 N# f
    11.            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
      3 \+ w# z0 r2 I& I9 Q
    12.     <!-- 自动扫描的包注解  -->+ v/ C. z* ~- {& M
    13.         <context:component-scan base-package="com.hoodo.rci" />; a9 a4 O0 E( {
    14.         <!-- 自动注入  -->
        w* O1 [& A; h( s
    15.         <mvc:annotation-driven />; S, p& \" M3 j1 S: {9 K; K
    16.     <!-- 视图解析器 -->2 z$ \9 y: J+ ^9 K
    17.         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      % ^9 F3 ~" b; f' i
    18.                 <property name="prefix" value="/WEB-INF/jsp/" />2 L$ _* e; P) y. {" y' T
    19.                 <property name="suffix" value=".jsp" />
      ' I8 Y+ m& W/ U5 m
    20.         </bean>
      0 O% v, ]" ?7 Y0 l& P' o. L; Q
    21.         ! @- m! r. o4 `1 F  _! j
    22.      <!-- 异常解析器 -->/ e! X! Q3 L4 w. t: v: T
    23.    <!--  <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
      3 {* d: X8 I) v3 s* \" N
    24.         <property name="defaultErrorView" value="common/error" />
      3 w! i- t+ R, @) c# f
    25.    </bean> -->
      & n% f9 ^, t8 X+ y" a9 ~
    26.      
      4 u' p4 J  Q& L! W( f7 Y% O' J0 m
    27.      <!-- 拦截器  -->6 U9 s, C4 I8 o9 ^
    28.         <!-- <mvc:interceptors>! C1 r: D% x% Q( N
    29.             <mvc:interceptor>$ i( a; o4 s( n' B9 n: u$ E
    30.                <mvc:mapping path="/admin/*/*" />
      ' M) ]; [( q" ^
    31.                <bean class="com.filter.CommonInterceptor"></bean>
      - Q8 K2 n, _3 _% a
    32.             </mvc:interceptor>: N1 Z  o0 ?2 S+ c) X, I
    33.         </mvc:interceptors> -->% U5 p0 T" W3 |  s- ^1 C( j% W

    34.   `, q5 r$ z: d! A
    35.         <!-- 上传文件相关的配置 -->5 h5 |3 Q0 Z; \) I% z# z# N
    36.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      + p! R8 K  W4 r
    37.         <property name="defaultEncoding" value="utf-8" />1 r5 J9 W) d' e  p
    38.                 <property name="maxUploadSize" value="104857600" />: o7 K: K1 }8 w0 b1 {1 ?) e$ B
    39.                 <property name="maxInMemorySize" value="4096" />+ }7 S$ _' z, G8 _
    40.         </bean>
      % z4 N& q! a: o6 z* E! a3 M$ W
    41. </beans>
    复制代码

    : H% y! D! n, }& P" U" b$ e8 t9 [1 c1 h5 J. ]7 ~" h' R0 C

    , L- _& {% l! ]- W9 hSpringMVC+hibernate4.3+Spring4.1整合案例

    - G: W/ J6 f$ N9 N/ f8 ~# j# |. s; ~  J7 F

    我的日常 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与我的日常享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和我的日常的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、我的日常管理员和版主有权不事先通知发贴者而删除本文


    JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

    1

    主题

    3

    听众

    341

    金钱

    四袋长老

    该用户从未签到

    沙发
    发表于 2016-03-17 21:46:03 |只看该作者
    : q& p# Z' Q5 r( w. A% D( J
    下来,学习一下。谢谢
    回复

    使用道具 举报

    woniu 实名认证   

    2

    主题

    0

    听众

    330

    金钱

    四袋长老

    该用户从未签到

    板凳
    发表于 2016-04-12 11:49:21 |只看该作者
    这个项目太棒勒!下下来学习下!
    回复

    使用道具 举报

    2

    主题

    0

    听众

    289

    金钱

    五袋长老

    该用户从未签到

    地板
    发表于 2016-05-05 16:00:00 |只看该作者
    下来,学习一下。谢谢
    回复

    使用道具 举报

    2

    主题

    0

    听众

    289

    金钱

    五袋长老

    该用户从未签到

    5#
    发表于 2016-05-05 16:00:11 |只看该作者
    下来,学习一下。谢谢
    回复

    使用道具 举报

    2

    主题

    0

    听众

    289

    金钱

    五袋长老

    该用户从未签到

    6#
    发表于 2016-05-05 16:00:44 |只看该作者
    感谢分享   ,
    回复

    使用道具 举报

    1

    主题

    0

    听众

    80

    金钱

    三袋弟子

    该用户从未签到

    7#
    发表于 2016-06-22 09:26:53 |只看该作者
    好资料,可以好好学习下
    回复

    使用道具 举报

    4

    主题

    0

    听众

    302

    金钱

    四袋长老

    该用户从未签到

    8#
    发表于 2016-09-26 16:41:42 |只看该作者
    看看。学习学习。谢谢楼主
    回复

    使用道具 举报

    46

    主题

    1

    听众

    584

    金钱

    三袋弟子

    该用户从未签到

    80后

    9#
    发表于 2016-10-30 11:15:32 |只看该作者
    SpringMVC+hibernate4.3+Spring4.1整合案例 [复制链接]
    回复

    使用道具 举报

    5

    主题

    0

    听众

    316

    金钱

    四袋长老

    该用户从未签到

    10#
    发表于 2017-08-14 08:49:01 |只看该作者
    不错不错不错,学习一下
    回复

    使用道具 举报

    快速回复
    您需要登录后才可以回帖 登录 | 立即注册

       

    关闭

    站长推荐上一条 /1 下一条

    发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
    快速回复 返回顶部 返回列表