MyBatisの別個の読み取りおよび書き込み、複数のデータソース(注実装)

####(注釈が達成)の分離を読み書きするMyBatisの複数のデータソースが------最初のテストのための2つのライブラリを作成する必要があり、私はここで使用master_testとslave_test 2つのライブラリである、2つのライブラリは、同様のを持っていますテーブル(怠惰、こんにちはこんにちは)、テーブル名t_userのテーブル構造|フィールド名|タイプ|注| |:------:|:------:|:------:| | ID | int型|主キーインクリメントID | |名前| VARCHAR |名前|![ファイル](https://img2018.cnblogs.com/blog/1602984/201909/1602984-20190922132558249-1393771686.jpg)2つのテーブルを追加し、メインのデータベースレコード名のテストを容易にするために、データの異なる部分は、 `最初のPOMファイルを` `導入、Springboot xiaoliu統合MyBatisの使用を開始するために、ライブラリから、xiaobinです 4.0.0 org.springframework.boot 春・ブート・スタータ親 2.1.4.RELEASE com.xiaobin mysql_master_slave 1.0-SNAPSHOT 1.8 1.18.6 1.3.2 1.18.6 org.springframework.boot 春・ブート・スターター・ウェブ org.projectlombok 唐辛子 $ {} lombok.version org.springframework.boot 春・ブート・スターターテスト org.projectlombok 唐辛子 $ {} lombox.version org.mybatis.spring.boot MyBatisのスプリング・ブートスターター $ {} mybatis.version mysqlの MySQLのコネクタ-javaの org.springframework.boot 春・ブート・スターター-JDBC com.alibaba ドルイドスプリング・ブートスターター 1.1.10 org.springframework.boot 春・ブート・スタータAOP `データソース@DataSourceカスタム注釈の切り替えを達成するために、本明細書ドルイドデータソース使用動的データソースの構成がapplication.yml AOPのprofile``スプリングを切り替えるように配置####:データソース:タイプ:com.alibaba.druid.pool.DruidDataSourceのドルイド:xiaobinマスター:#プライマリデータソースdriverClassName:はcom.mysql.jdbc.Driverユーザー名:rootパスワード:ルートURLます。jdbc:mysqlの:// localhostを:3306 / master_test serverTimezone? = GMTの%2B8&useUnicode =真&characterEncoding = UTF8のxiaobinスレーブ:#データソースdriverClassNameから:はcom.mysql.jdbc.Driverユーザー名:rootパスワード:ルートURLます。jdbc:mysqlの:// localhostの:? 3306 / slave_test serverTimezone = GMT%2B8&useUnicode = TRUE&characterEncoding = UTF8 MyBatisの:マッパー-場所:クラスパス:マッパー/ * XML `####マルチデータソースの構成class``パッケージcom.xiaobin.config;インポートcom.alibaba.druid.spring.boot .. autoconfigure.DruidDataSourceBuilder;輸入org.springframework.boot.context.properties.ConfigurationProperties;輸入org.springframework。context.annotation.Bean;輸入org.springframework.context.annotation.Configuration;輸入org.springframework.context.annotation.Primary;輸入org.springframework.stereotype.Component;インポートするjavax.sql.DataSource;輸入java.util.HashMapを;インポートjava.util.Map; / ** *作成:2019年9月22日11時42 *注:農業自己の*マルチデータソースの構成コード情報交換小グループ:260 532 022、参加を歓迎は、共有学習ありますアミューズメントのメンバー** / @Configuration @ComponentパブリッククラスDynamicDataSourceConfig {@Bean @ConfigurationProperties( "spring.datasource.druid.xiaobinマスター")公衆データソースxiaobinMasterDataSource(){DruidDataSourceBuilder.createを返す()ビルド();.} @豆@ConfigurationProperties( "spring.datasource.druid.xiaobinスレーブ")公衆データソースxiaobinSlaveDataSource(){DruidDataSourceBuilder.createを返す()ビルド();.} @Bean @Primary公共DynamicDataSourceデータソース(データソースxiaobinMasterDataSource、データソースxiaobinSlaveDataSource){地図 targetDataSources =新規HashMapの<>(); targetDataSources.put( "xiaobinマスター"、xiaobinMasterDataSource); targetDataSources.put( "xiaobinスレーブ"、xiaobinSlaveDataSource);(xiaobinMasterDataSource、targetDataSources)新しいDynamicDataSourceを返す;}} `` `#動的データ・ソース###クラススイッチ ``パッケージcom.xiaobin.config;インポートorg.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;インポートorg.springframework.lang.Nullable;インポートするjavax.sql.DataSource;インポートするJava。 util.Map; / ** *作成:2019年9月22日11:51 *注:*動的データソースコードの自習の農業交流小グループ:260 532 022は、学習が幸せなことで共有するために、参加することを歓迎します** /パブリッククラスDynamicDataSourceはAbstractRoutingDataSource {プライベート静的最終的にThreadLocalを拡張します contextHolder =新しいのThreadLocal <>(); 公共DynamicDataSource(データソースdefaultTargetDataSource、地図 targetDataSources){super.setDefaultTargetDataSource(defaultTargetDataSource); super.setTargetDataSources(targetDataSources); super.afterPropertiesSet();};パブリック静的ボイドsetDataSource(文字列データソース){contextHolder.set保護オブジェクトdetermineCurrentLookupKey(){()をgetDataSourceを返すを} @Override (データソース);}公共の静的な文字列をgetDataSource(){)(contextHolder.getを返す;}パブリック静的ボイドclearDataSource(){contextHolder.remove();}} `注釈を切り替える必要があり、` `#### @DataSourceカスタムダオこのコメント `` `パッケージcom.xiaobin.annotationにデータを追加、インポートjava.lang.annotation *; / ** *作成:2019年9月22日11:53 *注:カスタムデータソースの選択コメント*自習交換コードの農業小グループ:260 532 022、学習が幸せなこと** / @Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documented公共@interfaceデータソース{文字列名()デフォルトで共有するために、参加することを歓迎"";} `` #### AOPのアスペクトクラスconfiguration` `パッケージcom.xiaobin.aspect。輸入com.xiaobin.annotation.DataSource;輸入com.xiaobin.config.DynamicDataSource;輸入org.aspectj.lang.ProceedingJoinPoint;輸入org.aspectj.lang.annotation.Around;輸入org.aspectj.lang.annotation.Aspect;インポートorg.aspectj.lang.annotation.Pointcut;輸入org.aspectj.lang.reflect.MethodSignature;輸入org.springframework.stereotype.Component;輸入java.lang.reflect.Methodオブジェクト; / ** *作成:9分の2019 / 22 11時54分*注:*自習交換の農業小グループの数:260 532 022、学習が幸せなこと** / @Aspect @ComponentパブリッククラスDataSourceAspect {@Pointcut(「@注釈(com.xiaobinで共有するために、参加することを歓迎.annotation.DataSource) ")公共ボイドdataSourcePointCut(){} @Around(" dataSourcePointCut()「)パブリックオブジェクトの周囲(ProceedingJoinPoint点)のThrowable {MethodSignature署名を=(MethodSignature)point.getSignature()スロー;メソッドメソッド=署名を。 getMethod()メソッド。データソースデータソース= method.getAnnotation(DataSource.class);他(DataSourceが== NULL){DynamicDataSource.setDataSource( "xiaobinマスター")}もし{DynamicDataSource.setDataSource(dataSource.nameは());} {リターンポイントを試します.proceed();}最後に{DynamicDataSource.clearDataSource();}}} `` ####注釈ブート構成の重要な情報(あるいはランを与えられている) `` `パッケージcom.xiaobin;インポートcom.xiaobin.config .DynamicDataSourceConfig;インポートorg.mybatis.spring.annotation.MapperScan;輸入org.springframework.boot.SpringApplication;インポートorg.springframework.boot.autoconfigure.SpringBootApplication;インポートorg.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;インポートORG。 springframework.context.annotation.Import; / ** *作成:2019年9月22日11:17 *注:*自習交換の農業小グループの数:260 532 022、学習が幸せなことで共有するために、参加することを歓迎します** / @SpringBootApplication(= {DataSourceAutoConfigurationを除外する。クラス})@MapperScan(basePackages = "com.xiaobin.mapper")@import({DynamicDataSourceConfig.class})パブリッククラスstartappの{パブリック静的無効メイン(文字列[] args){SpringApplication.run(StartApp.class);} } `####試験controller``パッケージcom.xiaobin.api;インポートcom.xiaobin.Entity.TUser;インポートcom.xiaobin.mapper.UserMapper、インポートorg.springframework.beans.factory.annotation.Autowired ;輸入org.springframework.web.bind.annotation.GetMapping;輸入org.springframework.web.bind.annotation.PathVariable;輸入org.springframework.web.bind.annotation.RequestMapping;輸入org.springframework.web.bind.annotation .RestController;輸入はjava.util.List; / ** *作成:2019年9月22日12:08 *注:*自習交換の農業小グループの数:260 532 022、学習を共有するために、参加することを歓迎するが、幸せなことです** / @RestController @RequestMappingパブリッククラスUserControllerで{@AutowiredプライベートUserMapper userMapper。@GetMapping( "/ {名前} /リスト")公衆リスト リスト(@PathVariable( "名前")文字列名){(name.equals( "マスター"))なら、{(userMapper.queryAllWithMasterを返す);}そうでなければ{(userMapper.queryAllWithSlaveを返す);}}} `` ##図##エフェクト経路より伝統的な値は、プライマリ・データ・ソースから切り替えるため![ファイル](https://img2018.cnblogs.com/blog/1602984/201909/1602984-20190922132558471-1360902101.jpg)![ファイル]( https://img2018.cnblogs.com/blog/1602984/201909/1602984-20190922132558693-1674030148.jpg)####ディレクトリ構造![ファイル](https://img2018.cnblogs.com/blog/1602984/201909農業自習交換の/1602984-20190922132558939-255963524.jpg)送信元アドレス(データベースが独自に作成する必要があります)https://gitee.com/MyXiaoXiaoBin/learning-to-share/tree/master/mysql_master_slave ######コード小グループ:260 532 022、学習を共有するために、参加することを歓迎は幸せなことです

おすすめ

転載: www.cnblogs.com/hy-xiaobin/p/11567144.html