springmvc3.2+spring+hibernate4.2 全注解整合,亲测可用

亲测可用,2014-12-4

 - - - - - - - - - - - - - - - - - - - - - - - 项目所需架包  libs- - - - - - - - - - - - - - - - - - - - - -

antlr-2.7.7.jar

aopalliance-1.0.jar

aspectjweaver-1.5.3.jar

c3p0-0.9.1.jar (连接池)

commons-collections-3.2.1.jar

commons-fileupload-1.2.jar

commons-logging-1.1.3.jar

dom4j-1.6.1.jar

ehcache-core-2.4.3.jar

hibernate-c3p0-4.2.1.Final.jar

hibernate-commons-annotations-4.0.1.Final.jar

hibernate-core-4.2.1.Final.jar

hibernate-ehcache-4.2.1.Final.jar

hibernate-jpa-2.0-api-1.0.1.Final.jar

jandex-1.1.0.Final.jar

javassist-3.15.0-GA.jar

jboss-logging-3.1.0.GA.jar

jboss-transaction-api_1.1_spec-1.0.1.Final.jar

jstl.jar

mchange-commons-java-0.2.3.4.jar

mysql-connector-java-5.1.18-bin.jar (Mysql数据库连接lib)

oscache-2.4.1.jar (缓存lib,根据个人需求)

slf4j-api-1.6.1.jar

spring-aop-3.2.4.RELEASE.jar

spring-aspects-3.2.4.RELEASE.jar

spring-beans-3.2.4.RELEASE.jar

spring-context-3.2.4.RELEASE.jar

spring-context-support-3.2.4.RELEASE.jar

spring-core-3.2.4.RELEASE.jar

spring-expression-3.2.4.RELEASE.jar

spring-jdbc-3.2.4.RELEASE.jar

spring-orm-3.2.4.RELEASE.jar

spring-tx-3.2.4.RELEASE.jar

spring-web-3.2.4.RELEASE.jar

spring-webmvc-3.2.4.RELEASE.jar

standard.jar

 - - - - - - - - - - - - - - - - - - - - - - - - 相关配置文件- - - - - - - - - - - - - - - - - - - - - -


- - - - - - - - -- - - - - - - - - -- - - - - - - - - -web.xml- - - - - - - - - -- - - - - - - - - -- - - - - - -

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
     
     <!-- spring配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/context/spring-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- spring MVC配置 -->
    <servlet>  
        <servlet-name>springmvc</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <init-param>
            <param-name>contextConfigLocation</param-name>               
            <param-value>/WEB-INF/context/servlet-context.xml</param-value>  <!--指定XML文件位置-->
        </init-param> 
        <load-on-startup>4</load-on-startup>
          
    </servlet>  
    <servlet-mapping>  
        <servlet-name>springmvc</servlet-name>  
        <url-pattern>*.html</url-pattern>
        <url-pattern>*.json</url-pattern>  
    </servlet-mapping>
    
     <!-- 设置字符集 -->  
	  <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>openSessionInViewFilter</filter-name>  
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
    <init-param>  
      <param-name>singleSession</param-name>  
      <param-value>true</param-value>  
    </init-param>  
  </filter>  
	    
	  <filter-mapping>  
	    <filter-name>openSessionInViewFilter</filter-name>  
	    <url-pattern>/*</url-pattern>  
	  </filter-mapping>
    
    
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

- - - - - - -- - - - - - - - - -db.properties  关于数据库的配置文件- - - - - - - - - -- - - - - - - - -

db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test
db.username=root
db.password=root

 - - - - - - - - -- - - - - - - - - -servlet-context.xml  : springMvc 配置- - - - - - - - - -- - - - - - - - - -- - - - -

<?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:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    
  <!-- 添加注解驱动 -->  
  <mvc:annotation-driven />
  <!-- 注解扫描的包 -->  
  <context:component-scan base-package="com.test.controller" />
   
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/views/" />
      <property name="suffix" value=".jsp" />
  </bean>
  
  	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 annotation默认的办法映射适配器 -->    
	<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />    
	<bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
	<!-- 上传文件bean --> 
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" />
    
</beans>

 - - - - - - - - -- - - - - - - - - -spring-context.xml : spring 整合 hibernate 配置- - - - - - - - - -- - - - - - - - - -- - - - -


<?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:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context"  
    
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd  
       ">     
       
          <!-- 自动加载构建bean -->  
        <context:component-scan base-package="com.test" /> 
        
        <!-- 加载数据库属性配置文件 -->  
        <context:property-placeholder location="classpath:db.properties" />  
      
        <!-- 数据库连接池c3p0配置 -->  
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
            destroy-method="close">  
            <property name="jdbcUrl" value="${db.url}"></property>  
            <property name="driverClass" value="${db.driverClassName}"></property>  
            <property name="user" value="${db.username}"></property>  
            <property name="password" value="${db.password}"></property>  
            <property name="maxPoolSize" value="40"></property>  
            <property name="minPoolSize" value="1"></property>  
            <property name="initialPoolSize" value="1"></property>  
            <property name="maxIdleTime" value="20"></property>  
        </bean>  
          
        <!-- session工厂 -->  
        <bean id="sessionFactory"  
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
            <property name="dataSource">  
                <ref bean="dataSource" />  
            </property>  
            <property name="hibernateProperties">
            <props>
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
                <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop> 
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hiberante.format_sql">true</prop>
            </props>  
            </property>  
            <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"/>-->
           
            <!-- 扫描映射文件,实体类 -->  
            <property name="packagesToScan">  
                <list>  
                    <!-- 这里,是否可以匹配所有com.test开头,entity 结尾 下所有的实体!? -->  
                    <value>com.test.entity</value>
                </list>
            </property>
        </bean>
        
       <bean id="persistenceDelegate" class="com.test.core.base.DefaultPersistenceDelegate">
        <property name="sessionFactory" ref="sessionFactory"></property>    
        </bean>
        
        <bean id="queryDelegate" class="com.test.core.base.DefaultQueryDelegate">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
          
          <!-- 开启AOP监听 只对当前配置文件有效 -->
          <aop:aspectj-autoproxy expose-proxy="true" />
        
          <!-- 开启注解事务 只对当前配置文件有效 -->
          <tx:annotation-driven transaction-manager="transactionManager" />
          
        <!-- 配置事务管理器 -->  
        <bean id="transactionManager"  
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
            <property name="sessionFactory" ref="sessionFactory" />  
        </bean>  
          
        <!-- 配置事务通知属性 -->  
        <tx:advice id="txAdvice" transaction-manager="transactionManager">  
            <!-- 定义事务传播属性 -->  
            <tx:attributes>  
                <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 -->
                <tx:method name="list*" propagation="REQUIRED"/>
                <tx:method name="del*" propagation="REQUIRED"/>
                <tx:method name="do*" propagation="REQUIRED"/>
                <tx:method name="megor*" propagation="REQUIRED"/>
                <tx:method name="modify*" propagation="REQUIRED"/>
                <tx:method name="execute*" propagation="REQUIRED"/>
                <tx:method name="insert*" propagation="REQUIRED" />  
                <tx:method name="update*" propagation="REQUIRED" />  
                <tx:method name="edit*" propagation="REQUIRED" />  
                <tx:method name="save*" propagation="REQUIRED" />  
                <tx:method name="add*" propagation="REQUIRED" />  
                <tx:method name="new*" propagation="REQUIRED" />  
                <tx:method name="set*" propagation="REQUIRED" />  
                <tx:method name="remove*" propagation="REQUIRED" />  
                <tx:method name="delete*" propagation="REQUIRED" />  
                <tx:method name="change*" propagation="REQUIRED" />  
                <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
                <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
                <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
                <tx:method name="*" propagation="REQUIRED" read-only="true" />  
            </tx:attributes>  
        </tx:advice>  
      
        <!-- 配置事务切面 -->  
        <aop:config>  
            <aop:pointcut id="serviceOperation"  
                expression="execution(* com.test.service..*.*(..))" />  
            <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
            <aop:advisor advice-ref="txAdvice"
            pointcut="execution(* com.test.core.base.EntityServiceImpl.*(..))"/>
        </aop:config>  
      
         
      
    </beans>  


- - - - - - - - - - - -  省略- - -dao(数据层二次封装)- - - service(业务层)- - - entity(实体类)- - - - --controller(控制层)- - - 相关代码, - - - - - - - -- - - - 


 - - - - - - -- - - - - - - - - -此文用于小编在有网络的情况下,快速配置搭建,亲测可用,仅供参考- - - - - - - - - -- - - - - - -



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值