Home  >  Article  >  Java  >  Detailed explanation of examples of problems with JFinal getModel method and database usage in Java

Detailed explanation of examples of problems with JFinal getModel method and database usage in Java

黄舟
黄舟Original
2017-04-15 09:08:032314browse

This article mainly introduces the JFinal getModel method in java and the relevant information on solutions to problems in database use. Friends in need can refer to the

JFinal getModel method (get the Model from the page form Object) + database storage issue

1. getmodel method

1. Database mapping in the JConfig configuration class (stored to This configuration is required for 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("成功");
    } catch (Exception e) {
      System.out.println("连接失败");
    }
    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 is the 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;
  // 声明一个全局操作的变量
  public final static GOneQuestion questiondao = new GOneQuestion();
}

3. Front-end form


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

name="Modelname.atrrname" in the front-end means: For example, the model in this example is GOneQuestion, and the attribute in the form is A, so the name is: gOneQuestion.A

Note: Only the first letter becomes lowercase. Others remain unchanged

4.getmodel gets


GOneQuestion question =getModel(GOneQuestion.class);

2. Database storage issues

jfianl documentation: The public static final User dao object defined in

User is globally shared and can only be used for database queries and cannot be used for data bearing objects. . Data carrying needs to be implemented using new User().set(…).

means: For example, the questiondao defined by the model in this example can only be used for query and cannot be used to insert data.

When inserting data: (If used incorrectly, primary key duplication will occur)


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

DeleteWhen adding data: GOneQuestion.questiondao. Method name

The above is the detailed content of Detailed explanation of examples of problems with JFinal getModel method and database usage in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn