spring,mybatis整合配置文件
Posted anqli_java
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring,mybatis整合配置文件相关的知识,希望对你有一定的参考价值。
最近因为项目要求整合了spring+mybatis架构进行项目开发,现将相关整合配置文件整理如下:
基本架构:spring+springmvc+mybatis
分布式框架:dubbo+zookeeper
数据库:mysql
数据库连接池:Druid
1 数据库连接配置信息jdbc.properties
1 #mysql version database druid
2 # setting
3 validationQuery=SELECT 1
4 jdbc.driverClassName=com.mysql.jdbc.Driver
5 jdbc.url=jdbc:mysql://localhost:3306/ivan?useUnicode=true&characterEncoding=utf-8&useSSL=true
6 jdbc.username=root
7 jdbc.password=root
2 spring配置文件spring-register.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans.xsd
6 http://code.alibabatech.com/schema/dubbo
7 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
8
9 <!--引入配置文件-->
10 <!-- <context:property-placeholder location="classpath:config/jdbc.properties"/>-->
11 <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
12 <property name="locations">
13 <list>
14 <value>classpath:config/zookeeper.properties</value>
15 <value>classpath:config/jdbc.properties</value>
16 <value>classpath:config/log4j.properties</value>
17 </list>
18 </property>
19 </bean>
20 <bean id="userService" class="com.ivan.dubbo.service.impl.UserServiceImpl"/>
21 <!--提供方应用信息,用于计算依赖关系-->
22 <dubbo:application name="ivan-dubbo-server"></dubbo:application>
23
24 <!--使用zookeeper广播注册中心暴露服务地址-->
25 <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
26
27 <!-- 本机 伪集群 测试 -->
28 <!-- <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:4170?backup=127.0.0.1:4180,127.0.0.1:4190" />-->
29 <!-- <dubbo:registry protocol="zookeeper" address="127.0.0.1:4170,127.0.0.1:4180,127.0.0.1:4190"/>-->
30
31 <!-- <dubbo:registry id="ivan-dubbo-server1" protocol="zookeeper" address="127.0.0.1:4170" />
32 <dubbo:registry id="ivan-dubbo-server2" protocol="zookeeper" address="127.0.0.1:4180" />
33 <dubbo:registry id="ivan-dubbo-server3" protocol="zookeeper" address="127.0.0.1:4190" />
34 -->
35 <!---使用dubbo协议在20880端口暴露服务-->
36 <dubbo:protocol name="dubbo" port="20880"/>
37
38 <!--
39 官方注释:扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类。
40 测试发现:此处package不填写包名会无法注册Service,扫描全包需填写包首即可或者填写至类的上一级目录。
41 -->
42 <dubbo:annotation package="com"/>
43
44 <dubbo:service interface="com.ivan.service.provider.UserService" ref="userService" timeout="1200000"></dubbo:service>
45
46 </beans>
3 spring-mybatis.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
6 http://www.springframework.org/schema/tx
7 http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
8 <!--配置数据源-->
9 <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
10 <property name="driverClassName" value="${jdbc.driverClassName}" />
11 <property name="url" value="${jdbc.url}"/>
12 <property name="username" value="${jdbc.username}"/>
13 <property name="password" value="${jdbc.password}"/>
14
15 <!--初始化连接池大小-->
16 <property name="initialSize" value="5"/>
17 <property name="maxActive" value="20"/>
18
19 <!--连接池最小空闲-->
20 <property name="minIdle" value="0"/>
21 <!--获取连接池最大等待时间-->
22 <property name="maxWait" value="60000"/>
23 <property name="poolPreparedStatements" value="true"/>
24 <property name="maxPoolPreparedStatementPerConnectionSize" value="33"/>
25
26 <!--检测有效sql-->
27 <property name="validationQuery" value="${validationQuery}"/>
28 <property name="testOnBorrow" value="false"/>
29 <property name="testOnReturn" value="false"/>
30 <property name="testWhileIdle" value="true"/>
31
32 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
33 <property name="timeBetweenEvictionRunsMillis" value="60000"/>
34 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
35 <property name="minEvictableIdleTimeMillis" value="25200000"/>
36 <!-- 打开removeAbandoned功能 -->
37 <property name="removeAbandoned" value="true"/>
38 <!-- 1800秒,也就是30分钟 -->
39 <property name="removeAbandonedTimeout" value="1800"/>
40 <!-- 关闭abanded连接时输出错误日志 -->
41 <property name="logAbandoned" value="true"/>
42 <!-- 监控数据库 -->
43 <property name="filters" value="mergeStat"/>
44
45 </bean>
46 <!--MyBatis配置文件-->
47 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
48 <property name="dataSource" ref="dataSource"/>
49 <property name="mapperLocations" value="classpath*:com/ivan/**/mapping/*.xml"/>
50 </bean>
51 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
52 <property name="basePackage" value="com.ivan.dubbo.service.dao.mapper"/>
53 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
54 </bean>
55 <!--配置事务管理-->
56 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
57 <property name="dataSource" ref="dataSource"/>
58 </bean>
59 <!--注解方式配置事务-->
60 <!-- <tx:annotation-driven transaction-manager="transactionManager"/>-->
61 <!-- 拦截器方式配置事物 -->
62 <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
63 <tx:attributes>
64 <tx:method name="insert*" propagation="REQUIRED"/>
65 <tx:method name="update*" propagation="REQUIRED"/>
66 <tx:method name="delete*" propagation="REQUIRED"/>
67
68 <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
69 <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
70 <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
71
72 </tx:attributes>
73 </tx:advice>
74 <!--
75 <span style="font-family:FangSong_GB2312;"> </span>Spring aop事务管理
76 <span style="font-family:FangSong_GB2312;"> 此处配置正确无法发布提供者服务,目前没找到解决方案</span>
77
78 -->
79 <!-- <aop:config>
80 <aop:pointcut id="transactionPointcut" expression="execution(* com.ivan..service.impl.*Impl.*(..))" />
81 <aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointcut"/>
82 </aop:config> -->
83
84 </beans>
以上是关于spring,mybatis整合配置文件的主要内容,如果未能解决你的问题,请参考以下文章