ssm多数据源的操作
公司要求,需要使用兩個(gè)數(shù)據(jù)庫,一個(gè)mysql,一個(gè)oracle。所以需要配置兩個(gè)數(shù)據(jù)庫來進(jìn)行操作。
1.首先,需要在jdbc.properties文件中將兩個(gè)庫的配置數(shù)據(jù)寫入,不過一個(gè)寫driver,另一個(gè)寫driver2,區(qū)別兩個(gè)庫的變量名。
代碼如下:
#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中的配置:
??? 先開啟注解模式
??? 兩個(gè)數(shù)據(jù)庫分別配置不同id的DataSource
??? 就是配置切換數(shù)據(jù)庫的自定義類的路徑,選中默認(rèn)數(shù)據(jù)庫。
??? 配置aop攔截dao層的所有訪問接口,在dao層注解更改數(shù)據(jù)庫。
??? <?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數(shù)據(jù)庫 -->
?? ??? ?<bean id="dataSource_first"
?? ??? ??? ?class="org.springframework.jdbc.datasource.DriverManagerDataSource">
?? ??? ??? ?<property name="driverClassName" value="${jdbc.driver}" />
?? ??? ??? ?<property name="url" value="${jdbc.url}" />
?? ??? ??? ?<property name="username" value="${jdbc.username}" />
?? ??? ??? ?<property name="password" value="${jdbc.password}" />
?? ??? ?</bean>
?? ??? ?<!-- sqlite數(shù)據(jù)庫 -->
?? ??? ?<bean id="dateSource_second"
?? ??? ??? ?class="org.springframework.jdbc.datasource.DriverManagerDataSource">
?? ??? ??? ?<property name="driverClassName" value="${jdbc.driver2}" />
?? ??? ??? ?<property name="url" value="${jdbc.url2}" />
?? ??? ??? ?<property name="username" value="${jdbc.username2}" />
?? ??? ??? ?<property name="password" value="${jdbc.password2}" />
?? ??? ?</bean>
?? ??? ?<!-- 下面的是切換數(shù)據(jù)庫的自定義類 -->
?? ??? ?<bean id="dataSource" class="com.shiyanlou.util.MultipleDataSource">
?? ??? ?<!-- 默認(rèn)使用sqlite數(shù)據(jù)庫 -->
?? ??? ??? ?<property name="defaultTargetDataSource" ref="dateSource_second"></property>
?? ??? ??? ?<property name="targetDataSources">
?? ??? ??? ??? ?<map>
?? ??? ??? ??? ??? ?<entry key="dataSource_first" value-ref="dataSource_first"></entry>
?? ??? ??? ??? ??? ?<entry key="dateSource_second" value-ref="dateSource_second"></entry>
?? ??? ??? ??? ?</map>
?? ??? ??? ?</property>
?? ??? ?</bean>
?? ??? ?<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
?? ??? ??? ?<property name="dataSource" ref="dataSource"></property>
?? ??? ??? ?<property name="mapperLocations" value="classpath:/mappers/*.xml"></property>
?? ??? ??? ?<property name="configLocation" value="classpath:mybatis-config.xml" />
?? ??? ?</bean>
??? ?
?? ??? ?<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
?? ??? ??? ?p:basePackage="com.shiyanlou.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory">
??? ?
?? ??? ?</bean>
??? ?
?? ??? ?<bean id="txManager"
?? ??? ??? ?class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
?? ??? ??? ?<property name="dataSource" ref="dataSource"></property>
?? ??? ?</bean>
??? ?
?? ??? ?<tx:annotation-driven transaction-manager="txManager" />
?? ??? ?<!-- 切面 -->
?? ??? ?<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
?? ??? ?<bean id="dataSourceAspect" class="com.shiyanlou.util.DataSourceAspect"></bean>
?? ??? ??? ?<aop:config>
?? ??? ??? ??? ?<aop:aspect ref="dataSourceAspect">
?? ??? ??? ??? ??? ?<!-- 攔截所有service方法,在dao層添加注解 -->
?? ??? ??? ??? ??? ?<aop:pointcut expression="execution(* com.shiyanlou.dao..*.*(..))" id="dataSourcePointcut"/>
?? ??? ??? ??? ??? ?<aop:before method="intercept" pointcut-ref="dataSourcePointcut"/>
?? ??? ??? ??? ?</aop:aspect>
?? ??? ??? ?</aop:config>
??? </beans>
3.工具類的配置:自定義注解
????????? 注解類DataSource.java
??? /**
???? * <p>Title: DataSource.java</p>? ?
???? * <p>Description: </p> ?
???? * <p>Copyright: Copyright (c) 2018</p>? ?
???? * @author 林猛
???? * @date 2018年5月3日 ?
???? * @version 1.0? ?
??? */ ?
??? package com.shiyanlou.util;
??? ?
??? import java.lang.annotation.ElementType;
??? import java.lang.annotation.Retention;
??? import java.lang.annotation.RetentionPolicy;
??? import java.lang.annotation.Target;
??? ?
??? /**
???? * @author linmeng
???? *
???? */
??? ?
??? /*?? ?@Retention(RetentionPolicy.RUNTIME)
?? ??? ?@Target(ElementType.METHOD)*/
?? ??? ?@Target({ElementType.TYPE,ElementType.METHOD})
?? ??? ?@Retention(RetentionPolicy.RUNTIME)
?? ??? ?public @interface DataSource{
?? ??? ??? ?String value();
?? ??? ?}
??? ?
??? 注解類DataSourceAspect.java
??? /**
???? * <p>Title: DataSourceAspect.java</p>? ?
???? * <p>Description: </p> ?
???? * <p>Copyright: Copyright (c) 2018</p>? ?
???? * @author 林猛
???? * @date 2018年5月3日 ?
???? * @version 1.0? ?
??? */ ?
??? package com.shiyanlou.util;
??? ?
??? import java.lang.reflect.Method;
??? ?
??? import org.aspectj.lang.JoinPoint;
??? import org.aspectj.lang.reflect.MethodSignature;
??? ?
??? /**
???? * @author linmeng
???? *
???? */
??? public class DataSourceAspect{
??? //?? ?攔截目標(biāo)方法,獲取由@DataSource指定的數(shù)據(jù)源標(biāo)識(shí),設(shè)置到線程存儲(chǔ)中以便切換數(shù)據(jù)源
?? ??? ?public void intercept(JoinPoint point) throws Exception{
?? ??? ??? ?Class<?> target = point.getTarget().getClass();
?? ??? ??? ?MethodSignature signature=(MethodSignature)point.getSignature();
??????????? // 默認(rèn)使用目標(biāo)類型的注解,如果沒有則使用其實(shí)現(xiàn)接口的注解
??? ?
?? ??? ??? ?for (Class<?> clazz : target.getInterfaces()) {
?? ??? ??? ??? ?resolveDataSource(clazz, signature.getMethod());
?? ??? ??? ?}
?? ??? ??? ?resolveDataSource(target, signature.getMethod());
?? ??? ?}
?? ??? ?/**
?? ??? ? * 提取目標(biāo)對(duì)象方法注解和類型注解中的數(shù)據(jù)源標(biāo)識(shí)
?? ??? ? */
?? ??? ?
?? ??? ?public void resolveDataSource(Class<?>clazz,Method method) {
?? ??? ??? ?try {
?? ??? ??? ??? ?Class<?>[]types=method.getParameterTypes();
??? //?? ??? ??? ?默認(rèn)使用類型注解
?? ??? ??? ??? ?if (clazz.isAnnotationPresent(DataSource.class)) {
?? ??? ??? ??? ??? ?DataSource source = clazz.getAnnotation(DataSource.class);
?? ??? ??? ??? ??? ?DbContextHolder.setDataSource(source.value());
?? ??? ??? ??? ?}
??? //?? ??? ??? ?方法注解可以覆蓋類型注解
?? ??? ??? ??? ?Method m=clazz.getMethod(method.getName(), types);
?? ??? ??? ??? ?if (m!=null && m.isAnnotationPresent(DataSource.class)) {
?? ??? ??? ??? ??? ?DataSource source = m.getAnnotation(DataSource.class);
?? ??? ??? ??? ??? ?DbContextHolder.setDataSource(source.value());
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?System.out.println(clazz+":"+e.getMessage());
?? ??? ??? ?}
?? ??? ?}
??? }
??? 切換數(shù)據(jù)庫的工具類DbContextHolder.java:
??? /**
???? * <p>Title: DbContextHolder.java</p>? ?
???? * <p>Description: </p> ?
???? * <p>Copyright: Copyright (c) 2018</p>? ?
???? * @author 林猛
???? * @date 2018年5月2日 ?
???? * @version 1.0? ?
??? */ ?
??? package com.shiyanlou.util;
??? ?
??? /**
???? * @author linmeng
???? * 切換數(shù)據(jù)源的工具類
???? */
??? public class DbContextHolder {
?? ??? ?private static final ThreadLocal<String>THREAD_DATA_SOURCE =new ThreadLocal<>();
?? ??? ?/**
?? ??? ? * 設(shè)置當(dāng)前數(shù)據(jù)庫
?? ??? ? */
?? ??? ? public static void setDataSource(String dataSource) {
?? ??? ???????? THREAD_DATA_SOURCE.set(dataSource);
?? ??? ???? }
?? ??? ?/**
?? ??? ? * 取得當(dāng)前數(shù)據(jù)庫
?? ??? ? */
?? ??? ??? public static String getDataSource() {
?? ??? ???????? return THREAD_DATA_SOURCE.get();
?? ??? ???? }
??? ?
?? ??? ?/**
?? ??? ? * 清除上下文數(shù)據(jù)
?? ??? ? */
?? ??? ??? public static void clearDataSource() {
?? ??? ???????? THREAD_DATA_SOURCE.remove();
?? ??? ???? }
??? }
??? 到此為止,數(shù)據(jù)庫配置就完全完成了,使用的時(shí)候非常方便,如果配置的是兩個(gè)數(shù)據(jù)庫的話,有一個(gè)默認(rèn)數(shù)據(jù)庫,不需要任何修改,而需要使用另一個(gè)數(shù)據(jù)庫的時(shí)候,只需要在dao層添加一個(gè)注解:
??? /**
???? * <p>Title: CityDao.java</p>? ?
???? * <p>Description: </p> ?
???? * <p>Copyright: Copyright (c) 2018</p>? ?
???? * @author 林猛
???? * @date 2018年5月4日 ?
???? * @version 1.0? ?
??? */ ?
??? package com.shiyanlou.dao;
??? ?
??? import java.util.List;
??? import java.util.Map;
??? ?
??? import org.apache.ibatis.annotations.Param;
??? ?
??? import com.shiyanlou.domain.CityInfo;
??? import com.shiyanlou.util.DataSource;
??? ?
??? /**
???? * @author linmeng
???? *?? ?
???? */
??? @DataSource(value="dataSource_first")
??? public interface CityDao {
?? ??? ?
?? ??? ?public List<CityInfo>getAdInfo(@Param("city")String city);
?? ??? ?public Integer getAdInfoCount(String city);
?? ??? ?public List<CityInfo>getCityInfo(Map<String, Object>map);
?? ??? ?public List<CityInfo>getPoiData(Map<String, Object>map);
?? ??? ?public Integer getCityInfoCount(Map<String, Object>map);
?? ??? ?public List<CityInfo>singleCityExport(@Param("city")String city);
??? }
這個(gè)注解是跟spring-mybatis.xml中配置的targetDataSources中的entry key? value有關(guān)系。
這里面好多具體配置我也不是特別懂,但是這樣配置是可以使用的。大家可以試一下。
---------------------
作者:不會(huì)code的coder
來源:CSDN
原文:https://blog.csdn.net/qq_36746327/article/details/81033404
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!
轉(zhuǎn)載于:https://www.cnblogs.com/rey888/p/10253794.html
總結(jié)
以上是生活随笔為你收集整理的ssm多数据源的操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WC2007 石头剪刀布 数学+最小费用
- 下一篇: 第二章 向量(d2)有序向量:二分查找