jfinal的Db+Record模式示例

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

相关文章
相关标签/搜索