The JFinal getModel method and database use in java are problem solving

  • 2020-06-23 00:29:48
  • OfStack

JFinal getModel method (get Model object from page form) + database storage problem

1. getmodel method

1. Database mapping in the JConfig configuration class (this configuration is required when storing to the database)


public void configPlugin(Plugins me) {
  C3p0Plugin cp = null;
  try {
   cp = new C3p0Plugin(
     "jdbc:mysql://localhost:3306/huaxuetang?useUnicode=true&characterEncoding=utf-8",
     "root", "1234");
   System.out.println(" successful ");
  } catch (Exception e) {
   System.out.println(" The connection fails ");
  }
  me.add(cp);
  ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);
  arp.setShowSql(true);
  me.add(arp);
  arp.addMapping("bse_user", "id", User.class);
  arp.addMapping("grade_one_choice","id",GOneQuestion.class);
 }

arp. There are three parameters in addMapping (), the first is the database table name, the second primary key, and the third is the corresponding Model class name

2. Model class


import com.jfinal.plugin.activerecord.Model;

public class GOneQuestion extends Model<GOneQuestion>{
 private static final long serialVersionUID = 1L;
 //  The statement 1 A globally operated variable 
 public final static GOneQuestion questiondao = new GOneQuestion();
}

3. Front-end forms


<input type="text" name="gOneQuestion.A" class="required" maxlength="50" style="width: 250px"/>

name in the front end = "Modelname. atrrname" meaning: for example, model in this example is GOneQuestion, the attribute in the form is A, so name is gOneQuestion. A

Note: only the first letter is lowercase, nothing else

4. getmodel acquisition


GOneQuestion question =getModel(GOneQuestion.class);

2. Database storage problem

In the jfianl documentation:

The public static final dao object defined in User is globally Shared and can only be used for database queries, not for data bearer objects. Data hosting requires new User().set(...) To implement.

questiondao, as defined by model in this example, can only be used for queries, not for inserts.

When inserting data :(duplicate primary key problem occurs when using the wrong key)


new GOneQuestion()
    .set("book", question.getStr("book"))
    .save();

Delete add data when: GOneQuestion. questiondao. method name


Related articles: