好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用SpringBoot配置多数据源的经验分享

1. 引入jar包

pom.xml文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

<?xml version= "1.0" encoding= "UTF-8" ?>

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"

          xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" >

     <modelVersion> 4.0 . 0 </modelVersion>

     <parent>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-parent</artifactId>

         <version> 2.5 . 6 </version>

         <relativePath/> <!-- lookup parent from repository -->

     </parent>

     <groupId>com.multi.datasource</groupId>

     <artifactId>demo</artifactId>

     <version> 0.0 . 1 -SNAPSHOT</version>

     <name>demo</name>

     <description>Demo project for Spring Boot</description>

     <properties>

         <java.version> 1.8 </java.version>

     </properties>

     <dependencies>

 

         <dependency>

             <groupId>org.springframework.boot</groupId>

             <artifactId>spring-boot-starter</artifactId>

         </dependency>

 

         <dependency>

             <groupId>org.springframework.boot</groupId>

             <artifactId>spring-boot-starter-web</artifactId>

         </dependency>

 

         <dependency>

             <groupId>org.mybatis.spring.boot</groupId>

             <artifactId>mybatis-spring-boot-starter</artifactId>

             <version> 1.3 . 2 </version>

         </dependency>

 

         <dependency>

             <groupId>com.alibaba</groupId>

             <artifactId>druid</artifactId>

             <version> 1.1 . 8 </version>

         </dependency>

 

         <dependency>

             <groupId>mysql</groupId>

             <artifactId>mysql-connector-java</artifactId>

         </dependency>

 

         <dependency>

             <groupId>org.projectlombok</groupId>

             <artifactId>lombok</artifactId>

             <version> 1.16 . 22 </version>

         </dependency>

 

         <dependency>

             <groupId>org.springframework.boot</groupId>

             <artifactId>spring-boot-starter-test</artifactId>

             <scope>test</scope>

         </dependency>

 

     </dependencies>

 

     <build>

         <plugins>

             <plugin>

                 <groupId>org.springframework.boot</groupId>

                 <artifactId>spring-boot-maven-plugin</artifactId>

                 <configuration>

                     <excludes>

                         <exclude>

                             <groupId>org.projectlombok</groupId>

                             <artifactId>lombok</artifactId>

                         </exclude>

                     </excludes>

                 </configuration>

             </plugin>

         </plugins>

     </build>

 

</project>

2. properties配置

分别准备两个数据源

?

1

2

3

4

5

6

7

8

9

10

11

12

server.port= 18888

mybatis.mapper-locations=classpath:mapper/*.xml

 

my1.datasource.url=jdbc:mysql: //10.0.0.125:3306/wyl?autoReconnect=true

my1.datasource.driverClassName=com.mysql.cj.jdbc.Driver

my1.datasource.username=root

my1.datasource.password= 123456

 

my2.datasource.url=jdbc:mysql: //10.0.0.160:3306/wyl?autoReconnect=true

my2.datasource.driverClassName=com.mysql.cj.jdbc.Driver

my2.datasource.username=root

my2.datasource.password= 123456

3. 分别配置两个数据源

第一个数据源

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

package com.multi.datasource.config;

 

import com.alibaba.druid.pool.DruidDataSource;

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

 

import javax.sql.DataSource;

 

@Configuration

@MapperScan (basePackages = My1DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "my1SqlSessionFactory" )

public class My1DataSourceConfig {

 

     static final String PACKAGE = "com.multi.datasource.dao.my1" ;

     static final String MAPPER_LOCATION = "classpath:mapper/*.xml" ;

 

     @Value ( "${my1.datasource.url}" )

     private String url;

 

     @Value ( "${my1.datasource.username}" )

     private String user;

 

     @Value ( "${my1.datasource.password}" )

     private String password;

 

     @Value ( "${my1.datasource.driverClassName}" )

     private String driverClass;

 

     @Bean (name = "my1DataSource" )

     public DataSource my1DataSource() {

         DruidDataSource dataSource = new DruidDataSource();

         dataSource.setDriverClassName(driverClass);

         dataSource.setUrl(url);

         dataSource.setUsername(user);

         dataSource.setPassword(password);

         dataSource.setMaxWait(Integer.MAX_VALUE);

         dataSource.setTestOnBorrow( true );

         dataSource.setTestOnReturn( true );

         dataSource.setTestWhileIdle( true );

         return dataSource;

     }

 

     @Bean (name = "my1TransactionManager" )

     public DataSourceTransactionManager my1TransactionManager() {

         return new DataSourceTransactionManager(my1DataSource());

     }

 

     @Bean (name = "my1SqlSessionFactory" )

     public SqlSessionFactory my1SqlSessionFactory( @Qualifier ( "my1DataSource" ) DataSource my1DataSource)

             throws Exception {

         final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

         sessionFactory.setDataSource(my1DataSource);

         sessionFactory.setMapperLocations(

                 new PathMatchingResourcePatternResolver().getResources(My1DataSourceConfig.MAPPER_LOCATION));

         return sessionFactory.getObject();

     }

}

第二个数据源

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

package com.multi.datasource.config;

 

import com.alibaba.druid.pool.DruidDataSource;

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

 

import javax.sql.DataSource;

 

@Configuration

@MapperScan (basePackages = My1DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "my1SqlSessionFactory" )

public class My1DataSourceConfig {

 

     static final String PACKAGE = "com.multi.datasource.dao.my1" ;

     static final String MAPPER_LOCATION = "classpath:mapper/*.xml" ;

 

     @Value ( "${my1.datasource.url}" )

     private String url;

 

     @Value ( "${my1.datasource.username}" )

     private String user;

 

     @Value ( "${my1.datasource.password}" )

     private String password;

 

     @Value ( "${my1.datasource.driverClassName}" )

     private String driverClass;

 

     @Bean (name = "my1DataSource" )

     public DataSource my1DataSource() {

         DruidDataSource dataSource = new DruidDataSource();

         dataSource.setDriverClassName(driverClass);

         dataSource.setUrl(url);

         dataSource.setUsername(user);

         dataSource.setPassword(password);

         dataSource.setMaxWait(Integer.MAX_VALUE);

         dataSource.setTestOnBorrow( true );

         dataSource.setTestOnReturn( true );

         dataSource.setTestWhileIdle( true );

         return dataSource;

     }

 

     @Bean (name = "my1TransactionManager" )

     public DataSourceTransactionManager my1TransactionManager() {

         return new DataSourceTransactionManager(my1DataSource());

     }

 

     @Bean (name = "my1SqlSessionFactory" )

     public SqlSessionFactory my1SqlSessionFactory( @Qualifier ( "my1DataSource" ) DataSource my1DataSource)

             throws Exception {

         final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

         sessionFactory.setDataSource(my1DataSource);

         sessionFactory.setMapperLocations(

                 new PathMatchingResourcePatternResolver().getResources(My1DataSourceConfig.MAPPER_LOCATION));

         return sessionFactory.getObject();

     }

}

4. Dao目录

为了区分两个数据源,分别设置了不同的目录

?

1

2

3

4

5

6

7

8

9

10

11

package com.multi.datasource.dao.my1;

 

import com.multi.datasource.entity.UserEntity;

import org.apache.ibatis.annotations.Mapper;

 

@Mapper

public interface Test1Mapper {

    

     UserEntity query();

    

}

?

1

2

3

4

5

6

7

8

9

10

11

package com.multi.datasource.dao.my2;

 

import com.multi.datasource.entity.UserEntity;

import org.apache.ibatis.annotations.Mapper;

 

@Mapper

public interface Test2Mapper {

    

     UserEntity query();

    

}

5. Entity

?

1

2

3

4

5

6

7

8

9

10

package com.multi.datasource.entity;

 

import lombok.Data;

 

@Data

public class UserEntity {

 

     private String userName;

 

}

6. Mapper文件

从my1数据源查询

?

1

2

3

4

5

6

7

8

9

<? xml version = "1.0" encoding = "UTF-8" ?>

         <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

< mapper namespace = "com.multi.datasource.dao.my1.Test1Mapper" >

 

< select id = "query" resultType = "com.multi.datasource.entity.UserEntity" >

         select user_name as userName from t_user

     </ select >

 

</ mapper >

从my2数据源查询

?

1

2

3

4

5

6

7

8

9

<? xml version = "1.0" encoding = "UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

< mapper namespace = "com.multi.datasource.dao.my2.Test2Mapper" >

    

     < select id = "query" resultType = "com.multi.datasource.entity.UserEntity" >

         select user_name as userName from t_user

     </ select >

    

</ mapper >

7. Controller测试

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

package com.multi.datasource.controller;

 

import com.multi.datasource.dao.my1.Test1Mapper;

import com.multi.datasource.dao.my2.Test2Mapper;

import com.multi.datasource.entity.UserEntity;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

 

import javax.annotation.Resource;

 

@RestController

public class TestController {

 

     @Resource

     private Test1Mapper test1Mapper;

 

     @Resource

     private Test2Mapper test2Mapper;

 

     @RequestMapping ( "query" )

     public void query() {

         UserEntity user1 = test1Mapper.query();

         System.out.println( "my1 dataSource:" + user1);

 

 

         UserEntity user2 = test2Mapper.query();

         System.out.println( "my2 dataSource:" + user2);

     }

 

}

两个数据源,对应的 user_name 分别是 zhangsan和lisi

8. 结果验证

访问 http://localhost:18888/query ,结果如下

到此这篇关于使用SpringBoot配置多数据源的经验分享的文章就介绍到这了,更多相关SpringBoot配置多数据源内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/CSDN_WYL2016/article/details/124086758

查看更多关于使用SpringBoot配置多数据源的经验分享的详细内容...

  阅读:12次