【问题标题】:Spring Boot and multiple databasesSpring Boot 和多个数据库
【发布时间】:2015-11-21 08:55:15
【问题描述】:

我已经设置了一个带有单个数据库连接的基本 spring 项目。

在 application.properties 文件中我有数据库设置:

spring.datasource.url = jdbc:mysql://192.168.1.19/ticket
spring.datasource.username = dbusername
spring.datasource.password = dbpassword

我创建了一个其他 DAO 扩展的基础 DAO 类:

@Transactional
public class Dao<E> {

    @PersistenceContext
    private EntityManager entityManager;

    private Class<E> entityClass;

    public Dao(Class<E> entityClass) {
        this.entityClass = entityClass;
    }

    public void create(E object) {
        entityManager.persist(object);

        return;
    }

    public void delete(E object) {
        if (entityManager.contains(object)) {
            entityManager.remove(object);
        } else {
            entityManager.remove(entityManager.merge(object));
        }

        return;
    }

    @SuppressWarnings("unchecked")
    public List<E> getAll() {
        return entityManager.createQuery("from " + entityClass.getName()).getResultList();
    }

    public E get(long id) {
        return entityManager.find(entityClass, id);
    }

    public void update(E object) {
        entityManager.merge(object);
        return;
    }
}

这是一个扩展基本 DAO 的示例实体:

@Repository
public class PersonDao extends Dao<Person> {

    public PersonDao() {
        super(Person.class);
    }
}

目前这使用单个数据库,但我需要能够添加第二个数据库,并以某种方式在每个 DAO 中定义要使用的数据源。每个 DAO 将只使用一个数据库,因此 DAO 不需要能够连接到多个数据库。

我做了一些研究,这似乎表明我需要使用JdbcTemplate?但我似乎找不到符合我需要的教程。此外,在实体管理器被注入 DAO 的那一刻,但我看过的 JdbcTemplate 示例似乎没有使用实体管理器,这有点令人困惑。

【问题讨论】:

  • 既然你是从 Spring 开始的,那就看看 Spring Data repositories;他们基本上会为你生成整个 DAO 类,并自动构建最简单的查询。
  • 谢谢建议,我去看看

标签: java spring spring-boot


【解决方案1】:
database.password1=<password1>
database.url1=jdbc\:mysql\://localhost\:3306/twodbone
database.username1=<username1>
database.password2=<password1>
database.url2=jdbc\:mysql\://localhost\:3306/twodbtwo
database.username2=<username2>
database.driverClassName=com.mysql.jdbc.Driver

通过这种方式,您可以添加多个数据库并同时配置 hibernate.cfg.xml 文件和 applicationContext.xml 文件..

【讨论】:

  • 我的项目没有 hibernate.cfg.xml 或 applicationcontext.xml 文件,因为我使用的是 springboot,我相信它可以为我处理大部分内容。
  • 当存储库扩展 CrudRepository 并使用 findOne()、save() 方法时,它将如何工作? CrudRepository 如何知道使用 jdbcTemplate 而不是 entityManager?我认为这是我感到困惑的地方,因为示例链接没有提到使用 CrudRepository,但似乎在数据库上进行了自己的查询。
【解决方案2】:
@Repository
public class FooRepository
{
    @PersistenceContext
    private EntityManager entityManager;

    @Autowired(required = true)
    private JdbcTemplate jdbcTemplate;

    public void saveFoo(Foo foo)
    {
         this.entityManager.persist(foo);
    }

    public List<SomeReportPojo> getSomeReport()
    {
         return this.entityManager.queryForList("SELECT .. ",SomeProjectPojo.class); 
    }
}

应该保留 this.jdbcTemplate 而不是 jdbc 模板的 this.entityManager

这是一个简单的例子

【讨论】:

  • 对不起,我仍然不确定我是否理解。您是说继续使用 EntityManager 进行当前数据库连接,并使用 JdbcTemplate 进行第二次连接? spring/hibernate ORM 将如何与 JDBC 模板搭配使用?
猜你喜欢
  • 2015-09-07
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多