繁体   English   中英

如何在Play Framework中设置2个MySQL数据库?

[英]How do I set up 2 MySQL databases in Play Framework?

我需要设置2个单独的数据库。 一个用于Test类,另一个用于TestTwo类,但是我不知道如何配置application.conf文件。

application.conf(第1部分):

db.default.driver=com.mysql.jdbc.Driver 
db.default.url="jdbc:mysql://localhost/dbone?characterEncoding=UTF-8" 

db.dbtwo.driver=com.mysql.jdbc.Driver 
db.dbtwo.url="jdbc:mysql://localhost/dbtwo?characterEncoding=UTF-8" 


尝试1失败:两个类都保存到数据库1(dbone)中:
application.conf(第2部分):

ebean.default="*"
ebean.dbtwo="models.TestTwo"


尝试2失败:尝试保存内容时出现错误:

[PersistenceException: The type [class models.TestTwo] is not a registered entity? If you don't explicitly list the entity classes to use Ebean will search for them in the classpath. If the entity is in a Jar check the ebean.search.jars property in ebean.properties file or check ServerConfig.addJar().]

application.conf(第2部分):

ebean.default="models.Test"
ebean.dbtwo="models.TestTwo"


我该如何设置以便将Test对象保存到dbone并将TestTwo对象保存到dbtwo?


编辑:TestTwo类的要求(没什么特别的;我没有手动分配一个Ebean服务器,除了在application.conf文件中):

package models;
import javax.persistence.*;
import play.db.ebean.*;
import play.data.validation.Constraints.Required;

@Entity
public class TestTwo extends Model{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;

    @Required
    public String testString;

    public static Model.Finder<Long, TestTwo> find = new Model.Finder<Long, TestTwo>(Long.class, TestTwo.class);

    public static TestTwo create (String testString){
        TestTwo test = new TestTwo();
        test.testString = testString;
        test.save();
        return test;
    }
}

我遇到了同样的问题,这个问题是我在任何地方都可以找到的唯一相关主题。 解决该问题的方法不是很漂亮,但是我使属于非默认ebean服务器的所有模型类都覆盖了save() -, delete() -和update()方法。 这些覆盖分别调用super.save(dbname)super.delete(dbname)super.update(dbname) ,其中dbname是ebean服务器的名称。

启动时,我将名称从配置中拉出并保存,以免被硬编码。 尽管它看起来很多余,但对我来说却解决了这个问题。 我希望它可以帮助像我一样在几年后迷失于这个问题的其他人!

您是否编写了演化脚本? 如果不是,则需要在conf>evolutions>default>1.sql编写一个

创建表“ testtwo”:

create table testtwo(
    id          bigint auto_increment not null,
    testString      varchar(100) not null,
    constraint t1_testTwo primary key(id)
);

进一步:

SET FOREIGN_KEY_CHECKS=0;

drop table if exists testto;

SET FOREIGN_KEY_CHECKS=1;

刷新浏览器时,播放将要求“应用进化”,这将在数据库中创建“ testtwo”表,您可以在其中保存实体。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM