# 5.15 Standalone Use of ActiveRecord

The ActiveRecordPlugin can operate independently of the Java web environment, and it can run in any regular Java program. It's straightforward to use: the only difference compared to web projects is that you need to manually invoke its start() method to use it immediately. Below is a code example:

public class ActiveRecordTest {
  public static void main(String[] args) {
    DruidPlugin dp = new DruidPlugin("localhost", "userName", "password");
    ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
    arp.addMapping("blog", Blog.class);

    // The only difference from the JFinal web environment is that you need to manually call the start() method for the relevant plugins
    dp.start();
    arp.start();

    // After the above simple lines of code, you can start using it immediately
    new Blog().set("title", "title").set("content", "cxt text").save();
    Blog.dao.findById(123);
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Note: You must also manually start the other plugins that ActiveRecordPlugin depends on, as shown with dp.start() in the example.

The ActiveRecord module of JFinal has been independently released to the Maven repository. If you want to use only the JFinal ActiveRecord module without including the entire JFinal, you can use the following Maven dependency:

<dependency>
    <groupId>com.jfinal</groupId>
    <artifactId>activerecord</artifactId>
    <version>5.1.2</version>
</dependency>
1
2
3
4
5

The usage of this standalone module is exactly the same as when used within JFinal.

Special Note: The ActiveRecord module includes the Enjoy Template module. If you want to use the Enjoy Template engine, you can use it directly without adding the Maven dependency for the Enjoy Template module, as that would cause conflicts.

If you want to integrate it with Spring Boot, you can refer to this article: Integrating JFinal ActiveRecord with Spring Boot (opens new window).

Last Updated: 9/21/2023, 8:42:09 AM