ssm Springmvc+spring+hibernate

来源:互联网 发布:ubuntu 修改时区 编辑:程序博客网 时间:2024/05/06 13:56

1.spring-hibernate.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:p="http://www.springframework.org/schema/p"  xmlns:context="http://www.springframework.org/schema/context"  xmlns:tx="http://www.springframework.org/schema/tx"  xmlns:aop="http://www.springframework.org/schema/aop"  xmlns:mvc="http://www.springframework.org/schema/mvc"  xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.1.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/aop   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   ">      <!-- 组件扫描 -->        <context:component-scan base-package="com"></context:component-scan>        <!--         注意:必须加入mvc注解扫描 否则报         Could not obtain transaction-synchronized Session for current thread        -->        <mvc:annotation-driven></mvc:annotation-driven>       <!--引入 jdbc.properties -->        <context:property-placeholder            location="classpath:jdbc.properties"        />       <!-- 配置 data source -->       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">            <property name="url" value="${url}"></property>            <property name="username" value="${name}"></property>            <property name="password" value="${pass}"></property>            <property name="driverClassName" value="${driver}"></property>       </bean>        <!-- 配置工厂 -->        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">            <property name="dataSource" ref="dataSource"></property>            <!-- hibernate.cfg.xml                 里面是 hibernate的一些属性设置                      org.hibernate.dialect.H2Dialect                    hibernate.show_sql                    hibernate.format_sql            -->            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>            <!-- hibernate 对象关系映射文件路径 -->            <property name="mappingLocations" value="classpath:com/book/pojo/*.hbm.xml"></property>        </bean>        <!-- 配置事务管理 -->         <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">              <property name="sessionFactory" ref="sessionFactory"></property>          </bean>          <!-- spring 6  2. 配置事务属性 -->          <tx:advice id="txAdvice" transaction-manager="transactionManager" >          <!-- 定义对每个方法的事务控制属性 -->              <tx:attributes>                    <!-- 要开启事务的方法名称 -->                  <tx:method name="add*" propagation="REQUIRED"/>                  <tx:method name="*" />              </tx:attributes>          </tx:advice>          <!-- spring 6 3. 配置事务切入点, 再把事务属性和事务切入点关联起来 -->          <aop:config>              <aop:pointcut expression="execution(* com.book.service.*.*(..))" id="txPointcut"/>              <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>          </aop:config>  </beans>

2.dispatcherServlet.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:p="http://www.springframework.org/schema/p"  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-3.1.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.1.xsd    http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    <!-- 组件扫描 -->    <context:component-scan base-package="com"></context:component-scan>  </beans>

3.把spring-hibernate.xml 和 dispatcherServlet.xml 跟 web.xml放置同一目录

4.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>ssh</display-name>  <servlet>    <servlet-name>dispatcherServlet</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>        <param-name>contextConfigLocation</param-name>        <param-value>/WEB-INF/dispatcherServlet.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>dispatcherServlet</servlet-name>    <url-pattern>*.ssh</url-pattern>  </servlet-mapping>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/spring-hibernate.xml</param-value>  </context-param>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>

5.hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?><!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>        <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>        <property name="hibernate.show_sql">true</property>        <property name="hibernate.format_sql">true</property>    </session-factory></hibernate-configuration>

6 log4j.properties

log4j.rootLogger=debug,consolelog4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.layout=org.apache.log4j.SimpleLayout

7 SupDao.java

package com.book.dao;import javax.annotation.Resource;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.springframework.orm.hibernate4.support.HibernateDaoSupport;import org.springframework.stereotype.Service;@Servicepublic class SupDao extends HibernateDaoSupport{    @Resource(name="sessionFactory")    public void setTemplate(SessionFactory sessionFactory){        super.setSessionFactory(sessionFactory);;    }    public Session getSession(){        return getSessionFactory().getCurrentSession();    }}
0 0
原创粉丝点击