科帮网-Java论坛、Java社区、JavaWeb毕业设计

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

动态微博

查看: 8169|回复: 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:
    2 F. ?( a8 D( {5 z9 C
    2 l- |( q: z  W
    1. <?xml version="1.0" encoding="UTF-8"?>  
      ' |7 t0 W( n/ u4 c
    2. <beans xmlns="http://www.springframework.org/schema/beans"   
      * }# a9 i+ h( z+ S: t9 f3 a; M2 O
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   # I4 L, I8 }: e7 a% v: `, @
    4.     xmlns:p="http://www.springframework.org/schema/p"  
      6 w0 I0 C$ {) X# C6 z6 d* W$ q7 ^
    5.     xmlns:aop="http://www.springframework.org/schema/aop"   
      5 c' u, R& K0 N- f; M( |1 S
    6.     xmlns:context="http://www.springframework.org/schema/context"  & K0 {% N4 m$ i
    7.     xmlns:jee="http://www.springframework.org/schema/jee"  9 s& z. J  T5 Q3 Y4 i  g
    8.     xmlns:tx="http://www.springframework.org/schema/tx"  1 Q2 U3 c4 v2 \2 q
    9.     xsi:schemaLocation="    * ^- _- d% p: r$ o! G3 W
    10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
      4 y. t' Q) U8 R9 w
    11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
      * s- [  X' Q9 B* q+ S8 i# f
    12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
      # T8 Z* z/ ~" c
    13.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
      ' ]( Q5 r7 F* [$ b- A
    14.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> ( T  O( S6 c0 B& M1 _1 b4 p
    15.     4 k- k7 O4 R0 g
    16.     <!-- <mvc:annotation-driven /> -->
      ( c" Z9 R0 _2 o" j# @* h" ^7 m
    17.     <!-- 【配置视图解析器】 -->  : b) j# ], T0 B+ Q5 d% r+ ]
    18.     <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->  - R- v, U  h8 g
    19.     <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->  8 L$ L) J: U. F$ a1 X
    20. <!--    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
      9 }5 `. j  i$ y
    21.         <property name="prefix" value="/WEB-INF/jsp/"/>  
      ' Y) G# S5 {: G8 y5 i
    22.         <property name="suffix" value=".jsp"/>9 _$ ]. v# A  H+ a$ A
    23.     </bean> -->3 l/ ]! b  \: G1 P6 m, Q7 _0 E
    24.     4 i: e8 `  r. T4 f
    25.     <!--  导入springMvc.xml配置文件 --># i9 ], ~" ~" T" `
    26.     <import resource="classpath:config/spring-mvc.xml" />
      : I$ \; A- j- l8 ]" G4 B5 [$ B
    27.     <!-- 加载数据库配置 -->
      : n8 E( T8 A/ u$ C& G5 A
    28.     <context:property-placeholder location="classpath:config/db.properties" />3 i$ @! m0 k, E9 u6 w3 f; i+ O+ G8 |4 V
    29.         <!-- 加载数据源 -->' K+ U1 b& s# k/ x1 X9 U
    30.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      6 U8 J+ ~0 I' g% z0 d+ c" K5 e9 G' Y
    31.                 <property name="driverClassName" value="${db.driverClassName}" />& o7 ?) D. v" V9 e9 `
    32.                 <property name="url" value="${db.url}" />
      - }+ I' s4 M  N9 a/ z( l+ Z: l
    33.                 <property name="username" value="${db.username}" />1 _' S, X  X6 m8 e$ e2 ^
    34.                 <property name="password" value="${db.password}" />6 W: x6 E9 ^7 Q; n; c6 W
    35.                 <property name="maxActive" value="${db.maxActive}" />
      4 o+ p% Y# `, Z- y6 S7 x
    36.                 <property name="maxIdle" value="${db.maxIdle}" />9 S' H6 g9 H, ~* X; T4 I
    37.                 <property name="minIdle" value="${db.minIdle}" />
      . m0 Z/ k# s' ]0 Q5 V& h; R
    38.                 <property name="maxWait" value="${db.maxWait}" />
      $ |% y' ^4 w% n. u5 N) H9 A& D, Z
    39.         </bean>! u  ~1 r7 O. {  l
    40.    
      % C" ]6 g1 N3 O2 n! M% v5 |
    41.    
      5 c8 H  ?  i" Q+ s4 m
    42.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
      , j: T# T5 b" K: t3 \( _1 O
    43.         <property name="dataSource" ref="dataSource" />  
      5 w- j: a# E# e. x5 p  N
    44.         <property name="mappingDirectoryLocations">  : C, V. z, U" d& J! n: q
    45.                   <list>8 s: b3 S- Z3 D! W" Y& D
    46.                                 <value>classpath:config/hbm/</value>
      3 k! }( y9 E! u  F( X, t
    47.                         </list>% m$ H, y3 O( r' @6 y) n* n' w  Q
    48.         </property>  
      6 n9 m, z2 f/ F9 g* J2 O
    49.         <property name="hibernateProperties">  ' s( m! O2 k) U- L4 w8 J
    50.             <props>! M" E" J7 I6 X
    51.                                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>
      6 R, i" G; _, t; S$ h
    52.                                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
      * o8 u! p1 Y8 X1 k/ T/ d% p* ]
    53.                                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
      3 s# _5 u1 F( y0 c
    54.                                 <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->! ^1 ~" e$ M9 W6 T$ @$ M/ ]
    55.                                 </props> 2 Q2 P7 H( t3 c% \
    56.         </property>  
      7 Z7 X- d2 X1 ^' i. l$ D3 S
    57.     </bean>
      & O1 ^" ^% L/ z. ^
    58.    
      + O  ~/ V% t0 G- {6 H& ]
    59.     <!-- 声明式事务 -->  ( Z9 p8 D/ D  |7 c6 U
    60.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
      & O4 S" m$ l4 t
    61.         <property name="sessionFactory" ref="sessionFactory" />  ( W, Q& a% F+ T3 q
    62.     </bean>  
      , o" f3 L5 z! ]9 ]- x
    63.           ( k" F: `& \; n( T& k
    64.     <aop:config>  % i6 {* H; F( l6 G
    65.         <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />  
      9 G; d% l- r9 s7 F
    66.         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />  / A/ I! }& k2 n! E) X4 S/ ^
    67.     </aop:config>  ) z$ K7 R- `  |  u# Q! x
    68.       8 G. y5 c8 ]& y$ s' m4 Y
    69.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  $ E' }# V7 w" R, K2 y. w
    70.         <tx:attributes>  
      . E& P- v6 ?9 w6 o9 ^! A
    71.                 <tx:method name="save*"   propagation="REQUIRED" />7 U- ?% A0 u# E% B0 F" v
    72.                         <tx:method name="add*"    propagation="REQUIRED" />
      ' o" n6 P4 l8 i& R% }
    73.                         <tx:method name="create*" propagation="REQUIRED" />- s, U/ _0 h  w  ~( s) W' J4 D
    74.                         <tx:method name="insert*" propagation="REQUIRED" />( l7 v9 h- o" ~9 {$ P
    75.                         <tx:method name="update*" propagation="REQUIRED" />
      - w- ?8 ?2 D$ h  k3 k
    76.                         <tx:method name="merge*"  propagation="REQUIRED" />
      , s( Y: T- @% R& L: {  `
    77.                         <tx:method name="del*"    propagation="REQUIRED" />/ Q! Y! M" a5 K! _' U3 E; e
    78.                         <tx:method name="remove*" propagation="REQUIRED" />  o4 E! ^* A8 ?) a
    79.                         <tx:method name="put*"    propagation="REQUIRED" />6 K# U" s2 U* K# O- ]- S
    80.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />  
      9 Q( \4 M& e  M5 R# n' {: \
    81.         </tx:attributes>  3 q- p8 V5 U1 P- o) o5 G0 V
    82.     </tx:advice> 8 p8 L0 S3 }  f$ ~- r7 A! Z
    83. </beans>  
    复制代码
    spring-mvc.xml:
    8 F1 m6 E4 }- u. U4 n: j/ N- r" I/ n1 x4 _: E, @
    1. <?xml version="1.0" encoding="UTF-8"?>
      6 n) s/ }" i4 K; H2 Z- T
    2. <beans xmlns="http://www.springframework.org/schema/beans"
      " \! Q. j' Q2 W4 k. ?
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  ]. C+ b0 B$ l0 m' N
    4.         xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx". ?8 Q* v) K- s/ ~5 r  Q" L: ~' a
    5.         xmlns:mvc="http://www.springframework.org/schema/mvc"" B, X+ T4 t2 H9 j* ~
    6.         xsi:schemaLocation="http://www.springframework.org/schema/beans  : v" Q* f1 z1 V6 u! G
    7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  4 s+ y3 m( l  Z6 B! A# `
    8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
      $ O: ?& F0 F$ z; b5 h
    9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
      * ], A+ J' Z1 u/ c1 V7 |) d
    10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      ' F* s9 }: Z. ~) g7 s+ s% |, N+ y
    11.            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
      - r3 l8 a( D5 n( i# A
    12.     <!-- 自动扫描的包注解  -->
      % W  P- `# W0 u3 L! p. Y
    13.         <context:component-scan base-package="com.hoodo.rci" />
      - f  R2 }$ E! g/ x. X: l3 L
    14.         <!-- 自动注入  -->
      & c7 \& Y+ H6 J8 o* i! {" J+ Q" p2 T
    15.         <mvc:annotation-driven />
        p) V$ D5 V2 q8 q8 C- }( z* a5 ~
    16.     <!-- 视图解析器 -->
      & K' `+ A! r8 J7 Q
    17.         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">8 S5 q+ b' u; |! H/ d
    18.                 <property name="prefix" value="/WEB-INF/jsp/" />
      / X! g: B- P8 H" [
    19.                 <property name="suffix" value=".jsp" />1 n& e2 m( @: L
    20.         </bean>' j6 p7 [4 F& q, L( ~' c7 H
    21.         - i( ]/ X* m. P5 |7 y
    22.      <!-- 异常解析器 -->
      : o& w0 Q' E. D: N  O5 X
    23.    <!--  <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
      2 d2 \3 L/ {  f+ \
    24.         <property name="defaultErrorView" value="common/error" />
      * j- R* b/ ?" Y. M4 ~  O
    25.    </bean> -->& }5 s: s, K) U5 E: T- K( b1 m& K
    26.      6 t2 ^  q& }7 W3 m; V
    27.      <!-- 拦截器  -->; _7 H+ ^9 O* S" |# f; r
    28.         <!-- <mvc:interceptors>
      # F$ q( R( k; k6 m
    29.             <mvc:interceptor>
      4 Y# o5 O9 E2 B
    30.                <mvc:mapping path="/admin/*/*" />
      ( C4 T" f/ B+ p! y  R
    31.                <bean class="com.filter.CommonInterceptor"></bean>3 a: P# B" Z6 H4 A% Z, y& ?
    32.             </mvc:interceptor>( U5 I( @- d2 K+ h, N  W9 y
    33.         </mvc:interceptors> -->
      2 `1 z" }  B6 ~! s6 E

    34. - q4 R/ Y3 R5 V) u' Q9 ^' y) R
    35.         <!-- 上传文件相关的配置 -->
      / T+ A4 |! \" C& n: g0 X- L  z0 G3 F
    36.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      # `  I& I7 U8 R
    37.         <property name="defaultEncoding" value="utf-8" />$ ]1 b- L, z( K- O% y% o
    38.                 <property name="maxUploadSize" value="104857600" />. X! W( P; L/ Q- Q9 B
    39.                 <property name="maxInMemorySize" value="4096" />
      " B8 m. _; K5 l  c% ?9 x/ W$ f. k
    40.         </bean>$ ~- |, n3 M! R! f, Z" w' ^/ i& b
    41. </beans>
    复制代码

    & o2 e  l7 g# l, k! e
    * M$ d+ p6 V( ]" k" e( @
    ! \5 c. _3 F6 V6 qSpringMVC+hibernate4.3+Spring4.1整合案例

    5 d! r; u6 t9 m- ?; n2 K# `
    # q/ h6 U% G7 V% ?3 Z

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


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

    1

    主题

    3

    听众

    341

    金钱

    四袋长老

    该用户从未签到

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

    1 B+ a' [. X$ N8 U; R% @8 I下来,学习一下。谢谢
    回复

    使用道具 举报

    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 |只看该作者
    不错不错不错,学习一下
    回复

    使用道具 举报

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

       

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