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
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>app</display-name> <!-- context启动时加载hibennate的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 设置spring的前端分发器,接受请求 --> <servlet> <servlet-name>myservlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>myservlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 默认错误页面的设置 --> <error-page> <error-code>404</error-code> <location>/WEB-INF/jsp/404.jsp</location> </error-page> <!-- <error-page> <exception-type>java.lang.Exception</exception-type> <location>/WEB-INF/jsp/exception.jsp</location> </error-page> --> </web-app>
-
第二步,myservlet-servlet.xml(DispatcherServlet)的配置文件相关部分,注意,如果在配置中用到了aop,tx,mvc等标签,须在xmlns中导入。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 扫描注解配置的包 --> <context:component-scan base-package="com.tan.*" /> <!-- 基于注释的事务,当注释中发现@Transactional时,使用id为“transactionManager”的事务管理器 --> <!-- 如果没有设置transaction-manager的值,则spring以缺省默认的事务管理器来处理事务,默认事务管理器为第一个加载的事务管理器 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 设置spring的mvc用注解 --> <mvc:annotation-driven /> <!-- 设置handler的映射方式,前面注解是其中一种 --> <!-- HandlerMapping <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> --> <!-- 设置试图的解析ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 此处不加这行似乎也能在jsp中用jstl,只要正确引入了tag--> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 可以使用基于url的handlermapping <bean name="/hello" class="com.tan.controller.MyController"/> --> </beans>
-
第三步,hibenate相关的配置,spring-hibernate.xml。配置数据源->交给sessionFactory->交给spring事物管理transactionManager->spring接手
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- Hibernate4 --> <!-- shiyongproperties文件保存jdbs以及hibernate的相关变量,在具体配置处使用属性zhi值,必须在Spring配置文件的最前面加载,放在src目录 --> <context:property-placeholder location="classpath:persistence-mysql.properties" /> <!-- 获取数据源的几种方式DriverManagerDataSource、dbcp、c3p0,后两种支持连接池 --> <!-- class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" 有连接池作用 --> <!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource"无连接池作用 --> <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.user}" /> <property name="password" value="${jdbc.pass}" /> </bean> --> <!-- c3p0 有连接池作用,使用properties文件下的属性值,也可以直接填--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.user}" /> <property name="password" value="${jdbc.pass}" /> <property name="minPoolSize" value="2" /> <property name="maxPoolSize" value="50" /> <property name="initialPoolSize" value="10" /> <property name="maxIdleTime" value="60" /> <property name="acquireIncrement" value="2" /> </bean> <!-- 配置sessionFactory,统一管理一个数据库的连接 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <!-- 可以加多个包,需要hibenate映射的类的包 --> <value>com.tan.model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <!-- <prop key="hibernate.current_session_context_class">thread</prop> --> </props> </property> </bean> <!-- 配置Hibernate事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置事务异常封装 --> <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> </beans>
-
第四步,添加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 #如果没有表则创建,如果表结构更改则自动更新