The best practice of using the Squryl framework in the Java class library for database operation
In Java development, using the Squeryl framework for database operations is a common choice.Squeryl is a simple, elegant and easy -to -use ORM (object relationship mapping) framework. It provides a way to use the SCALA language to write a type of safe query language to operate the database.
This article will introduce the best practice of using the Squryl framework in the Java library for database operations.
Step 1: Add squeryl dependencies
First, add Squeryl dependencies in your Java project in POM.XML or Build.gradle files.You can find the latest version and dependency configuration on Squeryl's official website.
For Maven project, add the following dependencies:
<dependency>
<groupId>org.squeryl</groupId>
<artifactId>squeryl_2.13</artifactId>
<version>0.9.11</version>
</dependency>
For the Gradle project, add the following dependencies:
groovy
implementation 'org.squeryl:squeryl_2.13:0.9.11'
Step 2: Create a database connection
Before starting to use Squeryl, we need to create a database connection.Squeryl can be used with any standard JDBC driver.
Below is an example code using HikaricP as a connection pool:
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.squeryl.Session;
import org.squeryl.SessionFactory;
import org.squeryl.adapters.H2Adapter;
import javax.sql.DataSource;
public class DatabaseConnection {
private static SessionFactory sessionFactory;
public static void initialize() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
config.setUsername("your-username");
config.setPassword("your-password");
config.setMaximumPoolSize(10);
DataSource dataSource = new HikariDataSource(config);
sessionFactory = SessionFactory.buildSession(
dataSource,
new H2Adapter()
);
}
public static Session getSession() {
return sessionFactory.currentSession();
}
}
In this example, we use HIKARICP as the connection pool and specify the H2 database connection URL, username and password.You can change these configurations according to your needs.
Step 3: Define the physical class and database tables
Next, we need to define the physical class and database tables.Squeryl uses Case Class to represent the line of database tables.The case class is a simple POJO class, which has some attributes to maximize the database table.
The following is an example code that indicates the case class of the user:
import org.squeryl.annotations.Column;
import org.squeryl.annotations.Table;
@Table("users")
public class User {
@Column("id")
private Long id;
@Column("name")
private String name;
// Getters and setters
}
In this example, we used the name and list name of `@table` and@column` to specify the table name and list name.
Step 4: Define data access objects (DAO)
When performing database operations, using data access objects (DAO) is a common design mode.DAO is responsible for handling interaction with the database, including insertion, updating, deleting and querying operations.
The following is an example code that indicates the user data access object:
import org.squeryl.Session;
public class UserDao {
private static final Session session = DatabaseConnection.getSession();
public void insert(User user) {
session.insert(user);
}
public void update(User user) {
session.update(user);
}
public void delete(User user) {
session.delete(user);
}
public User getById(Long id) {
return session
.from(Tables.users)
.where(user -> user.id.eq(id))
.select()
.single();
}
}
In this example, we use the method of squeryl using the method of `databaseconnection.getSession ()` `method.We can use session objects to perform operations such as insertion, update, delete, and query.
Step 5: Execute the database operation
Now, we can use squeryl in the Java library for database operations.
Below is a sample code that demonstrates how to use the UserDao class to insert a user to the database:
public class Main {
public static void main(String[] args) {
DatabaseConnection.initialize();
UserDao userDao = new UserDao();
User user = new User();
user.setId(1L);
user.setName("John Doe");
userDao.insert(user);
}
}
In this example, we first call the method to initialize the database connection of `databaseconnection.Initialize ()` method.Then, we create a UserDao instance, create a new user object and set its ID and name.Finally, we call the `UserDao.Insert (user) method insert the user into the database.
Through the above steps, you can use the Squeryl framework for database operations.It should be noted that this article is just a simple example. You can perform more complicated database operations and inquiries according to your needs.
I hope this article will help you use the Squeryl framework in the Java library for database operations!