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

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

动态微博

查看: 8193|回复: 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:, O* A; H$ {9 f4 m4 T; _
    / \% A% Z3 e  T9 X
    1. <?xml version="1.0" encoding="UTF-8"?>  
      ! ~4 M5 x6 v$ t% x7 U
    2. <beans xmlns="http://www.springframework.org/schema/beans"    ' S9 U9 z/ ]5 ?
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      , K0 |  i. \1 [' t9 f# `, Z
    4.     xmlns:p="http://www.springframework.org/schema/p"  $ N6 r& ]0 @0 a( F
    5.     xmlns:aop="http://www.springframework.org/schema/aop"   
      4 S+ G7 N2 m3 O* g, \' T
    6.     xmlns:context="http://www.springframework.org/schema/context"  2 m: H) w# O( l  o) w
    7.     xmlns:jee="http://www.springframework.org/schema/jee"  
      " c! n0 B6 r! Q! e: K
    8.     xmlns:tx="http://www.springframework.org/schema/tx"  4 p* R# T7 m0 A$ l
    9.     xsi:schemaLocation="    2 d$ {" V6 s2 B2 R" e8 x
    10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
      7 u# j: B3 l: O" p) Y8 G
    11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
      3 Y  u: r( ]) j4 A7 r
    12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  5 t' }9 J  u8 T- R
    13.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
      3 I* m" _5 O. R( V+ r: T5 ^
    14.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> " {1 p1 a. G# ~* |3 c
    15.     ! g. z/ g  a( w. Q# L0 D
    16.     <!-- <mvc:annotation-driven /> -->, E  h7 ^/ Q, Y; T0 b
    17.     <!-- 【配置视图解析器】 -->  
      & ]# O* _( f2 o, j8 K3 }. }5 P
    18.     <!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->  * e- B2 [0 t( e" ~  k4 w4 z
    19.     <!-- 由于UserController返回的ModelAndView中的视图名是userlist,故该视图解析器将在/WEB-INF/jsp/userlist.jsp处查找视图 -->  
      % l3 s- ]5 T4 T" D$ t5 i
    20. <!--    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
      8 S5 b. e6 W& r; w; e$ Q; [
    21.         <property name="prefix" value="/WEB-INF/jsp/"/>  ! U( T3 h" @0 V  M3 ^
    22.         <property name="suffix" value=".jsp"/>
      0 V! x6 r2 a" r& g4 d( _2 Z9 ~
    23.     </bean> -->
      % y5 W8 f& q3 Q7 F- K
    24.     : c& h, L  s( N/ K( |
    25.     <!--  导入springMvc.xml配置文件 -->
      8 A+ Y' p$ |. Q% [* Z* i
    26.     <import resource="classpath:config/spring-mvc.xml" />
      : E' U4 y6 z: B% o- B0 D
    27.     <!-- 加载数据库配置 -->
      6 u( A" i! R) A+ |
    28.     <context:property-placeholder location="classpath:config/db.properties" />
        o& h; j: w; i8 ^
    29.         <!-- 加载数据源 -->1 N* {( s) A, l8 y; |* S3 S9 F; G4 W
    30.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">2 H1 M; x% h2 a, g6 u7 g+ Y6 k& `
    31.                 <property name="driverClassName" value="${db.driverClassName}" />& X# q. m0 r  n$ y, d
    32.                 <property name="url" value="${db.url}" />! n; Y4 x% a/ \6 Y3 u
    33.                 <property name="username" value="${db.username}" />
      - a+ I* c6 s5 t1 b! H$ W2 O
    34.                 <property name="password" value="${db.password}" />
      6 I# {1 L: K+ w; @  b0 l8 d. C
    35.                 <property name="maxActive" value="${db.maxActive}" />+ s3 F0 j7 y' C7 u
    36.                 <property name="maxIdle" value="${db.maxIdle}" />
      / H* V6 p: Z( Y# A
    37.                 <property name="minIdle" value="${db.minIdle}" />
      2 W1 z0 L/ X  P$ n' e3 f; k
    38.                 <property name="maxWait" value="${db.maxWait}" />8 l6 x& b% i6 a* G9 u! N
    39.         </bean>
      " ~- T. [) T# N  @5 j
    40.     . B* U! X- R6 i( ~" J
    41.     + a/ e" t( O/ c) |7 L( ~
    42.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
      7 h  _: V" h% ~
    43.         <property name="dataSource" ref="dataSource" />  & o( `8 u8 ]# I) Z
    44.         <property name="mappingDirectoryLocations">  0 C6 f7 n  e; p, W* Q4 @' |
    45.                   <list>! a* d' v7 i- q8 C) Y% F7 \
    46.                                 <value>classpath:config/hbm/</value>
      & s7 m: ~; k( o/ I! G
    47.                         </list>
      8 P( V6 [% ^& z4 A" E! B9 T
    48.         </property>  
      ) {. o6 z0 K* ?6 r( \' P
    49.         <property name="hibernateProperties">  ( |. w1 H$ i5 ?+ r# {+ u& V) t
    50.             <props>$ @$ T5 b% s; P" v# S4 t
    51.                                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>9 `2 P$ G! M- d! {
    52.                                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>& B! B; G7 o9 N- Y1 ?$ m
    53.                                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
      7 ?4 s3 P! R2 ^9 B
    54.                                 <!--<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>-->4 Q1 |! Y' z1 Z
    55.                                 </props> 5 N3 q" U- L3 F6 ^3 w. |: O
    56.         </property>  
      4 _, w$ G) ^0 n! Y. o# u2 a/ J
    57.     </bean>
      + [7 F$ a$ L8 Y
    58.    
      - d+ |4 j9 ~6 D* [$ U& o
    59.     <!-- 声明式事务 -->  
      4 ~! b7 N. q4 m4 j% H# P/ p$ S% S
    60.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  0 p0 f) q& H: l( r: R5 @
    61.         <property name="sessionFactory" ref="sessionFactory" />  
      8 U/ X# E3 T) s- i
    62.     </bean>  & Y/ K+ x! f1 z
    63.           : Y; D( V+ i. ]7 _' C  x
    64.     <aop:config>  * Y) Y- ?  a7 R9 z  W9 {
    65.         <aop:pointcut id="productServiceMethods" expression="execution(* com.service..*.*(..))" />  
      * ]7 R) ~# M1 {8 }; Z, G
    66.         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />  ) [3 Z2 O2 j& r8 T% u) F5 c
    67.     </aop:config>  * @; o4 p0 S! a" u' I
    68.       
      2 L! ~2 }- H1 J+ R! Y' p/ K. q
    69.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
      1 [" r1 C- Q6 ^0 W+ c8 G1 U! W4 Z
    70.         <tx:attributes>  9 s9 V' i8 n2 }: |% r" l
    71.                 <tx:method name="save*"   propagation="REQUIRED" />/ g2 x2 z; I% Z& ^, k8 Q
    72.                         <tx:method name="add*"    propagation="REQUIRED" />
      0 s6 Y- P& C- u0 g* S" ~' z) b
    73.                         <tx:method name="create*" propagation="REQUIRED" /># E7 G" B$ x) l% H$ N
    74.                         <tx:method name="insert*" propagation="REQUIRED" />
      + @5 g5 e+ R6 m5 ~% u0 R; {7 w$ p- N
    75.                         <tx:method name="update*" propagation="REQUIRED" />
      - N: t7 j5 i- x0 D' ^
    76.                         <tx:method name="merge*"  propagation="REQUIRED" />
      * D. H% `' m5 h6 W
    77.                         <tx:method name="del*"    propagation="REQUIRED" />, h& d" q, k" R0 k7 k
    78.                         <tx:method name="remove*" propagation="REQUIRED" />
        P( G# r2 m. ^) l( Y- O
    79.                         <tx:method name="put*"    propagation="REQUIRED" />7 i/ J$ I6 I7 e3 D
    80.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />  
      & O$ _) x2 ?& W- `
    81.         </tx:attributes>  
      $ }: [4 o. `' `
    82.     </tx:advice>
      1 g( {  B5 u& a# W5 S
    83. </beans>  
    复制代码
    spring-mvc.xml:/ E' Q" U5 |- @; q( P  f: ]) t
    ' ]$ e, I9 b7 s7 E8 o4 U1 D$ A! Z
    1. <?xml version="1.0" encoding="UTF-8"?>
      ) f! Z1 c3 V$ d) ?1 y' H7 F
    2. <beans xmlns="http://www.springframework.org/schema/beans"" Q6 f6 m1 L2 i2 `
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context", n5 W7 J  E$ g( S- o/ P. v) e; d' g
    4.         xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"; N) Z* X5 _$ K# ~) Q; w! G
    5.         xmlns:mvc="http://www.springframework.org/schema/mvc"
      $ g6 K& U, S) |! h" |6 r
    6.         xsi:schemaLocation="http://www.springframework.org/schema/beans  ) ~5 u0 w( k! j: U5 f
    7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      , E: p0 |3 ~! e0 p% T: y
    8.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  * p& h; d: x8 H, |
    9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  8 v6 N/ m- o' }' y0 g$ p) {% S
    10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      . t) w) s, u- f. D, }7 o4 Y
    11.            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
      " m2 |" h* g3 J. q: J2 `
    12.     <!-- 自动扫描的包注解  -->8 n: l2 I2 U, N7 U& [, D/ K) W/ D
    13.         <context:component-scan base-package="com.hoodo.rci" />7 \& {1 k8 A- r
    14.         <!-- 自动注入  -->
      0 `& s) E- A4 K, ^2 p) z# v
    15.         <mvc:annotation-driven />
      ) i* X, c  X; Z7 ?4 C/ M
    16.     <!-- 视图解析器 -->
      ) |% s& z* z4 p+ V( P1 _$ n
    17.         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      + [& y3 L* h) y4 g$ w7 b3 K! ]- C$ X2 r
    18.                 <property name="prefix" value="/WEB-INF/jsp/" />: E" N) X( ~9 C5 g
    19.                 <property name="suffix" value=".jsp" />
      8 I, _. u; j  f* M% B
    20.         </bean>
      4 F& Q8 Y2 {( z7 K! ^
    21.           J+ [# g6 L' q' w& F5 s* c/ K1 w
    22.      <!-- 异常解析器 -->9 t- I5 k4 R4 x- M% w9 X
    23.    <!--  <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">) j6 }# p# G6 |4 d
    24.         <property name="defaultErrorView" value="common/error" />
      ) v. R7 g5 U. F9 r* r0 ]) ~3 f. ^
    25.    </bean> -->
      9 I1 Z, [" ^5 S! m+ f
    26.      ! ~7 B' O1 j! e" j2 |9 v, {
    27.      <!-- 拦截器  -->
      1 i+ n/ x1 e0 u/ s% k3 r& x
    28.         <!-- <mvc:interceptors>
      ! C6 M) w4 z1 C, X+ c& ~( w
    29.             <mvc:interceptor>( F0 a- u7 z; i( E
    30.                <mvc:mapping path="/admin/*/*" />
      1 h2 d0 l/ X8 R9 }; |) |# o- P! C
    31.                <bean class="com.filter.CommonInterceptor"></bean>
      1 c) J0 o5 g9 c  {5 j$ ?
    32.             </mvc:interceptor>
      7 S: i  v0 b7 u  m& H9 b& X. O1 ?
    33.         </mvc:interceptors> -->
      " A5 \+ X5 ]; ?% B( {
    34. 7 Z: w* g" X5 _
    35.         <!-- 上传文件相关的配置 -->
      5 T8 ~4 [5 x- {/ C/ N0 y
    36.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      . _* P6 r$ a( s" m( i& C1 h
    37.         <property name="defaultEncoding" value="utf-8" />
      7 w* H7 s* V* G4 Z
    38.                 <property name="maxUploadSize" value="104857600" />0 _$ O. j# H- d; G1 M# R+ E. P) K
    39.                 <property name="maxInMemorySize" value="4096" />
      - z* C+ r. C4 I5 [. o" Q; _. D
    40.         </bean>7 O5 I3 i- W' O7 ^0 N# j
    41. </beans>
    复制代码
    * s6 z! c! N8 H, N# U

    1 W2 t; T6 e) u( B& y8 {! f# {3 o: p/ U8 n0 V
    SpringMVC+hibernate4.3+Spring4.1整合案例

    6 i: J9 n+ F6 l( e* @* [! [% a$ E: x2 O, N: w* I

    科帮网-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爱好者③

    4

    主题

    0

    听众

    81

    金钱

    三袋弟子

    该用户从未签到

    11#
    发表于 2017-10-22 20:29:57 |只看该作者

    ) I! r! ?/ _) E看看。学习学习。谢谢楼主
    回复

    使用道具 举报

    5

    主题

    0

    听众

    316

    金钱

    四袋长老

    该用户从未签到

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

    使用道具 举报

    46

    主题

    1

    听众

    584

    金钱

    三袋弟子

    该用户从未签到

    80后

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

    使用道具 举报

    4

    主题

    0

    听众

    302

    金钱

    四袋长老

    该用户从未签到

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

    使用道具 举报

    1

    主题

    0

    听众

    80

    金钱

    三袋弟子

    该用户从未签到

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

    使用道具 举报

    2

    主题

    0

    听众

    289

    金钱

    五袋长老

    该用户从未签到

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

    使用道具 举报

    2

    主题

    0

    听众

    289

    金钱

    五袋长老

    该用户从未签到

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

    使用道具 举报

    2

    主题

    0

    听众

    289

    金钱

    五袋长老

    该用户从未签到

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

    使用道具 举报

    woniu 实名认证   

    2

    主题

    0

    听众

    330

    金钱

    四袋长老

    该用户从未签到

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

    使用道具 举报

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

       

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