我已经用两个MySQL数据库配置了一个Micronaut应用程序,但它总是使用默认数据源执行操作。
如何使用多个数据库?
这是我的application.yml配置:
datasources:
default:
url: jdbc:mysql://localhost:3306/micronaut_demo
username: root
password: goti@181994
dialect: MYSQL
target:
url: jdbc:mysql://localhost:3306/micronaut_demo_target
username: root
password: goti@181994
dialect: MYSQL
jpa:
default:
packages-to-scan:
- 'com.example'
properties:
hibernate:
hbm2ddl:
auto: create-drop
show_sql: true
target:
properties:
hibernate:
hbm2ddl:
auto: create-drop
show_sql: true
这是我的Repository
声明:
@Singleton
@Repository("target")
public class DepartmentRepositoryImpl implements DepartmentRepository {
private EntityManager entityManager;
public DepartmentRepositoryImpl(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
@ReadOnly
public Optional<Department> findById(Long id) {
return Optional.ofNullable(entityManager.find(Department.class, id));
}
@Override
@Transactional
public Department save(String name) {
//save logic
}
}
如您所见,我指定了要使用的数据源:
@Repository("target")