欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring与mybatis整合配置文件

程序员文章站 2024-03-31 17:41:16
最近因为项目要求整合了spring+mybatis架构进行项目开发,现将相关整合配置文件整理如下: 基本架构:spring+springmvc+mybatis 分布式框...

最近因为项目要求整合了spring+mybatis架构进行项目开发,现将相关整合配置文件整理如下:

基本架构:spring+springmvc+mybatis

分布式框架:dubbo+zookeeper

数据库:mysql

数据库连接池:druid

1 数据库连接配置信息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"/>
 <!--提供方应用信息,用于计算依赖关系-->
 <dubbo:application name="ivan-dubbo-server"></dubbo:application>
 <!--使用zookeeper广播注册中心暴露服务地址-->
  <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
 <!-- 本机 伪集群 测试 -->
 <!-- <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协议在20880端口暴露服务-->
 <dubbo:protocol name="dubbo" port="20880"/>
 <!--
    官方注释:扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前applicationcontext中所有的类。
    测试发现:此处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">
 <!--配置数据源-->
 <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"/>
  <!--获取连接池最大等待时间-->
  <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"/>
  <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  <property name="timebetweenevictionrunsmillis" value="60000"/>
  <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  <property name="minevictableidletimemillis" value="25200000"/>
  <!-- 打开removeabandoned功能 -->
  <property name="removeabandoned" value="true"/>
  <!-- 1800秒,也就是30分钟 -->
  <property name="removeabandonedtimeout" value="1800"/>
  <!-- 关闭abanded连接时输出错误日志 -->
  <property name="logabandoned" value="true"/>
  <!-- 监控数据库 -->
  <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>
 <!--配置事务管理-->
 <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
  <property name="datasource" ref="datasource"/>
 </bean>
 <!--注解方式配置事务-->
<!-- <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事务管理
<span style="font-family:fangsong_gb2312;">       此处配置正确无法发布提供者服务,目前没找到解决方案</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>

总结

以上所述是小编给大家介绍的spring与mybatis整合配置文件,希望对大家有所帮助