SpringMVC4+hibernate4学习笔记(一)=>配置详解

SpringMVC4+hibernate4学习笔记(一)=>配置详解

鉴于目前资料大多数都是基于spring3的配置,本人在最初搭建的时候遇到很多问题,由此记录下来仅供参考

/* 2014-09-04 by feilengcui008@gmail.com */

使用的jar文件

springframework4.0.6(为了方便整个先导入)
hibernate4.3.6 /required/*下所有jar 以及 /optional下的c3p0(为了使用c3p0作为dataSource,使用其连接池)
jstl.jar standard.jar ----为了使用jstl标签库
apoalliance.jar ----在AOP一些地方依赖这个库
commons-logging.jar

配置详细步骤

  • 第一步,配置web.xml


    app



    contextConfigLocation
    /WEB-INF/spring-*.xml


    org.springframework.web.context.ContextLoaderListener



    myservlet
    org.springframework.web.servlet.DispatcherServlet
    1


    myservlet
    /



    404
    /WEB-INF/jsp/404.jsp

  • 第二步,myservlet-servlet.xml(DispatcherServlet)的配置文件相关部分,注意,如果在配置中用到了aop,tx,mvc等标签,须在xmlns中导入。



    <context:component-scan base-package="com.tan.*" />



    <tx:annotation-driven transaction-manager="transactionManager" />


    <mvc:annotation-driven />









  • 第三步,hibenate相关的配置,spring-hibernate.xml。配置数据源->交给sessionFactory->交给spring事物管理transactionManager->spring接手




    <context:property-placeholder location="classpath:persistence-mysql.properties" />





















    com.tan.model




    ${hibernate.hbm2ddl.auto}
    ${hibernate.dialect}
    ${hibernate.show_sql}








  • 第四步,添加persistence-mysql.properties文件,如果在spring-hibenate.xml直接配置数据源的值,就不需要

    jdbc.X

    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/springmvc(你的数据库名)?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf8
    jdbc.user=root
    jdbc.pass=
    # hibernate.X
    hibernate.connection.driverClass=com.mysql.jdbc.Driver
    hibernate.connection.url=jdbc:mysql://localhost:3306/springmvc(你的数据库名)
    hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    hibernate.connection.username=root
    hibernate.connection.password=
    hibernate.show_sql=true
    hibernate.hbm2ddl.auto=update #如果没有表则创建,如果表结构更改则自动更新

posted on 2014-09-05 09:29  feilengcui008  阅读(1598)  评论(9)    收藏  举报