springboot初学---配置多数据源

开发步骤:

1 配置多数据源

2 不同源的实体类放入不同包路径

3 声明不同的包路径下使用不同的数据源、事务支持

异构数据源支持:

1.mysql和MongoDB

实体类声明@Entity 关系型数据库支持类型、声明@Document 为mongodb支持类型,不同的数据源使用不同的实体就可以了

<1>.每个实体类只对应一种数据源

实体类:

@Entity
public class Person {
  …
}


@Document
public class User {
  …
}

jpa操作数据库

interface PersonRepository extends Repository<Person, Long> {
 …
}



interface UserRepository extends Repository<User, Long> {
 …
}

<2>.某个实体类即需要使用mysql有需要使用mongodb

实体类:

@Entity
@Document
public class Person {
  …
}

jpa操作数据库:

interface JpaPersonRepository extends Repository<Person, Long> {
 …
}

interface MongoDBPersonRepository extends Repository<Person, Long> {
 …
}

也可以通过配置不同的包路径实现不同数据库的操作

@EnableJpaRepositories(basePackages = "com.neo.repositories.jpa")
@EnableMongoRepositories(basePackages = "com.neo.repositories.mongo")
interface Configuration { }
posted @ 2019-01-24 10:14  MengJH  阅读(151)  评论(0编辑  收藏  举报