<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                首先,我們新建一個數據庫`guns_test`,并分別在`guns`數據庫和`guns_test`數據庫中分別新增同樣結構的兩個表`test`,sql文件如下,也可以在`src/test/sql`下找到這個sql文件 ``` DROP DATABASE IF EXISTS guns_test; CREATE DATABASE IF NOT EXISTS guns_test DEFAULT CHARSET utf8 COLLATE utf8_general_ci; use guns_test; SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for test -- ---------------------------- DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `aaa` int(11) NOT NULL AUTO_INCREMENT, `bbb` varchar(255) DEFAULT NULL, PRIMARY KEY (`aaa`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; SET FOREIGN_KEY_CHECKS = 1; ``` > 1.對表進行代碼生成,方便測試兩個數據源 ![image_1c4mosmth1fp4dv81ai6nbll8u1g.png-92.7kB](https://img-blog.csdnimg.cn/20181228174637358) > 2.打開application.yml中的多數據源開關 ![image_1ci8k9e4l1kmtjmsh4a12rb10q39.png-87.8kB](https://img-blog.csdnimg.cn/20181228174637413) > 3.配置application.yml中的多數據源的連接信息 ![image_1ci8kbvun1m7b1t0g1g7r1k7u1f58m.png-92.2kB](https://img-blog.csdnimg.cn/20181228174637460) **另外注意,如果想開啟多數據源,需要關閉kernel-core中mybatis-plus中的自動配置!!重要!!如下!!** ![image_1cpuhger9l1722v1afo8ab1vns1g.png-81.1kB](https://img-blog.csdnimg.cn/20181228174637499) > 4.編寫測試多數據源的代碼,注意觀察`@DataSource注解`,這些代碼都可以在`cn.stylefeng.guns.multi`包中找到 ``` package cn.stylefeng.guns.multi.service; /** * <p> * 服務類 * </p> * * @author fengshuonan * @since 2018-07-10 */ public interface TestService { /** * 測試多數據源的業務 * * @author stylefeng * @Date 2017/6/23 23:02 */ void testBiz(); /** * 測試多數據源的業務 * * @author stylefeng * @Date 2017/6/23 23:02 */ void testGuns(); } ``` ``` package cn.stylefeng.guns.multi.service.impl; import cn.stylefeng.guns.core.common.constant.DatasourceEnum; import cn.stylefeng.guns.multi.entity.Test; import cn.stylefeng.guns.multi.mapper.TestMapper; import cn.stylefeng.guns.multi.service.TestService; import cn.stylefeng.roses.core.mutidatasource.annotion.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * <p> * 服務實現類 * </p> * * @author fengshuonan * @since 2018-07-10 */ @Service public class TestServiceImpl implements TestService { @Autowired private TestMapper testMapper; @Override @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ) @Transactional public void testBiz() { Test test = new Test(); test.setBbb("bizTest"); testMapper.insert(test); } @Override @DataSource(name = DatasourceEnum.DATA_SOURCE_GUNS)` @Transactional public void testGuns() { Test test = new Test(); test.setBbb("gunsTest"); testMapper.insert(test); } } ``` ``` package cn.stylefeng.guns.multi.test; import cn.stylefeng.guns.base.BaseJunit; import cn.stylefeng.guns.multi.service.TestService; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; /** * 業務測試 * * @author fengshuonan * @date 2017-06-23 23:12 */ public class BizTest extends BaseJunit { @Autowired TestService testService; @Test public void test() { testService.testGuns(); testService.testBiz(); } } ``` > 5.執行`BizTest`這個測試類,可以看出,兩條數據同時插入了不同的數據庫中的兩張表中 ![image_1ci8kfj001dr91gg01l721g781l5f13.png-18.7kB](https://img-blog.csdnimg.cn/20181228174637539) ![image_1ci8kg1fo18671fafp4q1k5e186u1g.png-26kB](https://img-blog.csdnimg.cn/20181228174637578) 多數據源的原理就是一個項目同時配置了兩個`DataSource`,并把這兩個`DataSource`放到`DynamicDataSource`綁定,使用AOP進行動態切換當前操作的數據源。 **若想深入了解多數據源的配置和原理可參考`MybatisPlusConfig類`和`MultiSourceExAop類`,也可參考視頻教程第`7節 多數據源配置和使用`,內有詳細的講解**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看