您好,登錄后才能下訂單哦!
配置文件如下:
base-context.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--用于激活容器中注冊(cè)的bean--> <!--<context:annotation-config/>--> <context:property-placeholder location="classpath*:/props/*.properties" ignore-unresolvable="true"/> <context:component-scan base-package="com.ufind.server.*"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
db-mybatis.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"/> </bean> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ufind.server.infra.repository.sql"/> <property name="sqlSessionFactoryBeanName" value="sessionFactory"/> </bean> </beans>
persistence-context.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 數(shù)據(jù)庫(kù)基本信息配置 --> <property name="driverClassName" value="${db.jdbc.driver}"/> <property name="url" value="${db.jdbc.connection.url}"/> <property name="username" value="${db.jdbc.username}"/> <property name="password" value="${db.jdbc.password}"/> <!-- 初始化連接數(shù)量 --> <property name="initialSize" value="10"/> <!-- 最大并發(fā)連接數(shù) --> <property name="maxActive" value="100"/> <!-- 最小空閑連接數(shù) --> <property name="minIdle" value="20"/> <!-- 配置獲取連接等待超時(shí)的時(shí)間 --> <property name="maxWait" value="5000"/> <!-- 超過(guò)時(shí)間限制是否回收 --> <property name="removeAbandoned" value="true"/> <!-- 超過(guò)時(shí)間限制多長(zhǎng); --> <property name="removeAbandonedTimeout" value="120000"/> <!-- 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="40000"/> <!-- 用來(lái)檢測(cè)連接是否有效的sql,要求是一個(gè)查詢語(yǔ)句--> <property name="validationQuery" value="select 1"/> <!-- 申請(qǐng)連接的時(shí)候檢測(cè) --> <property name="testWhileIdle" value="true"/> <!-- 申請(qǐng)連接時(shí)執(zhí)行validationQuery檢測(cè)連接是否有效,配置為true會(huì)降低性能 --> <property name="testOnBorrow" value="false"/> <!-- 歸還連接時(shí)執(zhí)行validationQuery檢測(cè)連接是否有效,配置為true會(huì)降低性能 --> <property name="testOnReturn" value="false"/> <!-- 打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小 --> <property name="poolPreparedStatements" value="true"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="50"/> <!--屬性類(lèi)型是字符串,通過(guò)別名的方式配置擴(kuò)展插件,常用的插件有: 監(jiān)控統(tǒng)計(jì)用的filter:stat 日志用的filter:log4j 防御SQL注入的filter:wall --> <property name="filters" value="stat"/> </bean> </beans>
在mappers下邊是mybatis的xml文件,啟動(dòng)的時(shí)候出現(xiàn)錯(cuò)誤:
DEBUG o.m.spring.SqlSessionFactoryBean - Property 'configLocation' not specified, using default MyBatis Configuration
解決方式如下:
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"/> <property name="configLocation" value="classpath:spring/persistence-context.xml"/> </bean>
在sessionFactory下加入:
<property name="configLocation" value="classpath:spring/persistence-context.xml"/>
添加persistence-context.xml
的位置即可,或者所有的文件都在一個(gè)文件即可
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)億速云的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。