Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

5.5 JFinal's original Db-Record mode


May 14, 2021 JFinal manual



The Db class and its companion Record class provide richer database operation capabilities outside of the Model class. W ith the Db and Record classes, there is no need to map the database tables, and Record is equivalent to a generic Model. Here are some common uses of the Db-Record pattern:

Create a record object with a name property of James and an age property of 25 and add it to the database
Record user = new Record().set("name", "James").set("age", 25); Db. save ("user", user);

Delete records in the user table with an id value of 25
Db. deleteById ("user", 25);

Record, which has a query id value of 25, changed its name property to James and updated it to the database user s db. findById ("user", 25).set ("name", "James"); Db. update ("user", user);

Gets the name property of the user
String userName = user.getStr("name");
Gets the age property of the user
Integer userAge = user.getInt("age");

Find out all user over 18 years of age
List<Record> users = Db. find ("select * from user where age > 18");

Paged queries for user older than 18, with the current page number of 1, with 10 user per page
Page<Record> userPage = Db. paginate (1, 10, "select *", "from user where age > ?", 18);

Here's an example of transaction processing:

boolean succeed = Db. tx ( new IAtom(){
public boolean run() throws SQLException {
int count = Db. update ("update account set cash = cash - ? where id = ?", 100, 123);
int count2 = Db. update ("update account set cash = cash + ? where id = ?", 100, 456);
return count == 1 && count2 == 1;
}});

The above two database update operations are performed in one transaction and automatically roll back the transaction if an exception occurs during execution or if the invoke() method returns false.