我的日常

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

动态微博

查看: 8555|回复: 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:4 u% H3 j$ E, L. `5 p& D
    ! Y6 S% u7 y$ Z4 f+ H& F
    1. <?xml version="1.0" encoding="UTF-8"?>  
      ) x( ]9 t; b8 W: ]5 J
    2. <beans xmlns="http://www.springframework.org/schema/beans"    ( n) p7 v# L" u( R; Z- B, ~
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      # u, c6 Y+ Y& E" N  S7 W
    4.     xmlns:p="http://www.springframework.org/schema/p"  
      8 w$ {. \* l, e  s
    5.     xmlns:aop="http://www.springframework.org/schema/aop"   ; W- _& `3 X+ S+ l5 w6 s2 ]! K
    6.     xmlns:context="http://www.springframework.org/schema/context"  
      + Y/ B. k4 h- {7 W! a
    7.     xmlns:jee="http://www.springframework.org/schema/jee"  
      2 ]5 F& D6 i7 V" N0 A' ^
    8.     xmlns:tx="http://www.springframework.org/schema/tx"    t& j* R4 L2 O, k3 H4 h% S
    9.     xsi:schemaLocation="   
      , G; r9 L+ p6 h' D' u
    10.         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
    11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
      & V2 ^3 _; U1 t; R1 L
    12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  % ^4 E0 k$ {& m- D0 x
    13.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
      , W, p2 T* h8 ^* {+ `4 I# `
    14.         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
    15.    
      5 ?& z* X; C7 L0 |. B% A$ t
    16.     <!-- <mvc:annotation-driven /> -->' e  `2 Y% ]( ^- q& a% ?
    17.     <!-- 【配置视图解析器】 -->  1 _6 \& e2 v$ L& y
    18.     <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->  
      / f& p' [' Z$ I& I% u
    19.     <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->  
      & b0 P; L# x8 I6 ?$ `
    20. <!--    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
      9 }) N$ f8 i/ M" o: Y$ o
    21.         <property name="prefix" value="/WEB-INF/jsp/"/>  
      : d' x6 @+ v5 T' ?, ^2 Z9 i
    22.         <property name="suffix" value=".jsp"/>
      ( ?  l9 G$ A* h/ v& Z2 a" f
    23.     </bean> -->
      : c+ b2 F7 m+ o
    24.    
      * D; h' P- w/ x" p  r
    25.     <!--  导入springMvc.xml配置文件 -->: F6 c) y( U8 U$ K* S
    26.     <import resource="classpath:config/spring-mvc.xml" />* ~, _; g0 H2 I/ _* v7 c* u
    27.     <!-- 加载数据库配置 -->& R. R! F9 l8 B; D
    28.     <context:property-placeholder location="classpath:config/db.properties" />
      ' _2 Q* _. u" z' M; y( _7 |, C
    29.         <!-- 加载数据源 -->
      3 |1 R) F- N2 q) `0 e5 e* q. ^1 q
    30.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">& y3 k8 {, N7 }! [2 ]$ e
    31.                 <property name="driverClassName" value="${db.driverClassName}" />
      3 _( [6 o+ n# Z( r% e: _; ?% f9 ^
    32.                 <property name="url" value="${db.url}" />
      1 ~$ ~' u* ~, f; v# Q/ R
    33.                 <property name="username" value="${db.username}" />
      ! A) Z2 n' K" j8 Q: G& K
    34.                 <property name="password" value="${db.password}" />9 R- Y. T: _1 }6 ^
    35.                 <property name="maxActive" value="${db.maxActive}" />- {0 L& l& a0 r6 A! ]; s
    36.                 <property name="maxIdle" value="${db.maxIdle}" />
      & g. L/ \. A1 M- g
    37.                 <property name="minIdle" value="${db.minIdle}" />3 N+ v: p( o8 ?% Q8 W, z& C3 B
    38.                 <property name="maxWait" value="${db.maxWait}" />
      " P8 L3 \- L- s7 U7 K: f& C
    39.         </bean>
      2 ?1 B2 R5 i. J3 y& O4 U4 h
    40.     , |1 k7 N: J& p+ X! M
    41.    
      7 V0 y  `7 @- z7 `
    42.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
      + c2 R: w0 G3 B- L& u
    43.         <property name="dataSource" ref="dataSource" />  
      4 ~) W; V$ ]/ I2 u
    44.         <property name="mappingDirectoryLocations">  0 a& H2 Y* Z  l, E' ?
    45.                   <list>
      7 N8 P0 m. G" d1 a; `% R' ]
    46.                                 <value>classpath:config/hbm/</value>0 O4 a- W  w+ B$ A$ [
    47.                         </list>0 P' x4 K  Q0 H4 t6 J  b: |
    48.         </property>  9 ^, T8 _3 l9 N' H+ d$ c+ Y
    49.         <property name="hibernateProperties">    w2 E7 X! p: C0 h+ ?
    50.             <props>
      6 o9 a) K$ ]3 V+ m
    51.                                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>3 g* O1 W4 U3 s0 v" c3 G5 h  e
    52.                                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>- C( a/ i! n6 L0 R4 ~3 O
    53.                                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
      ) D* x- V+ _* c2 t8 v# w
    54.                                 <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->" y: S  a+ T6 ?1 i/ l8 _
    55.                                 </props> ) S+ C7 q9 h& T. K
    56.         </property>  + Y5 L3 G' Z3 r  `- j& ^; X/ n
    57.     </bean>
      $ ^/ ^0 E  t6 X! V! o$ i: F
    58.    
      4 D3 J9 c/ `+ b) l, w4 G. I! i
    59.     <!-- 声明式事务 -->  
      ! |* s. y  @  c
    60.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
      ' \7 ~1 ]+ l& q) ^( s5 \
    61.         <property name="sessionFactory" ref="sessionFactory" />  
      3 W! |- I5 V& z* p9 Q
    62.     </bean>  ! l+ M3 y" V9 \9 E6 S
    63.          
      8 j1 Q5 ~$ z5 [" \7 O' B- w) ^
    64.     <aop:config>  
        n+ g) i. n' J
    65.         <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />  # q! u$ F* b( m: Y: V
    66.         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />  ! ~' x& f* K" S( `  s# J
    67.     </aop:config>  
      6 m, V) B; R( b$ ?
    68.       
      . C: [8 X" D& {3 r
    69.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  - A6 f4 E6 b: q+ B
    70.         <tx:attributes>  
      - n+ F) ^7 [6 M7 x7 a' u
    71.                 <tx:method name="save*"   propagation="REQUIRED" />
      ! ~  ^& X! k! h/ A8 X' O
    72.                         <tx:method name="add*"    propagation="REQUIRED" />
      . d( S; ?. Z4 `- b; D# Q) b
    73.                         <tx:method name="create*" propagation="REQUIRED" />* _9 z6 r- G, T. M1 S3 q$ S
    74.                         <tx:method name="insert*" propagation="REQUIRED" />5 R4 M; n, _/ T
    75.                         <tx:method name="update*" propagation="REQUIRED" /># }4 E. d. C0 u$ a8 f, U; k* S0 e
    76.                         <tx:method name="merge*"  propagation="REQUIRED" />
      # H" W2 f# h& |- B2 F2 W
    77.                         <tx:method name="del*"    propagation="REQUIRED" />
      ! w8 @3 b% j4 Y
    78.                         <tx:method name="remove*" propagation="REQUIRED" />8 V' r1 k9 a6 ]0 [* O
    79.                         <tx:method name="put*"    propagation="REQUIRED" />7 M4 o0 Q  {" P
    80.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />  / \& V0 E5 G0 Z. L/ {6 S/ H) e4 s5 t
    81.         </tx:attributes>  $ K! ^- @! Q! \  a7 G
    82.     </tx:advice> 9 O7 Y: Z1 m! A
    83. </beans>  
    复制代码
    spring-mvc.xml:
    + {6 b6 F: p2 _; a9 H# k% \
    2 m" |6 M' Y, i- W0 R6 n0 g" m7 {. }/ b
    1. <?xml version="1.0" encoding="UTF-8"?>
      8 f- b6 o' O# ~$ e8 R7 K
    2. <beans xmlns="http://www.springframework.org/schema/beans"
      9 t! _, q; O) ]& |0 {! f* b- }: E$ ?
    3.         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 `
    4.         xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
      & ^$ |2 @0 y( F$ g+ _
    5.         xmlns:mvc="http://www.springframework.org/schema/mvc"" f1 J, O) s5 u* a) |& |
    6.         xsi:schemaLocation="http://www.springframework.org/schema/beans  
      * N/ Y( w' u. @% W. ]6 v- d9 l
    7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      . K+ D8 P- R  ]' h) H) C3 l) f( O
    8.            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
    9.            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
    10.            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 ^
    11.            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">& }7 ?+ k: q& X4 {: n/ d
    12.     <!-- 自动扫描的包注解  -->7 P# R' [5 w6 Q) S: X
    13.         <context:component-scan base-package="com.hoodo.rci" />4 W2 l. D) C& J/ `
    14.         <!-- 自动注入  -->/ ~4 h0 \7 G; b2 V. m% u
    15.         <mvc:annotation-driven />
      0 u; W1 k+ N+ O. c8 H
    16.     <!-- 视图解析器 -->3 F! Q4 n7 Z9 a, }# Q
    17.         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      : _7 ?' R: n% `
    18.                 <property name="prefix" value="/WEB-INF/jsp/" />
      2 Q3 y$ E( [; h  S0 |4 q
    19.                 <property name="suffix" value=".jsp" />0 A; l  T! F$ D0 e
    20.         </bean># N5 P# x: f( ]- j! G" s* Y" J0 T+ |
    21.         
      - O6 v3 _" I: }: V, D
    22.      <!-- 异常解析器 -->8 j) ]4 F0 M7 q, Z7 Q
    23.    <!--  <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">9 p. R8 u+ x3 ^
    24.         <property name="defaultErrorView" value="common/error" />$ V7 G# t/ M' k  l( k8 w3 a+ J
    25.    </bean> -->
      4 e! g" p, {2 a( j% C6 o
    26.      
      + ~) M- K3 p: x
    27.      <!-- 拦截器  -->
      8 `3 M, g1 e% x& f5 q) g" ~% J
    28.         <!-- <mvc:interceptors>
      ( ]5 U  M7 `1 t* n9 X
    29.             <mvc:interceptor>
      0 g+ b5 V5 c6 E1 D, A9 |" p  R8 v
    30.                <mvc:mapping path="/admin/*/*" />/ b% O- X- N, j8 S" t
    31.                <bean class="com.filter.CommonInterceptor"></bean>! Y( w. C5 |5 ^) z. b5 g' V; ?
    32.             </mvc:interceptor>2 ]& q7 L0 G: b! r$ O2 a
    33.         </mvc:interceptors> -->
      7 L1 C$ Q* E& _, A' {# U

    34. , p% ~; X" V/ v
    35.         <!-- 上传文件相关的配置 -->. W/ [0 a+ k  L
    36.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      8 a' l  v5 s/ V
    37.         <property name="defaultEncoding" value="utf-8" />7 [+ W/ ^# E* W; `
    38.                 <property name="maxUploadSize" value="104857600" />6 G$ d- h. V% [  s9 G
    39.                 <property name="maxInMemorySize" value="4096" />
      0 ?- G/ v# L! Z2 q$ w. K+ D" ?" B
    40.         </bean>" i( w) `% [  \8 ], Q: S) J
    41. </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爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

    1

    主题

    3

    听众

    341

    金钱

    四袋长老

    该用户从未签到

    沙发
    发表于 2016-03-17 21:46:03 |只看该作者

    # y' @. z- }* S下来,学习一下。谢谢
    回复

    使用道具 举报

    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群 科帮网手机客户端
    快速回复 返回顶部 返回列表