A simple wrapper around the ebean.io library to provide a one-stop-shop of shading all required dependencies and auto downloading the used driver.
Drop the ebean-wrapper.jar
inside your plugins directory and depend on it, if you are using this wrapper in a Minecraft (Spigot/Paper) server.
As an alternative you can shade the lib into your application or provide it in the classpath. This is not recommended for Minecraft plugins!
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation group: "net.silthus", name: "ebean-wrapper", version: "2.10.0"
}
<project>
...
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
...
<dependencies>
<dependency>
<groupId>net.silthus</groupId>
<artifactId>ebean-wrapper</artifactId>
<version>2.10.0</version>
</dependency>
</dependencies>
...
</project>
You can then create a new instance of the EbeanWrapper
by providing a Config
object. Use the config builder Config.builder()
to create a new instance of the config.
Make sure you provide a valid driver or create your own DriverMapping
.
EbeanWrapper wrapper = new EbeanWrapper(Config.builder()
.driver(Driver.MariaDB)
.username("root")
.password("root")
.url("jdbc:mysql://localhost:3306/foobar")
.build());
You will also need to add all classes that should be treated as entities to the entities method of the config builder.
Config.builder()
.entities(
User.class,
Address.class
).driver(Driver.H2)
...
.build();
The ebean-wrapper will download the required driver by default. You can disable this behaviour my passing .autoDownloadDriver(false)
to the ConfigBuilder
.
If you do that you can control on your own when to download the driver by calling wrapper.downloadDriver();
.
Then you can open the database connection or directly get the Database
object, which will also open the connection.
The connection will be cached and all subsequent calls to getDatabase()
use the same connection.
wrapper.connect();
// or directly get the database which will connect automatically
wrapper.getDatabase();
The ebean wrapper provides the following default driver mappings for auto downloading the appropriate driver.
Driver | Documentation | ConnectionString |
---|---|---|
h2 |
h2database.com | jdbc:h2:~/test |
mysql |
mysql.com | jdbc:mysql://host:port/database |
postgres |
jdbc.postgresql.org | jdbc:postgresql://host:port/database |
mariadb |
mariadb.com | jdbc:mysql://host:port/database |
sqlserver |
docs.microsoft.com | jdbc:sqlserver://localhost:1433;databaseName=AdventureWorks;integratedSecurity=true; |
sqlite |
github.com/xerial/sqlite-jdbc | jdbc:sqlite:/home/example/mydatabase.db |
ebean has great migration support for all of the different database types.
You can use this awesome feature by creating the following Migrations
class inside your test source root.
import io.ebean.annotation.Platform;
import io.ebean.dbmigration.DbMigration;
import java.io.IOException;
public class Migrations {
public static void main(String[] args) throws IOException {
DbMigration migration = DbMigration.create();
// location of the migration changeSet and where ddl is generated to
migration.setPathToResources("src/main/resources");
// add a series of database platforms to generate the ddl for ...
migration.addPlatform(Platform.POSTGRES, "postgres");
migration.addPlatform(Platform.MYSQL, "mysql");
migration.addPlatform(Platform.MARIADB, "mariadb");
migration.addPlatform(Platform.H2, "h2");
migration.addPlatform(Platform.SQLITE, "sqlite");
migration.generateMigration();
}
}
For this to work you will also need to test depend on all of the different driver implementations. Add the following test dependencies for it to work.
testImplementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.10.0'
testImplementation group: 'com.h2database', name: 'h2', version: '2.10.000'
testImplementation group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.10.0'
testImplementation group: 'mysql', name: 'mysql-connector-java', version: '2.10.02'
testImplementation group: 'org.xerial', name: 'sqlite-jdbc', version: '3.32.3.2'
testImplementation group: 'org.postgresql', name: 'postgresql', version: '42.2.18'
Also make sure you enable migrations when creating your EbeanWrapper
by setting migrations(this.getClass())
to the class of your application.
Then ebean will automatically run the migrations of all of your entities on start.