您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Springboot怎么集成mybatis實現多數據源配置”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Springboot怎么集成mybatis實現多數據源配置”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
新建springboot工程,引入web、mysql、mybatis依賴
<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>2.2.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
在application.properties中配置多數據源
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/ds0
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/ds1
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
多數據源配置與單個數據源配置不同點在于,spring.datasource之后多了一個數據源名稱primary/secondary用來區分不同的數據源;
初始化數據源
新建一個配置類,用來加載多個數據源完成初始化。
@Configuration public class DataSourceConfiguration { @Primary @Bean @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix = "spring.datasource.secondary") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } }
通過@ConfigurationProperties就可以知道這兩個數據源分別加載了spring.datasource.primary.*和spring.datasource.secondary.*的配置。@Primary注解指定了主數據源,當不指定數據源時,就會使用該主數據源。
mybatis配置
@Configuration @MapperScan( basePackages = "com*.primary", sqlSessionFactoryRef = "sqlSessionFactoryPrimary", sqlSessionTemplateRef = "sqlSessionTemplatePrimary") public class PrimaryConfig { private DataSource primaryDataSource; public PrimaryConfig(@Qualifier("primaryDataSource") DataSource primaryDataSource) { this.primaryDataSource = primaryDataSource; } @Bean public SqlSessionFactory sqlSessionFactoryPrimary() throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(primaryDataSource); return bean.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplatePrimary() throws Exception { return new SqlSessionTemplate(sqlSessionFactoryPrimary()); } }
@Configuration @MapperScan( basePackages = "com.*.secondary", sqlSessionFactoryRef = "sqlSessionFactorySecondary", sqlSessionTemplateRef = "sqlSessionTemplateSecondary") public class SecondaryConfig { private DataSource secondaryDataSource; public SecondaryConfig(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) { this.secondaryDataSource = secondaryDataSource; } @Bean public SqlSessionFactory sqlSessionFactorySecondary() throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(secondaryDataSource); return bean.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplateSecondary() throws Exception { return new SqlSessionTemplate(sqlSessionFactorySecondary()); } }
配置類上使用@MapperScan注解來指定當前數據源下定義的實體和mapper的包路徑,還注入了sqlSessionFactory和sqlSessionTemplate,通過@Qualifier注解指定了對應的數據源,其名字對應在DataSourceConfiguration配置類中的數據源定義的函數名。
各個對數據源對應路徑下的實體和mapper
@Data @NoArgsConstructor public class UserPrimary { private Long id; private String user_name; private Integer age; public UserPrimary(String name, Integer age) { this.user_name = name; this.age = age; } } public interface UserMapperPrimary { @Select("SELECT * FROM USER_0 WHERE USER_NAME = #{name}") UserPrimary findByName(@Param("name") String name); @Insert("INSERT INTO USER_0 (USER_NAME, AGE) VALUES(#{name}, #{age})") int insert(@Param("name") String name, @Param("age") Integer age); @Delete("DELETE FROM USER_0") int deleteAll(); }
@Data @NoArgsConstructor public class UserSecondary { private Long id; private String user_name; private Integer age; public UserSecondary(String name, Integer age) { this.user_name = name; this.age = age; } } public interface UserMapperSecondary { @Select("SELECT * FROM USER_1 WHERE USER_NAME = #{name}") UserSecondary findByName(@Param("name") String name); @Insert("INSERT INTO USER_1 (USER_NAME, AGE) VALUES(#{name}, #{age})") int insert(@Param("name") String name, @Param("age") Integer age); @Delete("DELETE FROM USER_1") int deleteAll(); }
測試
@Test public void test() { // 往Primary數據源插入一條數據 userMapperPrimary.insert("caocao", 20); // 從Primary數據源查詢剛才插入的數據,配置正確就可以查詢到 UserPrimary userPrimary = userMapperPrimary.findByName("caocao"); Assert.assertEquals(20, userPrimary.getAge().intValue()); // 從Secondary數據源查詢剛才插入的數據,配置正確應該是查詢不到的 UserSecondary userSecondary = userMapperSecondary.findByName("caocao"); Assert.assertNull(userSecondary); // 往Secondary數據源插入一條數據 userMapperSecondary.insert("sunquan", 21); // 從Primary數據源查詢剛才插入的數據,配置正確應該是查詢不到的 userPrimary = userMapperPrimary.findByName("sunquan"); Assert.assertNull(userPrimary); // 從Secondary數據源查詢剛才插入的數據,配置正確就可以查詢到 userSecondary = userMapperSecondary.findByName("sunquan"); Assert.assertEquals(21, userSecondary.getAge().intValue()); }
讀到這里,這篇“Springboot怎么集成mybatis實現多數據源配置”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。