简体   繁体   English

玩! 框架 - 将MySQL和MongoDB用于相同的应用程序

[英]Play! Framework - Using MySQL and MongoDB for same application

Is it possible to user MySQL Database and MongoDb database for same project using Play! 是否可以使用Play为同一个项目使用MySQL DatabaseMongoDb数据库! framework? 框架?

for example: I want 例如:我想要

@Entity Person to interact with my MySQL database and  
@Entity PersonData to interact with my MongoDB database?  

How can I do that? 我怎样才能做到这一点?

Please let me know 请告诉我
Thank you 谢谢

Yes, it is possible. 对的,这是可能的。 Just use the Morphia plugin for Play. 只需使用Morphia插件进行播放即可。 I have done it before. 我以前做过。 It is quite simple. 这很简单。

For the MongoDB models, just do something like this: 对于MongoDB模型,只需执行以下操作:

import play.modules.morphia.Model;

@Entity
public class YourMongoModel extends Model {
   ...
}

For the MySQL model, do this: 对于MySQL模型,请执行以下操作:

import play.db.jpa.Model;

@Entity
public class LogMessageX extends Model {
  ...
}

Notice the different imports. 注意不同的进口。

Then the application.conf file should contains something like this: 然后application.conf文件应该包含这样的内容:

# For MongoDB
morphia.db.host=localhost
morphia.db.port=27017
morphia.db.name=YourMongoDBName

# for MySQL
db=mysql:user:pwd@database_name

On the MySQL entity extend Model and add the JPA annotation (@Entity). 在MySQL实体上扩展Model并添加JPA注释(@Entity)。

For Mongo you need to use a third-party module such as this one: http://www.playframework.org/modules/mongo-1.3/home 对于Mongo,您需要使用第三方模块,例如: http//www.playframework.org/modules/mongo-1.3/home

Example: 例:

@MongoEntity("collectionName") @MongoEntity( “集合名”)

public class Car extends MongoModel { 公共类Car扩展MongoModel {

public String name;
public String colour;
public int topSpeed;

} }

Play's JPA plugin will not modify the Mongo class since it won't have the JPA @Entity annotation. Play的JPA插件不会修改Mongo类,因为它没有JPA @Entity注释。

For anyone out there interested, checkout Play's JPAEnhancer. 对于有兴趣的人,请查看Play的JPAE增强器。 It uses javaassist to modify the bytecode and add all the method impls - very cool! 它使用javaassist来修改字节码并添加所有方法impls - 非常酷!

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

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