JFinal插件配置java
//配置插件 public void configPlugin(Plugins me) { // 配置數據庫鏈接池插件 C3p0Plugin c3p0Plugin1 = new C3p0Plugin("Url","用戶名","密碼"); me.add(c3p0Plugin1); //啓用ActiveRecordPlugin插件 ActiveRecordPlugin activeRecordPlugin1 = new ActiveRecordPlugin(c3p0Plugin1); //添加數據庫方言 activeRecordPlugin1.setDialect(new AnsiSqlDialect()); me.add(activeRecordPlugin1); // 映射formModel 表到 FormModel模型 activeRecordPlugin1.addMapping("","",FormModel.class); //配置ehcache插件 me.add(new EhCachePlugin("src/ehcache.xml")); }
ehcache.xmlsql
<?xml version="1.0" encoding="utf-8" ?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> <diskStore path="java.io.tmpdir"/> <cache name="orgCodes" maxElementsInMemory="10000" eternal="true" overflowToDisk="true" copyOnRead="true" copyOnWrite="true"/> <cache name="activitys" maxElementsInMemory="10000" eternal="true" overflowToDisk="true" copyOnRead="true" copyOnWrite="true"/>
<!--
緩存配置
name:緩存名稱。
maxElementsInMemory:緩存最大個數。
eternal:對象是否永久有效,一但設置了,timeout將不起做用。
timeToIdleSeconds:設置對象在失效前的容許閒置時間(單位:秒)。僅當eternal=false對象不是永久有效時使用,可選屬性,默認值是0,也就是可閒置時間無窮大。
timeToLiveSeconds:設置對象在失效前容許存活時間(單位:秒)。最大時間介於建立時間和失效時間之間。僅當eternal=false對象不是永久有效時使用,默認是0.,也就是對象存活時間無窮大。
overflowToDisk:當內存中對象數量達到maxElementsInMemory時,Ehcache將會對象寫到磁盤中。
diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每一個Cache都應該有本身的一個緩衝區。
maxElementsOnDisk:硬盤最大緩存個數。
diskPersistent:是否緩存虛擬機重啓期數據 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。
memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。默認策略是LRU(最近最少使用)。你能夠設置爲FIFO(先進先出)或是LFU(較少使用)。
clearOnFlush:內存數量最大時是否清除。
--> </ehcache>
FormModel.java
public class FormModel extends FormBean<FormModel> { public static final FormModel FORM_MODEL = new FormModel(); }
FormBean.java
public abstract class FormBean<M extends FormBean<M>> extends Model<M> implements IBean{ private int getId() { int id = getLong("id").intValue();
//intValue把獲取到的long類型的值初始化爲int型 return id; } private String getCode() { String code = getStr("code"); return code; }public Map<String,Object> getData() { Map<String,Object> data = new HashMap<>(); data.put("id",getId()); data.put("code",getCode()); data.put("字段名",上面聲明的get方法); //等等return data; } }
JFinal首創Db + Record模式示例
JFinal配備的ActiveRecord插件,除了實現了相似Rails ActiveRecrod的功能以外,還實現了
Db + Record模式,此模式下,開發者甚至能夠連Model都不須要寫就能夠輕鬆操做數據庫,
如下是示例代碼:
數據庫
// 建立name屬性爲James,age屬性爲25的record對象並添加到數據庫 Record user = new Record().set("name", "James").set("age", 25); Db.save("user", user); // 刪除id值爲25的user表中的記錄 Db.deleteById("user", 25); // 查詢id值爲25的Record將其name屬性改成James並更新到數據庫 user = Db.findById("user", 25).set("name", "James"); Db.update("user", user); // 查詢id值爲25的user, 且僅僅取name與age兩個字段的值 user = Db.findById("user", 25, "name, age"); // 獲取user的name屬性 String userName = user.getStr("name"); // 獲取user的age屬性 Integer userAge = user.getInt("age"); // 查詢全部年齡大於18歲的user,並輸出其name屬性 List<Record> users = Db.find("select * from user where age > 18"); // 分頁查詢年齡大於18的user,當前頁號爲1,每頁10個user Page<Record> userPage = Db.paginate(1, 10, "select *", "from user where age > ?", 18);
獲取緩存中的值緩存
/** * 從緩存中獲取數據,若是有數據直接獲取,若是沒有數據則查詢數據庫 * */ Map<String,List<Map<String,Object>>> resultdata = CacheKit.get("unfinishedActivityOrg", orgCode + activityType + methodName + page + pageSize, new IDataLoader() { @Override public Map<String,List<Map<String,Object>>> load() { Map<String,List<Map<String,Object>>> result = new HashMap<>(); List<Record> records = Db.find(sql.toString(), orgCode + "%", "%" + activityType + "%", year, year, (page - 1) * pageSize, pageSize); List<Map<String,Object>> arr = new ArrayList<>(); /** * 拼裝數據 * */ for (Record record : records) { String cellPhone=record.getStr("phone"); String fixPhone = record.getStr("fixed_phone"); if(StrKit.isBlank(cellPhone)||"null".equals(cellPhone)){//若是聯繫電話或者固話爲null那麼設置爲暫無數據 cellPhone = "暫無數據"; } if(StrKit.isBlank(fixPhone)||"null".equals(fixPhone)){ fixPhone = "暫無數據"; } Map<String,Object> data = new HashMap<>(); data.put("name",record.getStr("orgName")); data.put("head",record.get("name")); data.put("phone",cellPhone+"/"+fixPhone); data.put("key",record.getLong("id")); arr.add(data); } result.put("tablePaneData",arr); return result; } });
這是我百度+本身整合的app