You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java版Play Framework新手求Ebean连接MySQL建模有效教程

Hey there, I’ve been in your exact spot—fumbling with outdated official docs when you’re just getting started with Play Framework, Ebean, and MySQL is super frustrating. Let me share a reliable, up-to-date workflow that’ll get you connected and modeling data without those annoying errors.

Step 1: Get Your Dependencies Right (Play 2.8+)

First, make sure your build.sbt has the correct dependencies. Outdated docs often miss new Ebean module or MySQL driver versions. Here’s what you need:

// Play Ebean module
libraryDependencies += guice
libraryDependencies += "io.ebean" % "ebean" % "12.1.10"
libraryDependencies += "io.ebean" % "play-ebean" % "7.0.1"

// MySQL JDBC Driver
libraryDependencies += "mysql" % "mysql-connector-java" % "8.0.33"

// Ebean SBT plugin for bytecode enhancement (critical!)
addSbtPlugin("io.ebean" % "sbt-ebean" % "12.1.10")

Note: Bytecode enhancement is non-negotiable for Ebean—it handles ORM magic like lazy loading. Skip this, and you’ll hit weird runtime errors.

Step 2: Configure Database & Ebean in application.conf

Older docs use deprecated config keys, so use these updated settings:

# MySQL Database Connection
db.default.driver=com.mysql.cj.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/your_db_name?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true"
db.default.username=your_db_user
db.default.password=your_db_password

# Ebean Configuration
ebean.default = ["models.*"]
play.ebean.ddl.generate=true
play.ebean.ddl.run=true
  • Replace your_db_name, your_db_user, and your_db_password with your actual MySQL details.
  • The serverTimezone=UTC flag fixes common time-related connection errors with MySQL 8+.
Step 3: Create Your First Ebean Model

Let’s make a simple User model to test things out. Create app/models/User.java:

package models;

import io.ebean.Model;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class User extends Model {
    @Id
    public Long id;
    public String email;
    public String name;

    // Ebean finder for easy queries
    public static final Finder<Long, User> find = new Finder<>(User.class);
}

Pro tip: If you hate writing getters/setters, add Lombok to your dependencies and use @Data to auto-generate them.

Step 4: Test the Connection & Model

Start your Play app with sbt run. If everything’s set up right:

  1. Play will automatically create the user table in your MySQL database (thanks to play.ebean.ddl.run=true).
  2. You can test queries in a controller, like:
public Result getUsers() {
    List<User> users = User.find.all();
    return ok(Json.toJson(users));
}
Troubleshooting Common Errors
  • ClassNotFoundException for MySQL Driver: Double-check your mysql-connector-java dependency in build.sbt—make sure the version is compatible with your MySQL server (MySQL 8+ needs driver 8.x).
  • Ebean Entity Not Found: Ensure your model package is included in ebean.default (e.g., models.*) and that the SBT Ebean plugin is properly added for bytecode enhancement.
  • Connection Timeout: Verify your MySQL server is running, and the db.default.url has the correct host/port. The allowPublicKeyRetrieval=true flag fixes access issues for some MySQL setups.

This workflow works for Play 2.8 and newer versions—way more reliable than the outdated official docs you might have found. If you hit a specific error, drop the stack trace and I can help you dig deeper!

内容的提问来源于stack exchange,提问作者Wale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:48:25