@Service
public class MyService {
private final Entity1Repository entity1Repository;
private final Entity2Repository entity2Repository;
@Autowired
public MyService(Entity1Repository entity1Repository, Entity2Repository entity2Repository) {
this.entity1Repository = entity1Repository;
this.entity2Repository = entity2Repository;
}
public void doSomething() {
// Use entity1Repository and entity2Repository to interact with databases
}
}
To configure two different databases in Spring Boot using HikariDataSource, you can use the following approach:
- Define separate properties for each database in your
application.properties
orapplication.yml
file. - Configure multiple DataSource beans, each pointing to a different database.
- Optionally, create JdbcTemplate beans for easier database interaction.
Here's how you can