1. 程式人生 > >MyBatis-Plus 多庫部署方式;spring mvc 多庫部署方式

MyBatis-Plus 多庫部署方式;spring mvc 多庫部署方式

1、實現mybatis-plus的多個數據庫的切換方式

     原始碼地址:https://github.com/baomidou/mybatisplus-spring-mvc

2、因為其文件都是相互依賴的,所以修改配置,就是在已有的配置中修改

     這裡配置多資料來源採用定義不同的profile方式修改啟動時連線的資料庫

     原版

       配置檔案位置:https://github.com/baomidou/mybatisplus-spring-mvc/tree/master/src/main/resources/spring

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
 5
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
> 6 7 <!-- 引入屬性檔案 --> 8 <context:property-placeholder location="classpath:config.properties"/> 9 10 <!-- Service包(自動注入) --> 11 <context:component-scan base-package="com.baomidou.springmvc.service"/> 12 13 <import resource="classpath:spring/spring-mybatis.xml"/> 14 </beans>
View Code

      修改後配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:util="http://www.springframework.org/schema/util"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7        http://www.springframework.org/schema/beans/spring-beans.xsd
 8        http://www.springframework.org/schema/context
 9        http://www.springframework.org/schema/context/spring-context.xsd
10        http://www.springframework.org/schema/util
11        http://www.springframework.org/schema/util/spring-util.xsd" profile="development">
12 
13     <!-- 引入屬性檔案 -->
14     <!--<context:property-placeholder location="classpath:applicationContext-profile.xml"/>-->
15     <!--<context:property-placeholder location="classpath:config.properties"/>-->
16 
17 
18     <beans profile="development">
19         <context:property-placeholder
20                 location="classpath*:common/*.properties,classpath*:development/*.properties"/>
21         <!-- Service包(自動注入) -->
22         <!-- 修改了package路徑 -->
23         <context:component-scan base-package="com.rent.springmvc.service"/>
24         <!--匯入mybatis-->
25         <import resource="classpath:spring/spring-mybatis.xml"/>
26     </beans>
27 
28 
29     <!-- 測試環境配置檔案 -->
30     <beans profile="test">
31         <context:property-placeholder
32                 location="classpath*:common/*.properties,classpath*:test/*.properties"/>
33 
34         <!-- Service包(自動注入) -->
35         <!-- 修改了package路徑 -->
36         <context:component-scan base-package="com.rent.springmvc.service"/>
37         <!--匯入mybatis-->
38         <import resource="classpath:spring/spring-mybatis.xml"/>
39     </beans>
40 
41     <!-- 生產環境配置檔案 -->
42     <beans profile="production">
43         <context:property-placeholder
44                 location="classpath*:common/*.properties,classpath*:production/*.properties"/>
45 
46         <!-- Service包(自動注入) -->
47         <!-- 修改了package路徑 -->
48         <context:component-scan base-package="com.rent.springmvc.service"/>
49         <!--匯入mybatis-->
50         <import resource="classpath:spring/spring-mybatis.xml"/>
51     </beans>
52 
53 </beans>
View Code

      專案路徑截圖

  

    development中寫修改的

1 validationQuery=SELECT 1
2 ###############本地資料庫
3 jdbc_url=jdbc\:mysql\://localhost\:3306/renttest?characterEncoding\=UTF-8
4 ##jdbc_url=jdbc\:mysql\://192.168.31.255\:3306/test?characterEncoding\=UTF-8
5 jdbc_username=root
6 jdbc_password=123456
View Code

      3、beans可以巢狀beans,這是核心配置

<beans profile="development">
<context:property-placeholder
location="classpath*:common/*.properties,classpath*:development/*.properties"/>
<!-- Service包(自動注入) -->
<!-- 修改了package路徑 -->
<context:component-scan base-package="com.rent.springmvc.service"/>
<!--匯入mybatis-->
<import resource="classpath:spring/spring-mybatis.xml"/>

</beans>

     import引入其他配置,這是一份拼接的配置檔案