springmvc(五)整合阿里 druid資料庫連接池和事務等配置,整合mybatis__資料庫

來源:互聯網
上載者:User

感謝我們的小領導,他在研究,我們在套用,他走了以後再沒有完善過,一直沿用至今。如果看這裡的朋友有什麼需要整合進來的,不吝賜教,謝謝各位了。

<?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: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.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.0.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.xsd
                        ">
    <!-- 如下配置中,如果配置了include-filter type=annotation 那麼只有在其中配置的註解會被掃描,前提是-->
    <context:component-scan base-package="com.net"></context:component-scan>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=""></property>
    </bean>
    <!-- 只有配置了如下標籤,@RequestMapping中的produces才會起作用,跟其註冊的訊息體轉換類有關 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean
                class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes" value="application/json" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <mvc:default-servlet-handler />
    <!-- location中的內容可以寫多個地址,比如可以這麼寫location="/,/**"前一個表示在根目錄尋找 -->
    <mvc:resources mapping="/upload/**" location="/upload/,/CKEditorUploadImage/" />
    <mvc:resources mapping="/images/**" location="/WEB-INF/images/" />
    <mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
    <mvc:resources mapping="/plugins/**" location="/WEB-INF/plugins/" />
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
    <mvc:resources mapping="/*.html" location="/WEB-INF/static_pages/" />
    <mvc:resources mapping="/*.xml" location="/WEB-INF/" />
    <mvc:interceptors>
        <bean class="com.net.intercpter.MyInterceptor"></bean>
    </mvc:interceptors>
    
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
        <property name="dataSource" ref="dataSource"></property>
     </bean>
    <!-- 99架構賦值寫法有問題,web請求可以注入屬性檔案的值,代碼測試無法注入,導致junit測試Dao層報載入設定檔出錯 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:dbconfig.properties</value>
            </list>
        </property>
    </bean>
    <!-- 阿里 druid資料庫連接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        <!-- 資料庫基本資料配置 -->
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="driverClassName" value="${driverClassName}" />
        <property name="filters" value="${filters}" />
        <!-- 最大並發串連數 -->
        <property name="maxActive" value="${maxActive}" />
        <!-- 初始化串連數量 -->
        <property name="initialSize" value="${initialSize}" />
        <!-- 配置擷取串連等待逾時的時間 -->
        <property name="maxWait" value="${maxWait}" />
        <!-- 最小空閑串連數 -->
        <property name="minIdle" value="${minIdle}" />
        <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑串連,單位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
        <!-- 配置一個串連在池中最小生存的時間,單位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
        <property name="validationQuery" value="${validationQuery}" />
        <property name="testWhileIdle" value="${testWhileIdle}" />
        <property name="testOnBorrow" value="${testOnBorrow}" />
        <property name="testOnReturn" value="${testOnReturn}" />
        <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
        <!-- 開啟removeAbandoned功能 -->
        <property name="removeAbandoned" value="${removeAbandoned}" />
        <!-- 1800秒,也就是30分鐘 -->
        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
        <!-- 關閉abanded串連時輸出錯誤記錄檔 -->
        <property name="logAbandoned" value="${logAbandoned}" />
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="delete*" propagation="REQUIRED" read-only="false"
                       rollback-for="java.lang.Exception"/>
            <tx:method name="insert*" propagation="REQUIRED" read-only="false"
                       rollback-for="java.lang.Exception" />
            <tx:method name="update*" propagation="REQUIRED" read-only="false"
                       rollback-for="java.lang.Exception" />
            <tx:method name="save*" propagation="REQUIRED" read-only="false"
                       rollback-for="java.lang.Exception" />
        </tx:attributes>
    </tx:advice>
    
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    <!-- 事物處理 -->
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* com.net.crud.service..*(..))" />   <!--目錄結構,該目錄下類註解:@Service("xxxService")-->
        <aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
    </aop:config>
    
    <!-- mybatis配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
        <!-- mapper掃描 -->
        <property name="mapperLocations" value="classpath:mybatis/*/*.xml"></property>
    </bean>
    
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg ref="sqlSessionFactory" />
    </bean>
    
    <!-- 自訂上傳攔截,如最大上傳值及最小上傳值 單個檔案4M,解決原始檔案上傳和spring上傳request被解析的問題-->
        <!-- url中帶有excludeUrls的http請求就不會被multipartResolver先解析-->
    <bean id="multipartResolver" class="com.net.resolver.MyMultipartResolver">
        <property name="excludeUrls">
            <list>
                <value>ckupload</value>
                <value>UEditorConfig</value>
                <!--
                    使用springmvc的檔案上傳實現了,所有暫時不需要轉換request,
                    疑問是不知道百度編輯器的jar中是否有檔案上傳的代碼,因為看到
                    有檔案上傳需要的jar包,而且也沒有得到config.json中配置的檔案格式名稱
                -->
                <!--                <value>UEditorUploadImage</value>-->
            </list>
        </property>
        <!-- 指定所上傳檔案的總大小不能超過16MB。注意maxUploadSize屬性的限制不是針對單個檔案,而是所有檔案的容量之和 -->
        <property name="maxUploadSize">
            <value>647772160</value>
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
        <property name="defaultEncoding">
            <value>utf-8</value>
        </property>
    </bean>
    

</beans>


檔案結構:


mybatis配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 配置實體類的別名,在實體類對應的mapper.xml中返回的類型就可以寫類名,而不需要全類名了 -->
    <typeAliases>
        <package name="com.net.crud.modal" />
    </typeAliases>
</configuration>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.