Play+Ebean操作数据库快速上手

配置:



application.conf:
db.default.driver=com.mysql.jdbc.Driver
db.default.url="mysql://root:[email protected]:3306/bigdata?characterEncoding=UTF-8"
//db.default.username="root"
//db.default.password="root"

ebean.default=["models.*"]

build.sbt:

lazy val `maxi` = (project in file(".")).enablePlugins(PlayJava, PlayEbean/*, PlayEnhancer*/)
"mysql" % "mysql-connector-java" % "5.1.18"

plugins.sbt:

addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0")

之前加入PlayEnhancer和
addSbtPlugin(“com.typesafe.sbt” % “sbt-play-enhancer” % “1.1.0”)
之后报错
Bean class models.* is not enhanced?
屏蔽之后即可。

使用

创建

models目录下(没有则创建一个)创建一个model

package models;

import com.avaje.ebean.Model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Created by maxi on 2016/12/28.
 */
@Entity
@Table(name="user")
public class UsersModel extends Model{
    @Id
    public int id;

    public String name;
    public String password;
    public String email;

    public static Finder find = new Finder(
            Long.class, UsersModel.class
    );

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

查询

    Ebean.execute(() -> {
        UsersModel user = Ebean.find(UsersModel.class,1);
        play.Logger.debug("staff login email: {}, pwd: {}", user.email, user.password);
    });

你可能感兴趣的:(Play+Ebean操作数据库快速上手)