2017-07-31 08:59

@淡定007 你看一眼源码就知道了 , 是强转, 所以可以为null

2017-07-28 17:19

@爪爪

如果是 为某个数据源配置不同的cache 那么
// 配置ActiveRecord插件 的时候 就可以切换了,
ActiveRecordPlugin arp1 = new ActiveRecordPlugin("xxx1", druidPlugin1);
// 缓存使用
arp1.setCache(new RedisCache());

ActiveRecordPlugin arp2 = new ActiveRecordPlugin("xxx2", druidPlugin2);
// 缓存使用
arp2.setCache(new XxxxCache());

2017-07-28 17:09

@爪爪
上面例子中是在 PropKit.get("cache_type", "ehcache"); 中配置的,
配置文件中有个叫 cache_type=xxx 的

private static String getCacheType() {
return PropKit.get("cache_type", "ehcache");
}

-------------------------------------------------
还是看你的需求, 可以说说业务场景, 分析一下怎么搭配比较方便
如果是动态的随时去更改,
可以把 PropKit.get("cache_type", "ehcache");
改成 :
private static final ThreadLocal cache_type = new ThreadLocal();
private static String getCacheType() {
String cache_type = cache_type.get("cache_type")
return cache_type != null ? cache_type : "ehcache";
}

publicstatic String setCacheType(String cache_type) {
cache_type.set(cache_type);
}
//快捷
publicstatic String setCacheTypeEhcache() {
setCacheType("ehcache");
}
publicstatic String setCacheTypeXxxcache() {
setCacheType("xxxx");
}

2017-07-28 12:19

注意:MySql 数据库表必须设置为 InnoDB 引擎时才支持事务,MyISAM 并不支持事务。

2017-07-28 12:16

那是受影响行数

2017-07-28 12:13

@魑魅魍魉-two Record 是非常棒的大招 , 比如你的系统中表字段会动态的增减或没主键等, 这个就非常合适, 用model反而不方便了,还需再更新一下TableMapping才能用.

model适合已知固定字段的业务, 常见业务一般都会定下字段和主键等,

根据业务场景选择合适的~

2017-07-28 09:27

@爪爪 RedisPlugin 挂参不一样就可以了 , 可以挂不同的IP,端口等, 来切换到不同的或多个 redis 服务 , 通过 cacheName 即可取到他们了 ,

2017-07-27 21:28

通过专用的工具(官网demo中有)生成的这样更方便吧 ? 通过ide提示就可以看见了,
public Role setName(String name){
return set("name", name);
}

2017-07-27 21:10

鼠标右键> 文件另存为>> OK

2017-07-27 21:06

@淡定007 generate jfinal 有专用的生成器

2017-07-27 21:05

@淡定007
回复: 这么写 就有数据了 但又发现mls转json都是{}空对象

答:
TreeNode 确定没有继承 Model 吗? 如果是继承的, 那么setId 等方法内部 需要这样写 set("id", id) ,
原因是: 源码中这样写到 attrs.put(attr, value);
public M set(String attr, Object value) {
Table table = getTable(); // table 为 null 时用于未启动 ActiveRecordPlugin 的场景
if (table != null && !table.hasColumnLabel(attr)) {
throw new ActiveRecordException("The attribute name does not exist: \"" + attr + "\"");
}

attrs.put(attr, value);
getModifyFlag().add(attr); // Add modify flag, update() need this flag.
return (M)this;
}

然后默认解析json的时候源码这样写的:

if (value instanceof Model) {
Map map = com.jfinal.plugin.activerecord.CPI.getAttrs((Model)value);
return mapToJson(map, depth);
}
if (value instanceof Record) {
Map map = ((Record)value).getColumns();
return mapToJson(map, depth);
}

而:
com.jfinal.plugin.activerecord.CPI.getAttrs((Model)value);
中调
protected Map getAttrs() {
return attrs;
}

所以:
如果你用
public void setId(String id) {
this.id = id;
}

那么 attrs Map中就没有值了~~
json中也就没有值了~
这个就是原因了~
再问需要发5毛红包了 ~2333

2017-07-27 17:16

@linuxea
http://www.jfinal.com/share/299

这个例子刚好适合你说的场景 ,

如果想时时的改变, 还可以在上述例子中改写
private static String getCacheType() {
return PropKit.get("cache_type", "ehcache");
}

把 PropKit.get("cache_type", "ehcache");
改成 :
private static final ThreadLocal cache_type = new ThreadLocal();
private static String getCacheType() {
String cache_type = cache_type.get("cache_type")
return cache_type != null ? cache_type : "ehcache";
}

publicstatic String setCacheType(String cache_type) {
cache_type.set(cache_type);
}
//快捷
publicstatic String setCacheTypeEhcache() {
setCacheType("ehcache");
}
publicstatic String setCacheTypeXxxcache() {
setCacheType("xxxx");
}

外面同线程的地方 调用一下set即可了, 是不是很简单了?~ 点个赞呗~

2017-07-27 16:57

@杜福忠 之前研究过一下
http://www.jfinal.com/share/299

2017-07-27 16:56

这样配置的:
ActiveRecordPlugin arp = new ActiveRecordPlugin(DbKit.MAIN_CONFIG_NAME, druidPlugin);
arp.setCache(new MyCache());

// 自己实现 接口 ICache 即可切换任何 Cache
public class MyCache implements ICache {
//.......
T get(String cacheName, Object key);
void put(String cacheName, Object key, Object value);
void remove(String cacheName, Object key);
void removeAll(String cacheName);
}