NoSQL (MongoDB) Example
This example demonstrates the usage of the j-framework to data access on a MongoDB instead of a relational database.
Prerequisites:
-
You should have JDK 17+ Installed. (Click Here).
-
You should create a Maven Project.
-
You should enable snapshot versions. (Click Here).
Project Content:
This section contains what you should add to your code structure for this example.
-
Maven Project with pom.xml that has the following contents:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jalalkiswani</groupId>
<artifactId>j-app-webstack</artifactId>
<version>7.0.0-SNAPSHOT</version>
</parent>
<artifactId>j-framework-data-nosql-mongo-example</artifactId>
</project>
-
Database configurations file located at
src/main/resources/config.properties
that has the following content:
nosql.url=mongodb://localhost:27017/
nosql.db.name=app
nosql.db.user=
nosql.db.password=
-
Account Model class located at
src/main/app/Account.java
that has the following content:
package com.app;
import java.util.UUID;
import com.jk.core.util.JKObjectUtil;
public class Account {
String id = UUID.randomUUID().toString();
String name;
double balance;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString() {
return JKObjectUtil.toString(this, false);
}
}
-
Main java class located at
src/main/java/com/app/App.java
which contains the following:
package com.app;
import java.util.List;
import com.jk.core.util.JK;
import com.jk.data.dataaccess.JKDataAccessFactory;
import com.jk.data.dataaccess.nosql.JKNoSqlDataAccess;
public class App {
private static String id;
public static void main(String[] args) {
JKNoSqlDataAccess dataAccess = JKDataAccessFactory.getNoSqlDataAccess();
insert(dataAccess);
getAll(dataAccess);
find(dataAccess);
update(dataAccess);
delete(dataAccess);
}
private static void insert(JKNoSqlDataAccess dataAccess) {
Account account = new Account();
account.setName("Jalal");
account.setBalance(100);
// insert the object in the database using JPA/Hibernate implementation
dataAccess.insert(account);
id = account.getId();
}
private static void getAll(JKNoSqlDataAccess dataAccess) {
// Retrieve list of objects from database using JPA
List<Account> list = dataAccess.getList(Account.class);
for (Account account : list) {
JK.print(account);
}
}
private static Account find(JKNoSqlDataAccess dataAccess) {
// Find an object from database using JPA
Account std = dataAccess.find(Account.class, id);
return std;
}
private static void update(JKNoSqlDataAccess dataAccess) {
Account std = find(dataAccess);
std.setName("Updated Jalal");
// update record in the databse
dataAccess.update(std);
}
private static void delete(JKNoSqlDataAccess dataAccess) {
dataAccess.delete(Account.class, id);
}
}
Alternatively, you can clone or download the tutorial repository then import the project into your IDE. |