JFinal-学习笔记(下)

第五章 ActiveRecord
ActiveRecord是JFinal最核心的组成部分。
1、 ActiveRecord是作为JFinal的plugin存在的,需要在JFinalConfig中的configPlugin中配置

需要在Plugin配置中,配置c3p0插件和ActiveRecordPlugin插件 两个信息
ActiveRecordPlugin 还要有Model类和数据库表的映射信息。
该映射默认表名的主键名为 id,如果主键名是别的,则需要手动指定。
2、Model
public class User extends Model<User>{
public static final User dao = new User();
}
dao对象是全局共享,只能用于数据库查询,不能用于数据承载对象。数据承载对象需要使用 new User().set(...)来实现
无需set/get,无需xml,无需annotation

一些常见的方法
new User().set("name","James").save();//新建对象并保存
getModel(User.class).save();//获取前台传入的model对象,并保存
User.dao.deleteById(getParaToInt());//获取前台传入的userid,并删除指定对象
List<User> users = User.dao.find("select * from user where age>18");//获取大于18岁的User集合
getModel(User.class).update();//获取前台传入的model对象(需要有主键信息),并更改
3、JavaBean与Model合体
JFinal 可以自动生成 Model、BaseModel、MappingKit、DataDictionary四类文件
4、JFinal还有Db+Recod模式
Record类 相当于一个通用的Model

Record user = new Record().set("name","James");
Db.save("user",user);

Db.deleteById("user",25);
5、声明式事务
ActiveRecord支持声明式事务,需要使用ActiveRecord提供的拦截器来实现。

6、Cache
在ehcache.xml中配置cache后,即可在ActiveRecord中使用,如
List<Blog> blogList = Blog.dao.findByCache("cacheNmae","key","select * from blog");
7、 ActiveRecord 支持多个数据库的Dialect
arp.setDialect(new MysqlDialect());
8、 表关联
一是直接使用sql得到关联数据。
二是在model中添加获取关联数据的方法

public class User extends Model<User>{
public static final User dao = new User();

public List<Blog> getBlogs(){
return Blog.dao.find("select * from blog where user_id=?",get("id"));
}
}
9、 复合主键
在映射时,指定复合主键名称,使用时同时指定复合主键值即可。
Db+Record模式,直接使用即可
Db.findById("user_role","roleId,userId",123,345);
10、针对Oracle
注意属性名的大小写、自增主键
11、多数据源支持
ActiveRecordPlugin可同时支持多数据源、多方言、多缓存、多事务级别
对每个ActiveRecordPlugin指定一个configName即可

在使用时,使用use方法,指定数据源即可
Blog b = Blog.me.findById(123);
b.use("backupDatabase").save();
12、非web环境下使用ActiveRecord
调用ARP插件的start方法后,也可使用,ARP依赖的其他插件同样需要调用start方法
第六章 EhCachePlugin
是JFinal集成的缓存插件
1、 需要在JFinalConfig中配置
plugin.add(new EhCachePlugin());
2、 CacheInterceptor
使用注解,并在ehcache.xml中配置一actionKey命名的cache,如 <cache name="/blog/list" ...>,"/"不可以省略
也可使用 @CacheName("blogList") 用来取代默认的actionKey作为actionNmae;
3、EvictInterceptor
根据CacheName注解自动清除缓存。
4、CacheKit
缓存操作工具类
5、ehcache.xml
见EhCache官方文档
第七章 RedisPlugin
1、同样需要在 configPlugin中配置
2、Redis与Cache
Redis对象通过 use()方法来获取到Cache对象。
3、非web环境使用RedisPlugin
引入jfinal.jar,多调用一下 redisPlugin.start()即可
第八章 Validator
1、Validator是一个拦截器,配置方式与拦截器完全一样
protected void validator(Controller c) 可以调用validateXxx(..)系列方法来进行后端校验
protected void handleError(Controller c) 可以调用c.keepPara(..)方法来将提交的值传回页面,c.render(..)方法来返回响应的页面。
keepModel方法可以保持住用户输入过的数据。
2、配置示例

public class UserController extends Controller{
@Before(LoginValidator.class)
public void login(){
//code
}
}
第九章 国际化

第十章 FreeMaker
JFinal默认使用FreeMaker作为View
如需要使用jsp作为默认视图,需要 在 configConstant方法中配置
cons.setViewType(ViewType.JSP);
2、示例
<table>
<#list userList as user>
<tr>
<td>${user.name}</td> //表中的列名
</tr>
</#list>
</table>
第十一章 JFinal架构
JFinalFilter——Handler1-N——ActionHandler(Interceptor-Controller-Render)

Plugin

posted @ 2017-02-23 09:16  夜星之光  阅读(2064)  评论(0编辑  收藏  举报