在applicationContext.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描dao和service -->
<context:component-scan base-package="com.fh.*"></context:component-scan>
<!-- 将sessionFactory的创建交由spring管理 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置hibernateTemplate模版(让咱们可以通过注解去使用hibernate) -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 定义一个事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 定义advice,配置传播特性、事务隔离级别、只读事务、回滚策略 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="ba*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 切点配置
execution(* com.fh.service.impl.*.*(..))
第一个*:任意返回值
第二个*:包下任意的类
第三个*:类中的所有方法
(..):任意参数 -->
<aop:config>
<aop:pointcut expression="execution(* com.fh.service.impl.*.*(..))" id="servicePointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>
</aop:config>
</beans>
2.在hibernate.cfg.xml中配置
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常情况下,一个session-factory节点代表一个数据库 -->
<session-factory>
<!-- 1.配置数据库的连接信息 -->
<!-- 配置数据库驱动类 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 配置数据库连接地址 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/nnn?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&</property>
<!-- 配置连接数据库使用的用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 配置连接数据库使用的密码 -->
<property name="hibernate.connection.password">123123</property>
<!-- 配置数据库方言,用于让hibernate在运行时根据方言生成不同的SQL语句,从而去兼容不同的数据库。 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 默认情况下是auto(自动的)即:如果不设置的话,会自动去你的classpath下面找一个bean-validation**包,找不到,
所以就会报the default Bean Validation factory找不到的错误。 -->
<property name="javax.persistence.validation.mode">none</property><!-- auto -->
<!-- 2.配置其它hibernate相关东西 -->
<!-- 配置hibernate在运行时是否显示SQL语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 配置是否对SQL语句进行格式化 -->
<property name="hibernate.format_sql">true</property>
<!-- 自动建表
create:始终会自动创建表,如果表存在就把删了再建
create-drop:自动创建表,在调用了sessionFactory.close方法的时候再把表删掉
update:如果数据库没有这个表那么就创建,如果有这张表就修改这张表的表结构 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 3.加载hibernate映射文件 -->
<mapping resource="com/fh/model/user/User.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
3.在spring-mvc.xml中配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启注解 补充springmvc注解 -->
<mvc:annotation-driven/>
<!-- base-package 扫描项目中 com.fh.controller下的java类-->
<context:component-scan base-package="com.fh.controller"/>
<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property value="/WEB-INF/view/" name="prefix"/>
<property value=".jsp" name="suffix"/>
</bean>
<!-- 文件解析器 -->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
<!-- 上传文件大小上限,单位为字节(10MB) -->
<property name="maxUploadSize">
<value>10485760</value>
</property>
<!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
<!-- 释放静态资源 -->
<!-- <mvc:resources location="/My97DatePicker/" mapping="/My97DatePicker/**"/>
<mvc:resources location="/images/" mapping="/images/**"/> -->
<!-- 放开静态资源 -->
</beans>
4.在web.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springMVC核心控制器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<!-- spring核心控制器 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数,设置springMVC的核心配置文件 -->
<init-param>
<!-- 固定写法,死格式 -->
<param-name>contextConfigLocation</param-name>
<!-- classpath:代表src目录 -->
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>