同源数据库的多源支持
日常项目中因为使用的分布式开发模式,不同的服务有不同的数据源,常常需要在一个项目中使用多个数据源,因此需要配置sping data
jpa对多数据源的使用,一般分一下为三步:
1 配置多数据源
2 不同源的实体类放入不同包路径
3 声明不同的包路径下使用不同的数据源、事务支持
异构数据库多源支持
比如我们的项目中,即需要对mysql的支持,也需要对mongodb的查询等。
实体类声明@Entity关系型数据库支持类型、声明@Document为mongodb支持类型,不同的数据源使用不同的实体就可以了
interface PersonRepository extends Repository<Person,Long>{…}
@Entity
public class Person {…}
interface UserRepository extends Repository<User,Long>{…}
@Document
public class User{…}
但是,如果User用户既使用mysql也使用mongodb呢,也可以做混合使用
interface JpaPersonRepository extends Repository<Person,Long>{…}
interface MongoDBPersonRepository extends Repository<Person,Long>{…}
@Entity
@Document
public class Person{…}
也可以通过对不同的包路径进行声明,比如A包路径下使用mysql,B包路径下使用mongoDB
@EnableJpaRepositories(basePackages="com.neo.repositories.jpa")
@EnableMongoRepositories(basePackages="com.neo.repositories.mongo")
interface Configuration{}