<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
pom.xml檔案
<?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>
分別準備兩個資料來源
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
第一個資料來源
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(); } }
第二個資料來源
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(); } }
為了區分兩個資料來源,分別設定了不同的目錄
package com.multi.datasource.dao.my1; import com.multi.datasource.entity.UserEntity; import org.apache.ibatis.annotations.Mapper; @Mapper public interface Test1Mapper { UserEntity query(); }
package com.multi.datasource.dao.my2; import com.multi.datasource.entity.UserEntity; import org.apache.ibatis.annotations.Mapper; @Mapper public interface Test2Mapper { UserEntity query(); }
package com.multi.datasource.entity; import lombok.Data; @Data public class UserEntity { private String userName; }
從my1資料來源查詢
<?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資料來源查詢
<?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>
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
存取 http://localhost:18888/query
,結果如下
到此這篇關於使用SpringBoot設定多資料來源的經驗分享的文章就介紹到這了,更多相關SpringBoot設定多資料來源內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45