Git Product home page Git Product logo

bjloquent's Introduction

bjLoquent

License: MIT

Better jLoquent

A Java ORM based on jLoquent, inspired by Laravel Eloquent.

License
This library is under the MIT license.

How to use it

bjLoquent is actually not so different of jLoquent. You should check the JUnit test file to see how it works. But here is a quick example.

Firstly, in order to use it you have to create a Configuration class, just like this.

Configuration.java

package com.yourpackage;

import org.bjloquent.DBConfig;
import org.bjloquent.DatabaseType;

public class Configuration implements DBConfig {

    @Override
    public DatabaseType getDatabaseType() {
        return DatabaseType.MYSQL;
    }

    @Override
    public String getHostName() {
        return "databaseHostname";
    }

    @Override
    public String getPortNumber() {
        return "databasePort";
    }

    @Override
    public String getDatabaseName() {
        return "databaseName";
    }

    @Override
    public String getUsername() {
        return "databaseUsername";
    }

    @Override
    public String getPassword() {
        return "yourPassword";
    }
}

Next to that, you should have created some models. I actually don't know how works migrations on jLoquent, and if they works (that is planned for later). Every model has to be named in cingular, bjLoquent will name it in plurial in the database.

To make a model, you have to define a primary key. But if the primary key is an integer called id, you don't have to specify it name. Here is an example:

User.java

package com.yourpackage;

import org.bjloquent.Model;

import java.sql.Timestamp;

public class User extends Model {
    private int id;
    private String name;
    private String email;
    private String password;
    private Timestamp joinedDate;

    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 getEmail() {
        return email;
    }

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

    public String getPassword() {
        return password;
    }

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

    public Timestamp getJoinedDate() {
        return joinedDate;
    }

    public void setJoinedDate(Timestamp joinedDate) {
        this.joinedDate = joinedDate;
    }
}

Conversely, if your primary key is not called id, you will need to specify it just like in the model below.

Player.java (with string UUID)

package com.yourpackage;

import org.bjloquent.Model;

import java.sql.Timestamp;

public class Player extends Model {
    private String uuid;
    private String name;
    private Timestamp joinedDate;
    private int score;

    public Player() {
        // We specify
        super.primaryKey = "uuid";
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getName() {
        return name;
    }

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

    public Timestamp getJoinedDate() {
        return joinedDate;
    }

    public void setJoinedDate(Timestamp joinedDate) {
        this.joinedDate = joinedDate;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

And here is an example of Main:

Main.java

package com.yourpackage;

import org.bjloquent.Connector;
import org.bjloquent.DBConfig;

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Connector connector = Connector.getInstance();
        DBConfig config = new Configuration();
        connector.setDBConfig(config);
        connector.open();

        User user = new User();
        user.setName("John Doe");
        user.setEmail("[email protected]");
        user.setPassword("123456");
        user.setJoinedDate(new java.sql.Timestamp(System.currentTimeMillis()));

        user.create();
        System.out.println("ID: " + user.getId());

        user.setName("Modified John Doe");
        user.save();

        Player player = new Player();
        String uuid = "";
        for (int i = 0; i < 4; i++) {
            uuid += String.valueOf((new Random().nextInt(10)));
        }
        player.setUuid(uuid);
        player.setName("John Doe");
        player.setJoinedDate(new java.sql.Timestamp(System.currentTimeMillis()));
        player.setScore(100);

        System.out.println("UUID: " + player.getUuid());

        player.create();

        player.setScore(200);
        player.setName("Modified John Doe");
        player.save();

        connector.close();
    }
}

bjloquent's People

Contributors

thederickff avatar sofianelasri avatar

Stargazers

 avatar

Forkers

suprasanity

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.