# 5.2 ActiveRecordPlugin

ActiveRecord exists as a Plugin within JFinal, so when using it, you need to configure the ActiveRecordPlugin in JFinalConfig.

Here's a sample code for the Plugin configuration:

public class DemoConfig extends JFinalConfig {
  public void configPlugin(Plugins me) {
  DruidPlugin dp = new DruidPlugin("jdbc:mysql://localhost/db_name", "userName", "password");
    me.add(dp);
    ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
    me.add(arp);
    arp.addMapping("user", User.class);
    arp.addMapping("article", "article_id", Article.class);
  }
}
1
2
3
4
5
6
7
8
9
10

The above code configures two plugins: DruidPlugin and ActiveRecordPlugin. The former is the druid data source plugin, and the latter supports ActiveRecord. In ActiveRecord, the method addMapping(String tableName, Class<? extends Model> modelClass) is defined, which establishes a mapping relationship between the database table name and the Model.

Furthermore, in the code above, arp.addMapping("user", User.class) assumes the default primary key name for the table is "id". If the primary key name is "user_id", it needs to be specified manually, as in: arp.addMapping("user", "user_id", User.class).

Important: The above arp.addMapping(...) configuration can be automated with the jfinal generator, eliminating the need for manual addition of such configurations. For detailed usage, refer to the documentation: https://www.jfinal.com/doc/5-4.

Last Updated: 9/17/2023, 6:16:57 AM