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.
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.
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, andyour_db_passwordwith your actual MySQL details. - The
serverTimezone=UTCflag fixes common time-related connection errors with MySQL 8+.
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.
Start your Play app with sbt run. If everything’s set up right:
- Play will automatically create the
usertable in your MySQL database (thanks toplay.ebean.ddl.run=true). - You can test queries in a controller, like:
public Result getUsers() { List<User> users = User.find.all(); return ok(Json.toJson(users)); }
- ClassNotFoundException for MySQL Driver: Double-check your
mysql-connector-javadependency inbuild.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.urlhas the correct host/port. TheallowPublicKeyRetrieval=trueflag 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

