公司要求,需要使用两个数据库,一个mysql,一个oracle。所以需要配置两个数据库来进行操作。
1.首先,需要在jdbc.properties文件中将两个库的配置数据写入,不过一个写driver,另一个写driver2,区别两个库的变量名。
代码如下:
#oracle web
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.3.4:1521:ORCL
jdbc.username=abc
jdbc.password=adasdsa
#mysql
jdbc.driver2=com.mysql.jdbc.Driver
jdbc.url2=jdbc:mysql://192.168.3.4:3306/logcount?useUnicode=true&characterEncoding=utf-8
jdbc.username2=root
jdbc.password2=1234565
2.在spring-mybatis.xml中的配置:
先开启注解模式
两个数据库分别配置不同id的DataSource
就是配置切换数据库的自定义类的路径,选中默认数据库。
配置aop拦截dao层的所有访问接口,在dao层注解更改数据库。
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!-- 开启注解模式 -->
<context:annotation-config />
<context:component-scan base-package="com.shiyanlou" />
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- oracle数据库 -->
<bean )String city);
}
这个注解是跟spring-mybatis.xml中配置的targetDataSources中的entry key value有关系。
这里面好多具体配置我也不是特别懂,但是这样配置是可以使用的。大家可以试一下。
---------------------
作者:不会code的coder
来源:CSDN
原文:https://blog.csdn.net/qq_36746327/article/details/81033404
版权声明:本文为博主原创文章,转载请附上博文链接!
相关文章: