1. Mybatis配置多數(shù)據(jù)源

        共 5694字,需瀏覽 12分鐘

         ·

        2021-08-19 13:27

        在項目中經(jīng)常會存在操作不同數(shù)據(jù)庫得情況,最為典型就是后管系統(tǒng),如果不走接口進行調(diào)用,那么勢必會有多個數(shù)據(jù)源作為數(shù)據(jù)管理的得突破口。在此我們可以想一下到底是微服務接口暴露模式的后管好還是基于多數(shù)據(jù)源的模式好?雖然都是管理數(shù)據(jù),而且效果都差不多。但是接口模式的管理對開發(fā)不太友好。所以這塊我們稍微學習一下mybatis的多數(shù)據(jù)配置,我們想要的結(jié)果是項目兼容任意多個數(shù)據(jù)庫,這里我們用mysql做為研究對象。至于其他的數(shù)據(jù)庫也是一樣的。
        在之前我們學習mybatis的時候說mybatis是java的orm框架,然后這個orm框架要和spring進行整合,需要mybatis-spring這樣一個橋的東西。當然在springboot中提供了基于自動配置的mybatis,這塊我們學的是原生的.
        首先我們導入相關(guān)的maven依賴
        <!--        數(shù)據(jù)庫-->        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>8.0.26</version>        </dependency>
        <!-- orm--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency>
        <!-- orm spring--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency>
        <!-- 數(shù)據(jù)源--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.9</version> </dependency>
        數(shù)據(jù)源這塊采用druid,這塊為了實踐,我們采用MapperFactoryBean和MapperScannerConfigurer兩種方式進行探討。代碼如下:
        @Configurationpublic class MybatisConfig {
        public DruidDataSource getDruidDataSource(String url,String userName,String password) throws SQLException { DruidDataSource ds = new DruidDataSource(); ds.setUrl(url); ds.setUsername(userName); ds.setPassword(password); try { ds.setFilters("stat,mergeStat,slf4j"); } catch (Exception var18) { } ds.setMaxActive(50); ds.setInitialSize(1); ds.setMinIdle(1); ds.setMaxWait(60000); ds.setTimeBetweenEvictionRunsMillis(120000); ds.setMinEvictableIdleTimeMillis(300000); ds.setValidationQuery("SELECT 'x'"); ds.setPoolPreparedStatements(true); ds.setMaxPoolPreparedStatementPerConnectionSize(30); ds.setTestWhileIdle(true); ds.setTestOnReturn(false); ds.setTestOnBorrow(false); ds.init(); return ds; }
        @Bean(name = "dataO") public SqlSessionFactoryBean getSqlSessionFactoryOne1() throws Exception { //xml和實體的映射 SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(getDruidDataSource("jdbc:mysql://127.0.0.1:3306/tianjl?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true","root","tianjingle")); sqlSessionFactoryBean.setTypeAliasesPackage("com.example.demo.one"); Resource[] resources = new Resource[]{new ClassPathResource("tian/one/OneMapper.xml")}; sqlSessionFactoryBean.setMapperLocations(resources); return sqlSessionFactoryBean; }
        @Bean(name = "dataTwo") public MapperFactoryBean getSqlSessionFactoryTwo() throws Exception { //xml和實體的映射 SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(getDruidDataSource("jdbc:mysql://127.0.0.1:3306/tianjl?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true","root","tianjingle")); sqlSessionFactoryBean.setTypeAliasesPackage("com.example.demo.two"); sqlSessionFactoryBean.setMapperLocations(new ClassPathResource("tian/two/TwoMapper.xml")); //單個數(shù)據(jù)源所有的數(shù)據(jù)庫映射 MapperFactoryBean mapperFactoryBean=new MapperFactoryBean(); //設置sqlSessionTemplate,zhuru yong de mapperFactoryBean.setMapperInterface(TwoMapper.class); mapperFactoryBean.setSqlSessionFactory(sqlSessionFactoryBean.getObject()); return mapperFactoryBean; }
        @Bean @ConditionalOnBean(name = "dataO") public static MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer mapperScannerConfigurer=new MapperScannerConfigurer(); mapperScannerConfigurer.setBasePackage("com.example.demo.one.mapper");//這塊指定掃描的接口 mapperScannerConfigurer.setSqlSessionFactoryBeanName("dataO"); return mapperScannerConfigurer; }}
        在完成上述方法之后,我們編寫測試方法
            @Resource    private OneMapper oneMapper;

        @Resource private TwoMapper twoMapper;

        @GetMapping(value = "/one") public BaikeResponse one(){ BaikeResponse baikeResponse=new BaikeResponse(); baikeResponse.setData(oneMapper.one()); return baikeResponse; }
        @GetMapping(value = "/two") public BaikeResponse two(){ BaikeResponse baikeResponse=new BaikeResponse(); baikeResponse.setData(twoMapper.two()); return baikeResponse;}
        小結(jié):這塊我們采用比較原始的方式配置了了兩種多數(shù)據(jù)源的實現(xiàn),這塊我們也可以對此進行擴展,封裝成讓習慣了使用starter的同學摸不著頭腦的形式。好了,本次demo就演示到這里了。
        晚安~




        瀏覽 51
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
          
          

            1. 香港三级日本三级韩国三级 | 玖玖视频在线观看免费 | 无码不卡视频 | 国产午夜亚洲精品午夜鲁丝片 | 99这里有精品视频 |