zoukankan      html  css  js  c++  java
  • spring4+hibernate4 整合

    1.web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0">
    
        <!-- 设置由Sprng载入的Log4j配置文件位置 -->
        <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>/WEB-INF/conf/log4j.properties</param-value>
        </context-param>
        <!-- Spring刷新Log4j配置文件变动的间隔,单位为毫秒 -->
        <context-param>
            <param-name>log4jRefreshInterval</param-name>
            <param-value>10000</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
    
        <filter>
            <description>URL encoding filter</description>
            <display-name>URL Encoding filter</display-name>
            <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <servlet>
            <servlet-name>Dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>Dispatcher</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    
    </web-app>

    2. spring-mvc.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    
        <context:property-placeholder location="/WEB-INF/conf/my.properties"/>
        <context:component-scan base-package="outfox.course.weixinkaoshen"/>
    
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
              destroy-method="close">
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <property name="minPoolSize" value="5"/>
            <property name="maxPoolSize" value="50"/>
            <property name="checkoutTimeout" value="5000"/>
            <property name="maxStatements" value="100"/>
            <property name="preferredTestQuery" value="SELECT 1"/>
            <property name="testConnectionOnCheckout" value="true"/>
            <property name="idleConnectionTestPeriod" value="1800"/>
            <property name="acquireIncrement" value="2"/>
            <property name="maxIdleTime" value="600"/>
        </bean>
        <bean id="sessionFactory"
              class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="packagesToScan" value="com.example.data.storage"/>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">false</prop>
                </props>
            </property>
        </bean>
        <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
        <bean id="transactionManager"
              class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="create*" propagation="REQUIRED"/>
                <tx:method name="save*" propagation="REQUIRED"/>
                <tx:method name="add*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="remove*" propagation="REQUIRED"/>
                <tx:method name="del*" propagation="REQUIRED"/>
                <tx:method name="import*" propagation="REQUIRED"/>
                <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true"/>
            </tx:attributes>
        </tx:advice>
    
        <!-- 定义切面,在 * com.example.myproject.dao.*Dao.*(..) 中执行有关的hibernate session的事务操作 -->
        <aop:config proxy-target-class="true">
            <aop:pointcut id="serviceOperation" expression="execution(* com.exaple.myproject.dao.*Dao.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
        </aop:config>
    
        <!-- 处理请求的函数返回String类型时自动当做json处理 -->
        <mvc:annotation-driven>
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <!-- <constructor-arg ref="utf8Charset"/> -->
                    <!-- <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" /> -->
                    <property name="writeAcceptCharset" value="false"/>
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/plain;charset=UTF-8</value>
                            <value>text/html;charset=UTF-8</value>
                            <!--<value>text/json;charset=UTF-8</value>-->
                        </list>
                    </property>
                </bean>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/json;charset=UTF-8</value>
                        </list>
                    </property>
    
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
    
    </beans>

    3. applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    </beans>

    4. log4j.properties

    log4j.rootLogger=info, console
    
    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.Target=System.out
    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    log4j.appender.console.layout.ConversionPattern=%d{yyyyMMdd-HHmmss} %p %l %m%n
    
    log4j.logger.com.example.myproject=info
  • 相关阅读:
    【Hadoop环境搭建】Centos6.8搭建hadoop伪分布模式
    【Java安装】Centos6.8 安装Java1.6
    【SVN】win7 搭建SVN服务器
    【KVM】Ubuntu14.04 安装KVM
    【VNC】Ubuntu14.04LTS下安装VNC View
    【虚拟化】支持IDE/SATA/SCSI
    【KVM安装】在Centos6.8中安装KVM
    【脚本】新增未扩展磁盘容量
    【SVN】自动定时更新
    【GIS】地球经纬度和米换算(转)
  • 原文地址:https://www.cnblogs.com/ylty/p/7718017.html
Copyright © 2011-2022 走看看