Y
Y
Yourmind2019-10-14 13:20:32
PostgreSQL
Yourmind, 2019-10-14 13:20:32

How can I use 2 databases in my spring boot application?

application:
spring.datasource.userandadvertisement.url=jdbc:postgresql://localhost:
5432 /
mydate
.Driver
spring.datasource.usersecret.url= jdbc:postgresql://localhost:5432/for_user_secure
spring.datasource.usersecret.username=test_user
spring.datasource.usersecret.password=qwerty
spring.datasource.usersecret.driverClassName=org.postgresql .Driver
//client class
@Entity
@Table(name = "Users")
public class User {
@Id
// @Column(name="login")
@GeneratedValue(strategy= GenerationType.AUTO)
private UUID iduser;
@Column(unique=true)
private String login;
private String phone;
//link to photo
private String photo;
public User(){}
...
//password class
@Entity
@Table(name = "UsersSecure")
public class UserSecure {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private UUID idusersecure;
@Column(unique=true)
private String login;
@NotNull
private String password;
private String used token;
public UserSecure(){}
...
and their repositories:
@Repository
@Qualifier("jdbcUserandadvertisement")
public interface UserRepository extends CrudRepository {
//get all clients
List findAll();
...}
public interface UserSecureRepository extends CrudRepository {
UserSecure save(UserSecure userSecure);
...}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Osipov, 2019-10-15
@Shiftuia

I didn’t implement such a thing with JPA, but I’ll assume that you need to configure two EntityManagers with two different dataSources through the configuration class and connect your own EntityManager to each repository

R
rageorg, 2020-01-21
@rageorg

Here I found how to make several databases How can I set up "multi-user" using Spring hikariCP?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question