springmvc-spring4.3-hibernate5框架整合

来源:互联网 发布:淘宝福袋退货 编辑:程序博客网 时间:2024/05/21 15:03

                                                             springmvc-spring4.3-hibernate5三大框架整合

       为了跟上时代的潮流,我决定用hibernate5,同时也复习一下以前所学的知识,这整合也费了我一番功夫,所以我决定将这个项目的主要部分记录下来,一方面做为以后的参考,一方面分享给广大的java学习爱好者 
      
       1、首先是properties文件和xml文件

       


       2、其次是引入jar包,这部分在我上传的工程里可以看到,所以先不截图了


       
      3、根据mvc架构建几个包,写好对应的类
              
      
       这里我简单的展示一下xml文件是怎么配置的吧

      1). spring-mvc-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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"
default-autowire="byName" default-lazy-init="false">


<!-- 自动扫描dao和service包(自动注入) -->
<context:component-scan base-package="com.ssh.dao" />
<context:component-scan base-package="com.ssh.service" />
<context:component-scan base-package="com.ssh.daoImpl" />
<context:component-scan base-package="com.ssh.serviceImpl" />
<!-- 加载service,此时要排除要controller,因为controller已经spring-mvc中加载过了 -->
<context:component-scan base-package="com.ssh.controller">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>


<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:config/properties/dbconfig.properties" />


<!-- 配置数据源1 -->
<bean name="dataSource"  class="org.apache.commons.dbcp.BasicDataSource"  
destroy-method="close">
<property name="driverClassName" value="${diver_name}"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="defaultAutoCommit" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="10000" />
<property name="minEvictableIdleTimeMillis" value="10000" />
<property name="validationQuery" value="select 1" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
</bean>   
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
   <property name="hibernateProperties"> 
<props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.format_sql">true</prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>   
</props>
</property>
<!-- 注解方式配置 -->
<property name="packagesToScan">
<list>
<value>com.ssh.entity</value>
</list>
</property>
</bean>


<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory" /> 
    </bean>
<tx:annotation-driven transaction-manager="transactionManager" />  
    
    <!-- 配置事务异常封装 -->
    <bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
  
 <aop:config>
  <!-- 通过aop定义事务增强切面 -->
 <aop:pointcut expression="execution(* com.ssh.serviceImpl.*.*(..))" id="pointcut" /> 
 <aop:advisor advice-ref="advice" pointcut-ref="pointcut" /> 
 </aop:config>
 <aop:aspectj-autoproxy/>
 <tx:advice id="advice" transaction-manager="transactionManager">
 <tx:attributes>
     <tx:method name="query*" read-only="true" propagation="REQUIRED" />
              <tx:method name="find*" read-only="true" propagation="REQUIRED" />
              <tx:method name="add*"  propagation="REQUIRED" />
              <tx:method name="save*"  propagation="REQUIRED" />
              <tx:method name="delete*"  propagation="REQUIRED" />
              <tx:method name="update*"  propagation="REQUIRED" />
             <tx:method name="*"  propagation="REQUIRED" />
     </tx:attributes>
 </tx:advice>
</beans>

2). spring-mvc-hibernate.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
         http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
http://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws-4.3.xsd">

<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
<!-- 加载controller的时候,不加载service,因为此时事物并未生效,若此时加载了service,那么事物无法对service进行拦截 -->
<context:component-scan base-package="com.ssh.controller">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

<!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
<bean id="defaultViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="contentType" value="text/html" />
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<!-- 设置返回json的字符编码 -->
<bean id="mappingJacksonHttpMessageConverter"
 class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
  <list>
  <value>text/html;charset=UTF-8</value>
  </list>
  </property>
</bean> 

<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射, 配置一个基于注解的定制的WebBindingInitializer,解决日期转换问题,方法级别的处理器映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="cacheSeconds" value="0" />
<property name="messageConverters">
<list >
  <ref bean="mappingJacksonHttpMessageConverter" />
  </list>
</property>
</bean>

    <!-- 上传  -->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
            id="multipartResolver">  
        <property name="resolveLazily" value="true" />  
        <property name="maxUploadSize" value="20971520" />  
        <property name="defaultEncoding" value="utf-8" />       
    </bean>  
</beans>

3). 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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SSHProject</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- 加载所有的配置文件 -->  
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:config/xml/spring-*.xml</param-value>  
  </context-param>
  <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:config/properties/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
  <!-- 配置Spring监听 -->  
  <listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  </listener>  
  <!-- 配置SpringMVC -->  
  <servlet>  
    <servlet-name>springMVC</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath*:config/xml/spring-mvc.xml</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
   <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.shtml</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.jpg</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.png</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.gif</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.js</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.css</url-pattern>
  </servlet-mapping>  
  <error-page>
    <error-code>404</error-code>
    <location>/page/common/404.jsp</location>
  </error-page> 
  
  <!-- 配置字符集 -->  
  <filter>  
    <filter-name>encodingFilter</filter-name>  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    <init-param>  
        <param-name>encoding</param-name>  
        <param-value>UTF-8</param-value>  
    </init-param>  
    <init-param>  
        <param-name>forceEncoding</param-name>  
        <param-value>true</param-value>  
    </init-param>  
  </filter>  
  <filter-mapping>  
    <filter-name>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping> 
  
  <!-- 配置Session -->  
  <filter>  
    <filter-name>openSession</filter-name>  
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>  
    <init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
  </filter>  
  <filter-mapping>  
    <filter-name>openSession</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping>   
</web-app>

以上就是主要xml文件的详细配置,我搭的这个框架主要实现了ajax,hibernate注解生成表,静态资源拦截功能,最重要的是这个框架真正实现了getcurrentSession事务管理功能,这是我搭这个框架所要达到的最终目的,就是用hibernate5来管理事务,哈哈,终于跟上时代的潮流了!!!





0 0
原创粉丝点击