1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        SpringBoot連接兩個(gè)或多個(gè)數(shù)據(jù)庫(kù)(SpringBoot+SqlServer)

        共 5632字,需瀏覽 12分鐘

         ·

        2021-12-02 14:48

        場(chǎng)景

        同事要用SqlServer寫一個(gè)小項(xiàng)目,但是有個(gè)問題就是他們的數(shù)據(jù)需要存到多個(gè)數(shù)據(jù)庫(kù)中,其中兩個(gè)庫(kù)的名字是account和game,不知道怎么處理切換,然后我這邊寫了個(gè)測(cè)試暫時(shí)沒有問題,求大神指教

        SpringBoot配置文件

        #連接sqlserver數(shù)據(jù)庫(kù)
        spring.datasource.account.jdbc-url=jdbc:sqlserver://172.16.10.2:1433;DatabaseName=account
        spring.datasource.account.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
        spring.datasource.account.username=sa
        spring.datasource.account.password=plo#@!


        spring.datasource.game.jdbc-url=jdbc:sqlserver://172.16.10.2:1433;DatabaseName=game
        spring.datasource.game.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
        spring.datasource.game.username=sa
        spring.datasource.game.password=plo#@!

        自定義配置文件

        package org.geemp.game.config;

        import org.apache.ibatis.session.SqlSessionFactory;
        import org.mybatis.spring.SqlSessionFactoryBean;
        import org.mybatis.spring.SqlSessionTemplate;
        import org.mybatis.spring.annotation.MapperScan;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.boot.context.properties.ConfigurationProperties;
        import org.springframework.boot.jdbc.DataSourceBuilder;
        import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.Configuration;
        import org.springframework.context.annotation.Primary;
        import org.springframework.jdbc.datasource.DataSourceTransactionManager;

        import javax.sql.DataSource;

        /**
        * @author JackRen
        * @date 2021-12-01 14:14
        * @description:
        */


        @Configuration
        @MapperScan(basePackages= {"org.geemp.game.account"},sqlSessionFactoryRef="gameSqlSessionFactory")
        public class DataSourceAccountConfig {

        @Bean(name="accountDataSource")
        @ConfigurationProperties(prefix="spring.datasource.account")
        public DataSource accountDataSource() {
        return DataSourceBuilder.create().build();
        }

        @Bean(name="accountSqlSessionFactory")
        @Primary
        public SqlSessionFactory accountSqlSessionFactory(@Qualifier("accountDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
        }
        @Bean(name="accountTransactionManager")//配置事務(wù)
        @Primary
        public DataSourceTransactionManager accountTransactionManager(@Qualifier("accountDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
        }
        @Bean(name="accountSqlSessionTemplate")
        @Primary
        public SqlSessionTemplate accountSqlSessionTemplate(@Qualifier("accountSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
        }

        }
        package org.geemp.game.config;

        import org.apache.ibatis.session.SqlSessionFactory;
        import org.mybatis.spring.SqlSessionFactoryBean;
        import org.mybatis.spring.SqlSessionTemplate;
        import org.mybatis.spring.annotation.MapperScan;
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.boot.context.properties.ConfigurationProperties;
        import org.springframework.boot.jdbc.DataSourceBuilder;
        import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.Configuration;
        import org.springframework.context.annotation.Primary;
        import org.springframework.jdbc.datasource.DataSourceTransactionManager;

        import javax.sql.DataSource;

        /**
        * @author JackRen
        * @date 2021-12-01 14:15
        * @description:
        */


        @Configuration
        @MapperScan(basePackages= {"org.geemp.game.gm"},sqlSessionFactoryRef="gameSqlSessionFactory")
        public class DataSourceGameConfig {

        @Bean(name="gameDataSource")
        @ConfigurationProperties(prefix="spring.datasource.game")
        public DataSource gameDataSource() {
        return DataSourceBuilder.create().build();
        }

        @Bean(name="gameSqlSessionFactory")
        public SqlSessionFactory gameSqlSessionFactory(@Qualifier("gameDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
        }
        @Bean(name="gameTransactionManager")//配置事務(wù)
        public DataSourceTransactionManager gameTransactionManager(@Qualifier("gameDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
        }
        @Bean(name="gameSqlSessionTemplate")
        public SqlSessionTemplate gameSqlSessionTemplate(@Qualifier("gameSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
        }

        }

        在配置類掃描的包下進(jìn)行數(shù)據(jù)交互


        我們以game為例:

        mapper:

        package org.geemp.game.gm.mapper;

        import org.apache.ibatis.annotations.Param;
        import org.apache.ibatis.annotations.Select;
        import org.springframework.stereotype.Component;

        import java.util.List;
        import java.util.Map;

        /**
        * @author JackRen
        * @date 2021-12-01 14:43
        * @description:
        */

        public interface GameMapper {

        @Select("select CT_INGOT,CT_GOLD,CT_POINT from chartable where CT_USERID = #{CT_USERID} and CT_NAME=#{CT_NAME}")
        List> obtainDataBy(@Param("CT_USERID") String CT_USERID,@Param("CT_NAME") String CT_NAME);
        }

        service:

        package org.geemp.game.gm.service;

        import org.geemp.game.gm.mapper.GameMapper;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;

        import java.util.List;
        import java.util.Map;

        /**
        * @author JackRen
        * @date 2021-04-27 13:31
        * @description:
        */

        @Service
        public class GameDBServiceImpl {

        @Autowired
        private GameMapper gameMapper;

        public List> obtainDataBy(String CT_USERID, String CT_NAME){
        List> dataList= gameMapper.obtainDataBy(CT_USERID,CT_NAME);
        return dataList;
        }

        }

        同樣的,account和game是一樣的寫法,篇幅限制不再粘貼,到這里就完成了,可以啟動(dòng)測(cè)試!


        瀏覽 192
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            粗大的内捧猛烈进出爽女视频 | 无码人妻精品一区二区三区99仓 | 欧美重囗味成人无码区 | 大胸丰满护士hd | 在线a人片免费观看视频 | 美女扒开粉嫩尿囗给男生桶 | 国产精品视频啪啪 | 欧美成熟熟女在线观看视频 | 狂操小嫩逼| 大香焦久久 |