License | Master | Develop | Version |
|
|
|
|
MiniDAO is a java library created to form a quick connection between java model and database.
Its purpose is to simplify requests, but it is not made for creating a database automatically. I will maybe add a feature to execute scripts on initialization, or to expose a maven job, a bit like Artisan and Eloquent in PHP.
If you want to participate, I'm fully open to it. If you want a new feature incorporated or encounter a bug, don't hesitate to fill an issue.
- Quick Start
- Installation
- Annotations
- Configuration
- Main class : MiniDAO
- Query Builders
- Development
- License
Add those lines in your pom.xml:
<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>com.thomaskint</groupId>
<artifactId>minidao</artifactId>
<version>{version}</version>
</dependency>
</dependencies>
Start by creating an MDConnectionConfig object with login informations to your database:
MDConnectionConfig connectionConfig = new MDConnectionConfig(MDDriver.MYSQL, "{url}", "{port}", "{username}", "{password}", "{database}");
For example:
MDConnectionConfig connectionConfig = new MDConnectionConfig(MDDriver.MYSQL, "127.0.0.1", "3306", "minidao", "password", "minidao");
Then, set the library config:
MiniDAO miniDAO = new MiniDAO(connectionConfig);
Create an object reflecting one of your database table:
@MDEntity(tableName = "user")
public class User {
@MDId
@MDField(fieldName = "id", allowedSQLActions = SELECT)
public BigDecimal id;
@MDField(fieldName = "email")
public String email;
@MDField(fieldName = "login")
public String login;
@MDField(fieldName = "password")
public String password;
}
Now, you can retrieve data from your database:
public List<User> getUsers() {
List<User> users = null;
try {
users = miniDAO.read().getEntities(User.class);
} catch (MDException e) {
e.printStackTrace();
}
return users;
}
Clone project:
git clone https://gitlab.com/tkint/MiniDAO.git
If ojdbc8 (12.2.0.1) is not already installed in your local maven repository will need to create an oracle account. Then you can either download and install ojdbc8 or install ojdbc8 during maven build
Now, install MiniDAO in your local maven repository:
mvn clean install
First of all, you should create an oracle account
Go to oracle website, accept OTN License Agreement and download ojdbc8.jar.
Now, go to your download directory, open a bash and run the following command:
mvn install:install-file -Dfile=ojdbc8.jar -DgroupId=com.oracle -DartifactId=ojdbc8 -Dversion=12.2.0.1 -Dpackaging=jar
It will install ojdbc8 on your local maven repository, making it available to you and MiniDAO.
First of all, you should create an oracle account
Next, you have to accept the license agreement
In settings.xml, there are fields for credentials:
<username>{oracle_username}</username>
<password>{oracle_password}</password>
Change {oracle_username} and {oracle_password} respectively by your oracle username and password.
Now, next time you want to install MiniDAO in your local maven repository, you will have to run the following command:
mvn clean install -s settings.xml
It will automatically install MiniDAO and ojdbc8 on your local maven repository based on the oracle credentials you have given.
Define class as an object of database
- constraints:
- class must be public
- must have an empty constructor if custom ones are defined
- properties:
- tableName: table name in database
- allowedSQLActions: authorized verbs on this entity
- default: { SELECT, INSERT, UPDATE, DELETE }
- examples:
@MDEntity(tableName = "user")
public class User {
}
@MDEntity(tableName = "user_token", allowedSQLActions = { SELECT, INSERT })
public class UserToken {
}
Define field as a column of the entity table
- constraints:
- field must be public
- properties:
- fieldName: field name in database
- allowedSQLActions: authorized verbs on this field
- default: { SELECT, INSERT, UPDATE, DELETE }
- examples:
@MDEntity(tableName = "user")
public class User {
@MDField(fieldName = "id", allowedSQLActions = SELECT)
public BigDecimal id;
@MDField(fieldName = "email")
public String email;
}
Define field as the primary key of the entity table
- constraints:
- must be on a field annotated with MDField
- examples:
@MDEntity(tableName = "user")
public class User {
@MDId
@MDField(fieldName = "id", allowedSQLActions = SELECT)
public BigDecimal id;
@MDField(fieldName = "email")
public String email;
}
Define field as a link of the entity table to the parent class table
- constraints:
- must be on a field annotated with MDField
- MDField must be a foreign key referencing the parent entity primary key
- examples:
@MDEntity(tableName = "user")
public class User {
@MDId
@MDField(fieldName = "id", allowedSQLActions = SELECT)
public BigDecimal id;
@MDField(fieldName = "email")
public String email;
}
@MDEntity(tableName = "player")
public class Player extends User {
@MDId
@MDField(fieldName = "id_player")
public BigDecimal idPlayer;
@MDInheritLink
@MDField(fieldName = "id_user")
public BigDecimal idUser;
@MDField(fieldName = "pseudo")
public String pseudo;
}
NB: This feature is not really useful, I made it as a Proof of Concept for linking entities between them before making @MDManyToOne and @MDOneToMany
Define field as a foreign key link to the referenced entity
- constraints:
- field must be public
- field must be an MDEntity object reference
- field must NOT be annotated with MDField
- properties:
- fieldName: field name of the link key in database
- targetFieldName: field name of the target key
- target: entity to link
- loadPolicy:
- LAZY: avoid loading of linked entity when current one is retrieved
- HEAVY: force loading of linked entity when current one is retrieved
- examples:
@MDEntity(tableName = "user")
public class User {
@MDId
@MDField(fieldName = "id", allowedSQLActions = SELECT)
public BigDecimal id;
@MDField(fieldName = "email")
public String email;
}
@MDEntity(tableName = "user_token")
public class UserToken {
@MDManyToOne(fieldName = "id_user", targetFieldName = "id", target = User.class, loadPolicy = HEAVY)
public User user;
@MDField(fieldName = "token")
public String token;
}
Define field as a foreign key link to the referenced entity
- constraints:
- field must be public
- field must be a collection of MDEntity object
- field must NOT be annotated with MDField
- properties:
- fieldName: field name of the link key in database
- targetFieldName: field name of the target foreign key
- target: entity to link
- loadPolicy:
- LAZY: avoid loading of linked entity when current one is retrieved
- HEAVY: force loading of linked entity when current one is retrieved
- examples:
@MDEntity(tableName = "user")
public class User {
@MDId
@MDField(fieldName = "id", allowedSQLActions = SELECT)
public BigDecimal id;
@MDField(fieldName = "email")
public String email;
@MDOneToMany(fieldName = "id", targetFieldName = "id_user", target = UserToken.class, loadPolicy = HEAVY)
public List<UserToken> userTokens;
}
@MDEntity(tableName = "user_token")
public class UserToken {
@MDManyToOne(fieldName = "id_user", targetFieldName = "id", target = User.class, loadPolicy = HEAVY)
public User user;
@MDField(fieldName = "token")
public String token;
}
The MDConnectionConfig object allow you to setup a connection with a database. You can instantiate multiple MiniDAO connections and MDConnectionConfig.
This object has the following properties:
- MDDriver driver:
The driver is used by MiniDAO to build the uri and instantiate Connection.
For now, supported drivers are MYSQL, ORACLE and H2 (file only). In the future, MiniDAO will be able to do specific requests based on this value. - String url:
Ip address or domain name of the database server - String port:
Port used to access database server instance - String login:
Login of the user - String password:
Password of the user - String instance:
Name of database
In progress
In progress
In progress
In progress
When the default methods are not enough to get what you want, you can use Query Builders to make your own queries and execute them.
public class UserRetriever {
private MiniDAO miniDAO;
public UserRetriever(MiniDAO miniDAO) {
this.miniDAO = miniDAO;
}
public List<User> getUsersByFirstName(String firstName) throws MDException {
MDSelectBuilder selectBuilder = new MDSelectBuilder();
selectBuilder.select().from(User.class).where("first_name", EQUAL, firstName);
String query = selectBuilder.build();
ResultSet resultSet;
List<User> users;
try {
resultSet = miniDAO.executeQuery(query);
users = miniDAO.mapResultSetToEntities(resultSet, User.class);
} catch (MDException exception) {
throw exception;
}
return users;
}
}
In progress
In progress
In progress
MiniDAO is developed and distributed under the terms of the MIT License