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引入其他配置,这是一份拼接的配置文件

原文地址:https://www.cnblogs.com/liuyangfirst/p/9916254.html