How to use the Squeryl framework in the Java library
The method of using the Squeryl framework in the Java library
Squeryl is a simple and powerful ORM (object relationship mapping) framework, which can help us easily process the mapping between the object and the relationship database in the Java application.This article will introduce how to use the Squryl framework in the Java library and provide some code examples.
1. Add squeryl dependencies
First of all, we need to add Squeryl dependencies to the construction file (such as Maven's pom.xml).Add the following code to the <DependenCies> tags in the pom.xml file:
<dependency>
<groupId>org.squeryl</groupId>
<artifactId>squeryl_2.12</artifactId>
<version>0.9.9-6</version>
</dependency>
If you are using Gradle, you can add the following code to the Build.gradle file:
groovy
compile 'org.squeryl:squeryl_2.12:0.9.9-6'
2. Create a database connection
Before using Squeryl, you need to create a database connection.You can use any standard JDBC driver to connect to the database, such as MySQL JDBC driver.Here are a sample code to create mysql database connection:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
private static final String DB_URL = "jdbc:mysql://localhost:3306/mydatabase";
private static final String DB_USERNAME = "username";
private static final String DB_PASSWORD = "password";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
}
}
3. Create a physical class
Next, define the physical class in the Java code, indicating the table in the database.Each physical class should have a non -ginseng constructor, and use squeryl's@column` annotation to define the name in the table.For example, the following is a physical class that indicates the user:
import org.squeryl.annotations.Column;
public class User {
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
// omit the getter and setter method
}
4. Create a DAO class
In order to access the database table corresponding to the physical class, we need to create a DAO (data access object) class.The DAO class is responsible for performing the operation with database interaction, such as querying, inserting, updating and deleting.Here are a sample code using Squryl DAO class:
import org.squeryl.Query;
import org.squeryl.Session;
import org.squeryl.SessionFactory;
import org.squeryl.adapters.H2Adapter;
public class UserDao {
private SessionFactory sessionFactory;
public UserDao() {
sessionFactory = SessionFactory$.MODULE$.jdbcSession(() -> DatabaseConnection.getConnection(), new H2Adapter());
}
public Query<User> getAllUsers() {
Session session = sessionFactory.currentSession();
return session.query(User.class);
}
public void addUser(User user) {
Session session = sessionFactory.currentSession();
session.insert(user);
}
// omit other database operation methods
}
5. Execute the database operation
Now, we can use the above DAO class in the Java code to perform the database operation.The following is an example of code using the DAO class:
public class Main {
public static void main(String[] args) {
UserDao userDao = new UserDao();
// Get all users
Query<User> users = userDao.getAllUsers();
// Print user information
for (User user : users) {
System.out.println("ID: " + user.getId() + ", Name: " + user.getName());
}
// Add new users
User newUser = new User();
newUser.setName("John");
userDao.addUser(newUser);
}
}
Through the above steps, we can use the Squeryl framework in the Java library to operate the database.I hope this article will help you!