Jfinal数据库基本操作(—) 连接数据库以及Model创建


连接数据库需要的步骤:

0.放入需要的jar包

1.DemoConfig的插件配置

public void configPlugin(Plugins me) {  
//添加插件  
me.add(new EhCachePlugin());  

DruidPlugin druidPlugin = new
DruidPlugin("jdbc:mysql://127.0.0.1:3306/jfinal_demo", "root","admin");  
me.add(druidPlugin);  

//激活插件  
ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(druidPlugin);  
activeRecordPlugin.addMapping("user", UserModel.class); //需要自行创建UserModel类  
me.add(activeRecordPlugin);  
}

2.配置UserController类,进行操作

public void action1() {  
System.out.println("<<<<<<<<<<<action1>>>>>>>>>");  
//增  
UserModel user = new UserModel();  
user.set("name","JJJJJC");  
user.set("password", 6987745);  
user.save();  
//通过id查询数据  
UserModel dbModel = UserModel.DAO.findById(user.getLong("id"));  


String name = user.getStr("name");  
String password = user.getStr("password");  
renderJson("name:"+name+" password:"+password);


}

3.创建UserMode

public class UserModel extends Model<UserModel>{

private static final long serialVersionUID = 1L;  
public static final UserModel DAO = new UserModel();  
}


原文链接:https://www.cnblogs.com/Mortty/p/13378552.html