日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ssm多数据源的操作

發(fā)布時間:2025/4/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ssm多数据源的操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

公司要求,需要使用兩個數(shù)據(jù)庫,一個mysql,一個oracle。所以需要配置兩個數(shù)據(jù)庫來進行操作。

1.首先,需要在jdbc.properties文件中將兩個庫的配置數(shù)據(jù)寫入,不過一個寫driver,另一個寫driver2,區(qū)別兩個庫的變量名。

代碼如下:

#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中的配置:

??? 先開啟注解模式
??? 兩個數(shù)據(jù)庫分別配置不同id的DataSource
??? 就是配置切換數(shù)據(jù)庫的自定義類的路徑,選中默認數(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">
?? ??? ?<!-- 默認使用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{
??? //?? ?攔截目標方法,獲取由@DataSource指定的數(shù)據(jù)源標識,設(shè)置到線程存儲中以便切換數(shù)據(jù)源
?? ??? ?public void intercept(JoinPoint point) throws Exception{
?? ??? ??? ?Class<?> target = point.getTarget().getClass();
?? ??? ??? ?MethodSignature signature=(MethodSignature)point.getSignature();
??????????? // 默認使用目標類型的注解,如果沒有則使用其實現(xiàn)接口的注解
??? ?
?? ??? ??? ?for (Class<?> clazz : target.getInterfaces()) {
?? ??? ??? ??? ?resolveDataSource(clazz, signature.getMethod());
?? ??? ??? ?}
?? ??? ??? ?resolveDataSource(target, signature.getMethod());
?? ??? ?}
?? ??? ?/**
?? ??? ? * 提取目標對象方法注解和類型注解中的數(shù)據(jù)源標識
?? ??? ? */
?? ??? ?
?? ??? ?public void resolveDataSource(Class<?>clazz,Method method) {
?? ??? ??? ?try {
?? ??? ??? ??? ?Class<?>[]types=method.getParameterTypes();
??? //?? ??? ??? ?默認使用類型注解
?? ??? ??? ??? ?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è)置當前數(shù)據(jù)庫
?? ??? ? */
?? ??? ? public static void setDataSource(String dataSource) {
?? ??? ???????? THREAD_DATA_SOURCE.set(dataSource);
?? ??? ???? }
?? ??? ?/**
?? ??? ? * 取得當前數(shù)據(jù)庫
?? ??? ? */
?? ??? ??? public static String getDataSource() {
?? ??? ???????? return THREAD_DATA_SOURCE.get();
?? ??? ???? }
??? ?
?? ??? ?/**
?? ??? ? * 清除上下文數(shù)據(jù)
?? ??? ? */
?? ??? ??? public static void clearDataSource() {
?? ??? ???????? THREAD_DATA_SOURCE.remove();
?? ??? ???? }
??? }

??? 到此為止,數(shù)據(jù)庫配置就完全完成了,使用的時候非常方便,如果配置的是兩個數(shù)據(jù)庫的話,有一個默認數(shù)據(jù)庫,不需要任何修改,而需要使用另一個數(shù)據(jù)庫的時候,只需要在dao層添加一個注解:

??? /**
???? * <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);
??? }

這個注解是跟spring-mybatis.xml中配置的targetDataSources中的entry key? value有關(guān)系。

這里面好多具體配置我也不是特別懂,但是這樣配置是可以使用的。大家可以試一下。
---------------------
作者:不會code的coder
來源:CSDN
原文:https://blog.csdn.net/qq_36746327/article/details/81033404
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!

轉(zhuǎn)載于:https://www.cnblogs.com/rey888/p/10253794.html

總結(jié)

以上是生活随笔為你收集整理的ssm多数据源的操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 男男车车的车车网站w98免费 | 粗喘呻吟撞击猛烈疯狂 | 久操久操 | 国产香蕉在线 | 97精品免费视频 | 在线观看免费大片 | 成人精品在线视频 | 男女做爰猛烈吃奶啪啪喷水网站 | 中文字幕在线视频网 | 人与动物2免费观看完整版电影高清 | 中文字幕在线播放 | 欧美日韩一区二区三区国产精品成人 | 夫妻性生活黄色大片 | 日韩免费av一区二区 | 国产三极片 | 欧洲精品久久久久毛片完整版 | 国产精品一区二区三区免费在线观看 | 国产黄a三级三级看三级 | 丰满大爆乳波霸奶 | 亚洲成av人片一区二区 | 极品新婚夜少妇真紧 | 风间由美一区二区三区 | 成人激情视频在线观看 | 爱情岛论坛自拍 | 黄色免费在线观看网站 | 精品人妻一区二区三区换脸明星 | 免费av一级片 | av色在线 | 国产麻豆精品在线 | 午夜视频黄色 | 手机在线亚洲 | 国产香蕉在线视频 | 国产三级在线免费观看 | 超碰人人艹| 亚洲精品www久久久久久广东 | 成为性瘾网黄的yy对象后 | 2021av| 女性生殖扒开酷刑vk | 2019年中文字幕 | 蜜臀尤物一区二区三区直播 | 日韩在线视频你懂的 | 白嫩少妇激情无码 | 亚洲精品在线不卡 | 三级国产三级在线 | 国产va| 欧美日韩高清在线 | 国产做爰xxxⅹ高潮视频12p | 一级二级在线观看 | 少女国产免费观看 | 欧美国产大片 | 中文久久久 | 天天插天天透 | 午夜吃瓜 | 亚洲国产精品久久久久久久 | 骚虎视频在线观看 | 欧美整片在线观看 | 日韩一区二区三区在线看 | 91尤物视频 | 91视频久久 | 久久精视频 | 六月婷婷综合 | 一级免费黄色片 | 日韩免费高清一区二区 | av免费福利| 亚洲av人无码激艳猛片服务器 | 男男play呻吟动漫网站 | 国产精品久久久久久久久久久不卡 | 艳妇臀荡乳欲伦交换h漫 | 男女做爰猛烈吃奶啪啪喷水网站 | av一级网站| 日韩一区二区三区免费在线观看 | 亚洲黄色三级 | 一区二区网 | 欧洲女性下面有没有毛发 | 91亚洲国产成人久久精品麻豆 | 免费的黄色一级片 | 黄色网址在线播放 | 看全色黄大色黄大片女一次牛 | 亚洲图片 欧美 | 西西大胆午夜视频 | 少妇一级淫免费放 | 少妇无套高潮一二三区 | 七七久久| 国产男女猛烈无遮挡免费视频 | 欧美国产乱视频 | 一本色道久久综合无码人妻 | 国产亚洲精久久久久久无码苍井空 | 日韩福利网站 | xnxx国产| 色偷偷888欧美精品久久久 | 国产精品成熟老女人 | 午夜久久视频 | 香港a毛片 | 日本高清不卡码 | 成人免费视频软件网站 | www.久色| 亚洲手机视频 | 91精品国产综合久久精品 | av电影在线播放 |