springboot+mybatis配置多数据源,springboot集成mybatisplus多数据源

  springboot+mybatis配置多数据源,springboot集成mybatisplus多数据源

  新建跳羚工程,引入web、mysql、mybatis依赖

  依赖groupIdorg.springframework.boot/groupId artifact id spring-boot-starter-web/artifact id/依赖关系依赖groupIdorg.mybatis.spring.boot/groupId artifactId mybatis-spring-boot-starter/artifactId版本2。2 .2/版本/依赖关系依赖groupId MySQL/groupId artifact id MySQL-connector-Java/artifact id作用域运行时/作用域/依赖性依赖groupIdorg.projectlombok/groupId artifact id lombok/artifact id选项真/可选/依赖依赖groupIdorg.springframework.boot/groupId artifactId spring-boot-starter-test/artifactId scope test/scope/dependency依赖groupId JUnit/group在应用程序.属性中配置多数据源

  春天。数据来源。初级。JDBC-URL=JDBC : MySQL ://localhost :3306/DS0 spring。数据来源。初级。用户名=rootspring。数据来源。初级。password=rootspring。数据来源。初级。driver-class-name=comMySQL。CJ。JDBC。驾驶员

  春天。数据来源。次要的。JDBC-URL=JDBC : MySQL ://localhost :3306/DS1 spring。数据来源。次要的。用户名=rootspring。数据来源。次要的。password=rootspring。数据来源。次要的。driver-class-name=comMySQL。CJ。JDBC。驾驶员

  多数据源配置与单个数据源配置不同点在于,spring.datasource之后多了一个数据源名称小学/中学用来区分不同的数据源;

  初始化数据源

  新建一个配置类,用来加载多个数据源完成初始化。

  @ configuration公共类数据源配置{ @ Primary @ Bean @ configuration属性(前缀=spring.datasource.primary )公共数据源

   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@NoArgsConstructorpublic 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@NoArgsConstructorpublic 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实现多数据源配置详解流程的文章就介绍到这了,更多相关Springboot多数据源配置内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

 

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: