spring和mybatis整合配置信息

1.spring-mabatis.xml文件,主要是完成 dao層和pojo的自動(dòng)掃描裝配。

<?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">

    <!-- 自動(dòng)掃描 -->
    <context:component-scan base-package="com.df.test" />
    
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <!--或者這樣 引入配置文件 -->
    <!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化連接大小 -->
        <property name="initialSize" value="${initialSize}"/>
        <!-- 連接池最大數(shù)量 -->
        <property name="maxActive" value="${maxActive}"/>
        <!-- 連接池最大空閑 -->
        <property name="maxIdle" value="${maxIdle}"/>
        <!-- 連接池最小空閑 -->
        <property name="minIdle" value="${minIdle}"/>
        <!-- 獲取連接最大等待時(shí)間 -->
        <property name="maxWait" value="${maxWait}"/>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自動(dòng)掃描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:com/df/test/mapping/*.xml"/>
    </bean>

    <!-- DAO接口所在包名,Spring會(huì)自動(dòng)查找其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.df.test.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!-- (事務(wù)管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

2.在此之前,我們要新建配置文件jdbc.properties,用來保存數(shù)據(jù)庫鏈接的相關(guān)信息,如下:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb
username=root
password=123456
#初始定義連接數(shù)                
initialSize=0
#定義最大連接數(shù)
maxActive=20
#定義最大空閑
maxIdle=20
#定義最小空閑
minIdle=1
#定義最長等待時(shí)間
maxWait=60000 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,268評(píng)論 6 342
  • 什么是Spring Spring是一個(gè)開源的Java EE開發(fā)框架。Spring框架的核心功能可以應(yīng)用在任何Jav...
    jemmm閱讀 16,771評(píng)論 1 133
  • 日本網(wǎng)友常常發(fā)起投票活動(dòng),選出你認(rèn)為最美的一日警察署長,紛紛發(fā)出自己認(rèn)為理想的選擇。那么日本獨(dú)有的一日署長到底...
    櫻花君閱讀 6,777評(píng)論 0 1
  • 看《奇葩說》一期節(jié)目,女性專屬停車位是否是一種歧視。蔡康永說得好,他說,是一種歧視,因?yàn)殡S著社會(huì)的進(jìn)步,衣食足知榮...
    煙花易冷冷冷閱讀 389評(píng)論 0 2

友情鏈接更多精彩內(nèi)容