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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
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"> <!-- 引入属性文件 -->
<context:property-placeholder location="classpath:config.properties"/> <!-- Service包(自动注入) -->
<context:component-scan base-package="com.baomidou.springmvc.service"/> <import resource="classpath:spring/spring-mybatis.xml"/>
</beans>
修改后配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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" profile="development"> <!-- 引入属性文件 -->
<!--<context:property-placeholder location="classpath:applicationContext-profile.xml"/>-->
<!--<context:property-placeholder location="classpath:config.properties"/>--> <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> <!-- 测试环境配置文件 -->
<beans profile="test">
<context:property-placeholder
location="classpath*:common/*.properties,classpath*:test/*.properties"/> <!-- Service包(自动注入) -->
<!-- 修改了package路径 -->
<context:component-scan base-package="com.rent.springmvc.service"/>
<!--导入mybatis-->
<import resource="classpath:spring/spring-mybatis.xml"/>
</beans> <!-- 生产环境配置文件 -->
<beans profile="production">
<context:property-placeholder
location="classpath*:common/*.properties,classpath*:production/*.properties"/> <!-- Service包(自动注入) -->
<!-- 修改了package路径 -->
<context:component-scan base-package="com.rent.springmvc.service"/>
<!--导入mybatis-->
<import resource="classpath:spring/spring-mybatis.xml"/>
</beans> </beans>
项目路径截图
development中写修改的
validationQuery=SELECT 1
###############本地数据库
jdbc_url=jdbc\:mysql\://localhost\:3306/renttest?characterEncoding\=UTF-8
##jdbc_url=jdbc\:mysql\://192.168.31.255\:3306/test?characterEncoding\=UTF-8
jdbc_username=root
jdbc_password=123456
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引入其他配置,这是一份拼接的配置文件