spring與mybatis整合配置文件
最近因?yàn)轫?xiàng)目要求整合了spring+mybatis架構(gòu)進(jìn)行項(xiàng)目開發(fā),現(xiàn)將相關(guān)整合配置文件整理如下:
基本架構(gòu):spring+springmvc+mybatis
分布式框架:dubbo+zookeeper
數(shù)據(jù)庫:mysql
數(shù)據(jù)庫連接池:Druid
1 數(shù)據(jù)庫連接配置信息jdbc.properties
#mysql version database druid # setting validationQuery=SELECT 1 jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ivan?useUnicode=true&characterEncoding=utf-8&useSSL=true jdbc.username=root jdbc.password=root
2 spring配置文件spring-register.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--引入配置文件-->
<!-- <context:property-placeholder location="classpath:config/jdbc.properties"/>-->
<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/zookeeper.properties</value>
<value>classpath:config/jdbc.properties</value>
<value>classpath:config/log4j.properties</value>
</list>
</property>
</bean>
<bean id="userService" class="com.ivan.dubbo.service.impl.UserServiceImpl"/>
<!--提供方應(yīng)用信息,用于計(jì)算依賴關(guān)系-->
<dubbo:application name="ivan-dubbo-server"></dubbo:application>
<!--使用zookeeper廣播注冊中心暴露服務(wù)地址-->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<!-- 本機(jī) 偽集群 測試 -->
<!-- <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:4170?backup=127.0.0.1:4180,127.0.0.1:4190" />-->
<!-- <dubbo:registry protocol="zookeeper" address="127.0.0.1:4170,127.0.0.1:4180,127.0.0.1:4190"/>-->
<!-- <dubbo:registry id="ivan-dubbo-server1" protocol="zookeeper" address="127.0.0.1:4170" />
<dubbo:registry id="ivan-dubbo-server2" protocol="zookeeper" address="127.0.0.1:4180" />
<dubbo:registry id="ivan-dubbo-server3" protocol="zookeeper" address="127.0.0.1:4190" />
-->
<!---使用dubbo協(xié)議在20880端口暴露服務(wù)-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--
官方注釋:掃描注解包路徑,多個(gè)包用逗號分隔,不填pacakge表示掃描當(dāng)前ApplicationContext中所有的類。
測試發(fā)現(xiàn):此處package不填寫包名會無法注冊Service,掃描全包需填寫包首即可或者填寫至類的上一級目錄。
-->
<dubbo:annotation package="com"/>
<dubbo:service interface="com.ivan.service.provider.UserService" ref="userService" timeout="1200000"></dubbo:service>
</beans>
3 spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!--配置數(shù)據(jù)源-->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--初始化連接池大小-->
<property name="initialSize" value="5"/>
<property name="maxActive" value="20"/>
<!--連接池最小空閑-->
<property name="minIdle" value="0"/>
<!--獲取連接池最大等待時(shí)間-->
<property name="maxWait" value="60000"/>
<property name="poolPreparedStatements" value="true"/>
<property name="maxPoolPreparedStatementPerConnectionSize" value="33"/>
<!--檢測有效sql-->
<property name="validationQuery" value="${validationQuery}"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<property name="testWhileIdle" value="true"/>
<!-- 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000"/>
<!-- 打開removeAbandoned功能 -->
<property name="removeAbandoned" value="true"/>
<!-- 1800秒,也就是30分鐘 -->
<property name="removeAbandonedTimeout" value="1800"/>
<!-- 關(guān)閉abanded連接時(shí)輸出錯(cuò)誤日志 -->
<property name="logAbandoned" value="true"/>
<!-- 監(jiān)控?cái)?shù)據(jù)庫 -->
<property name="filters" value="mergeStat"/>
</bean>
<!--MyBatis配置文件-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:com/ivan/**/mapping/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ivan.dubbo.service.dao.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--配置事務(wù)管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--注解方式配置事務(wù)-->
<!-- <tx:annotation-driven transaction-manager="transactionManager"/>-->
<!-- 攔截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--
<span style="font-family:FangSong_GB2312;"> </span>Spring aop事務(wù)管理
<span style="font-family:FangSong_GB2312;"> 此處配置正確無法發(fā)布提供者服務(wù),目前沒找到解決方案</span>
-->
<!-- <aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.ivan..service.impl.*Impl.*(..))" />
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointcut"/>
</aop:config> -->
</beans>
總結(jié)
以上所述是小編給大家介紹的spring與mybatis整合配置文件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java請求流量合并和拆分提高系統(tǒng)的并發(fā)量示例
這篇文章主要為大家介紹了Java請求流量合并和拆分提高系統(tǒng)的并發(fā)量示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
解決idea 項(xiàng)目編譯后沒有class文件的問題
這篇文章主要介紹了解決idea 項(xiàng)目編譯后沒有class文件的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Spring Boot 2結(jié)合Spring security + JWT實(shí)現(xiàn)微信小程序登錄
這篇文章主要介紹了Spring Boot 2結(jié)合Spring security + JWT實(shí)現(xiàn)微信小程序登錄,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Java實(shí)戰(zhàn)之仿天貓商城系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Java制作一個(gè)基于SSM框架的迷你天貓商城系統(tǒng),文中采用的技術(shù)有JSP、Springboot、SpringMVC、Spring等,需要的可以參考一下2022-03-03

