科帮网

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

动态微博

查看: 8638|回复: 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:
    6 z3 }9 S  z7 L8 j- @% M& g
    6 ]* f8 A* g0 z1 m
    1. <?xml version="1.0" encoding="UTF-8"?>  ; L1 Y/ b. P  U) P3 @2 a# O
    2. <beans xmlns="http://www.springframework.org/schema/beans"   
      . v4 g$ Q7 S, F, }! W) Y
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   ! R! ?+ c7 K, f' ~9 a
    4.     xmlns:p="http://www.springframework.org/schema/p"  6 E! W2 B9 a( I3 i
    5.     xmlns:aop="http://www.springframework.org/schema/aop"   6 |2 o! n% Y( E9 O1 Q/ L/ ^
    6.     xmlns:context="http://www.springframework.org/schema/context"  . L3 v1 Z7 c0 J* J9 u5 `
    7.     xmlns:jee="http://www.springframework.org/schema/jee"  , l; i" g8 g4 c2 V8 o  e
    8.     xmlns:tx="http://www.springframework.org/schema/tx"  8 V, h6 f* M5 q$ ]. ?
    9.     xsi:schemaLocation="      i, d3 R3 x  k0 Z8 ?, V
    10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  0 I) ^8 x/ x7 n7 W0 U- _! j7 u0 Z
    11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  ' p2 |/ }) D7 m/ f7 e. L
    12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  ' f$ X) W) @1 }  z2 D
    13.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
      + d3 q# c, T6 a$ v* Q- {: Q1 o, Z" ~
    14.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 5 \: H3 J* R6 i
    15.    
      ) s5 Z. z  C0 n  m5 `% _/ {
    16.     <!-- <mvc:annotation-driven /> -->! f' G' u+ y7 R- ~+ P3 X
    17.     <!-- 【配置视图解析器】 -->  , C6 ^( e' `. g3 g1 a
    18.     <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->  
      3 ~) [: A& }7 h7 m/ E; o
    19.     <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->  
      . f' C9 m" h- H
    20. <!--    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  " T! y4 N8 L+ Q8 P2 ?0 G: ^
    21.         <property name="prefix" value="/WEB-INF/jsp/"/>  , g$ ^: K3 A$ {& Y' [. d4 y
    22.         <property name="suffix" value=".jsp"/>9 t( @7 w! `. u% K; ], ]
    23.     </bean> -->) L0 ?/ m1 v, {
    24.    
      $ |. Y! b( r/ v1 A( G
    25.     <!--  导入springMvc.xml配置文件 --># y8 {( G2 H7 F  Z& J
    26.     <import resource="classpath:config/spring-mvc.xml" />
        B6 m/ W0 ]( ?3 n; }8 @
    27.     <!-- 加载数据库配置 -->
      : m& N; H4 R; ^& U! ~/ o
    28.     <context:property-placeholder location="classpath:config/db.properties" />
      9 P* D! T- j5 e6 `* Y
    29.         <!-- 加载数据源 -->
      1 h, h, `, D" \
    30.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      9 E. ~( ]& D; |
    31.                 <property name="driverClassName" value="${db.driverClassName}" />
      . `, J. t' m1 W$ l; s$ U' G
    32.                 <property name="url" value="${db.url}" />* M/ {( \8 N% j( c1 f. V& A) W
    33.                 <property name="username" value="${db.username}" />
      6 i* i9 c. j- e9 J/ O
    34.                 <property name="password" value="${db.password}" />! ~- G: Q7 r% B' V" o# @4 H
    35.                 <property name="maxActive" value="${db.maxActive}" />
      ' T" Y8 B  k) @3 {- w' f4 @& S# v; T
    36.                 <property name="maxIdle" value="${db.maxIdle}" />6 f  E# l. J) z
    37.                 <property name="minIdle" value="${db.minIdle}" />/ F6 X' @) Y  [0 f. L8 J* t& z
    38.                 <property name="maxWait" value="${db.maxWait}" />  s+ ^6 J' _$ v' x+ @
    39.         </bean>9 h9 D" l" n2 H9 W6 y
    40.    
      2 A  R- P" S  u, h! `4 w, r
    41.     2 ?$ p6 T9 ^! R2 T
    42.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  1 t( s1 ?& Z" c) ~/ U
    43.         <property name="dataSource" ref="dataSource" />  
      , `$ ^- _5 |) w  D+ h* S
    44.         <property name="mappingDirectoryLocations">  ( }$ `  q4 T; I9 o; v1 Z% B+ u
    45.                   <list>
      1 v! ?- C+ {  ]+ }) P% V) C. b: ~0 U
    46.                                 <value>classpath:config/hbm/</value>
      : Z7 Q1 ^- V/ _
    47.                         </list>' b% \: q: u$ j
    48.         </property>  
      7 y$ |0 U, v6 E- b/ t
    49.         <property name="hibernateProperties">  
      : B2 I* x. r- u6 q/ s. ?6 u
    50.             <props>; d5 Q+ i$ e; f/ c% c( {
    51.                                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>/ l8 r0 Q4 s" \  b+ l
    52.                                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
      * x  R+ S) L1 r
    53.                                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
      ( S) _1 k" d# `' `3 e. k9 O5 W0 {
    54.                                 <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->
      2 X; o: J* X  D) j4 z
    55.                                 </props>
      3 W9 P1 f5 @/ M6 e: |
    56.         </property>  8 B6 `& u$ d: s5 Z' F3 _3 n
    57.     </bean>
      3 N" W2 w# C1 P
    58.     : [2 ]; [5 s5 U% R5 Z- \8 }
    59.     <!-- 声明式事务 -->  2 i( T' M3 }( H9 k/ @' P! B
    60.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
      4 l- `( \& y  r$ L8 Z
    61.         <property name="sessionFactory" ref="sessionFactory" />  + Y, y9 p& R+ u) h
    62.     </bean>  8 L$ ?; T- H* }+ J% k
    63.           : b# L% x* Q8 |- p; d
    64.     <aop:config>  
      ; }2 Z8 z, S9 |# i
    65.         <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />  
      - S; V" _2 W+ S. {
    66.         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />  / h7 K/ N4 }" P# @" W
    67.     </aop:config>  
      # S6 f4 p+ @: |' M9 W( `% ]5 S
    68.       
      ( C5 J: `5 Y1 E5 x% Z
    69.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  : ~9 y' h7 r% p) `
    70.         <tx:attributes>  " x5 `# Z+ z1 p. c3 Y+ }5 T
    71.                 <tx:method name="save*"   propagation="REQUIRED" />6 T4 s# x' f- S
    72.                         <tx:method name="add*"    propagation="REQUIRED" />% X! H, ~% n0 Y' d
    73.                         <tx:method name="create*" propagation="REQUIRED" />
      $ {6 A  E$ k7 T! U- h+ d8 X
    74.                         <tx:method name="insert*" propagation="REQUIRED" />; y$ O7 P- X, i8 j$ u9 |1 |& M
    75.                         <tx:method name="update*" propagation="REQUIRED" />
      1 S- ^+ ~5 V$ R
    76.                         <tx:method name="merge*"  propagation="REQUIRED" />; Y* C" ?% A4 h. _+ M* t5 O
    77.                         <tx:method name="del*"    propagation="REQUIRED" />
      9 e; j5 J0 x1 ~- f% X2 G5 C
    78.                         <tx:method name="remove*" propagation="REQUIRED" />) [) c3 S: Q" G. J8 R3 ^8 {6 x
    79.                         <tx:method name="put*"    propagation="REQUIRED" />, e2 o' K4 P% ^' @; n
    80.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />  
      3 ^6 Z& m' C% ~* D  e2 \3 p6 G
    81.         </tx:attributes>  
      5 I: B9 ]- o6 \8 Y
    82.     </tx:advice> % T8 h' I5 G* }5 G0 f
    83. </beans>  
    复制代码
    spring-mvc.xml:
    8 c5 ]; t; J; C: v+ J% h! }. E
    * b3 w- |/ d4 [
    1. <?xml version="1.0" encoding="UTF-8"?>
      ' B9 x, V- W9 ~$ {# |  v
    2. <beans xmlns="http://www.springframework.org/schema/beans"8 d! r7 h. d; }9 B9 P# w
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"4 q  w3 W* q/ Y
    4.         xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"* a0 t- N8 l) L1 T, c, a
    5.         xmlns:mvc="http://www.springframework.org/schema/mvc"4 d* @* p! z- c% r) t2 E5 E
    6.         xsi:schemaLocation="http://www.springframework.org/schema/beans  
      4 l$ }8 @7 z  ~9 p
    7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      $ l0 M5 w; I7 |9 |% t" M* _
    8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
      4 ?3 r( z1 F2 M
    9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  1 V6 p$ H0 o& q5 T2 @  h% P! f- \
    10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd$ N$ |3 S5 W+ Y0 u
    11.            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
      # v- }; e/ S7 x; ^) G
    12.     <!-- 自动扫描的包注解  -->
      * E  T3 T1 m' R( `( e1 O4 h( p; j1 J
    13.         <context:component-scan base-package="com.hoodo.rci" />5 `& E. _8 I# ^6 {6 D
    14.         <!-- 自动注入  -->
      & q8 B1 C- ?7 `0 D
    15.         <mvc:annotation-driven />. ^/ g. ]' F$ K. j3 g* J5 j
    16.     <!-- 视图解析器 -->
      & Q4 c# B( R  \0 h* w" F1 M6 y
    17.         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">( J$ W; ?% \  A7 C5 d
    18.                 <property name="prefix" value="/WEB-INF/jsp/" />8 ]# d, o/ Z2 I8 x
    19.                 <property name="suffix" value=".jsp" />: o$ _& B: J8 m, {, o
    20.         </bean>
      , X3 C" x1 v# J
    21.         $ g4 i/ }( ^' ~; f
    22.      <!-- 异常解析器 -->2 V0 K/ Y3 V+ \: W% u6 L) Y& u9 y' K' L
    23.    <!--  <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
      & X$ j1 F: T$ a% r# t3 P' g
    24.         <property name="defaultErrorView" value="common/error" />
      $ B2 `5 d" z4 C% W
    25.    </bean> -->
      $ S4 p5 b# ]3 A1 M
    26.      9 ~2 U4 ^5 W# A( ~, L+ S' ~" q
    27.      <!-- 拦截器  -->
      7 h6 ^7 U' k" Y+ x7 W* z
    28.         <!-- <mvc:interceptors>
      6 m( Y1 m  K0 d
    29.             <mvc:interceptor># C& V9 |6 _" U: |2 m, R  |
    30.                <mvc:mapping path="/admin/*/*" />
      4 V6 c: N& [+ \& Q! |
    31.                <bean class="com.filter.CommonInterceptor"></bean>
      ) Q4 |  ?. _+ B4 p( r$ e! |& m. ?
    32.             </mvc:interceptor>
      $ j% u& w: J, h, o& I
    33.         </mvc:interceptors> -->4 p. @  O8 B. E: e$ Z
    34. , x! s) S4 K9 s5 t
    35.         <!-- 上传文件相关的配置 -->" [, A9 c/ L, ^# d/ }" G/ s% a
    36.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">: ~& e. x+ j2 _% L% ]' X
    37.         <property name="defaultEncoding" value="utf-8" />
      - @: x% U$ }( e
    38.                 <property name="maxUploadSize" value="104857600" />
      * k3 M' g6 Z# ?) H
    39.                 <property name="maxInMemorySize" value="4096" />
      : _+ j+ ~( ^  A
    40.         </bean>
      % e  Y' N8 m2 b
    41. </beans>
    复制代码

    - l0 r" t, S. ~* @  Q: i2 N
    2 s  T) t: U* O
    ' f4 K0 b5 X9 q4 ~# o1 e1 g: n" LSpringMVC+hibernate4.3+Spring4.1整合案例
    * S# p/ q: c5 o
    $ w4 b  V+ L; \- U& r0 B) F2 `

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


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

    1

    主题

    3

    听众

    341

    金钱

    四袋长老

    该用户从未签到

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

    * J- l' v6 w# I- B! Z. v; b下来,学习一下。谢谢
    回复

    使用道具 举报

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