.
This commit is contained in:
49
src/main/java/com/lxyer/controller/HomeController.java
Normal file
49
src/main/java/com/lxyer/controller/HomeController.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.lxyer.controller;
|
||||
|
||||
import com.jfinal.aop.Before;
|
||||
import com.jfinal.plugin.activerecord.Db;
|
||||
import com.jfinal.plugin.activerecord.SqlPara;
|
||||
import com.jfinal.plugin.ehcache.CacheInterceptor;
|
||||
import com.jfinal.plugin.redis.Cache;
|
||||
import com.jfinal.plugin.redis.Redis;
|
||||
import com.lxyer.model.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by JUECHENG at 2018/1/7 14:40.
|
||||
*/
|
||||
public class HomeController extends IController {
|
||||
|
||||
public void index(){
|
||||
String para = getPara();
|
||||
|
||||
Cache cache = Redis.use();
|
||||
String cacheKey = "user-" + para;
|
||||
|
||||
User user = cache.get(cacheKey);
|
||||
if (user == null)
|
||||
cache.setex(cacheKey, 10, user = User.dao.findById(para+""));
|
||||
|
||||
if (user != null)
|
||||
System.out.println(user.toJson());
|
||||
|
||||
renderText("hello fly");
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询用户
|
||||
*/
|
||||
@Before(CacheInterceptor.class)
|
||||
public void query_user(){
|
||||
|
||||
//User.dao.find("select * from user")
|
||||
|
||||
SqlPara sqlPara = Db.getSqlPara("user.list");
|
||||
|
||||
|
||||
List<User> users = User.dao.find(sqlPara);
|
||||
|
||||
renderJson(users);
|
||||
}
|
||||
}
|
103
src/main/java/com/lxyer/controller/IController.java
Normal file
103
src/main/java/com/lxyer/controller/IController.java
Normal file
@@ -0,0 +1,103 @@
|
||||
package com.lxyer.controller;
|
||||
|
||||
import com.jfinal.core.Controller;
|
||||
import com.jfinal.kit.Kv;
|
||||
import com.jfinal.plugin.activerecord.*;
|
||||
import com.lxyer.config.E;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by Lxyer at 2017/9/10 13:55.
|
||||
*/
|
||||
public class IController extends Controller {
|
||||
|
||||
public Kv getParams(String... key) {
|
||||
Kv kv = Kv.create();
|
||||
for (String k : key) {
|
||||
if (k.contains("=")) { //如果没有值使用默认值
|
||||
kv.put(k.split("=")[0], getPara(k.split("=")[0], k.split("=")[1]));
|
||||
continue;
|
||||
} else if (k.contains("<")) { //强制使用"<"右侧的值
|
||||
kv.put(k.split("<")[0], k.split("<")[1]);
|
||||
continue;
|
||||
}
|
||||
kv.put(k, getPara(k));
|
||||
}
|
||||
return kv;
|
||||
}
|
||||
|
||||
public int getPn(){
|
||||
return getParaToInt("pn", 1);
|
||||
}
|
||||
public int getPs(){
|
||||
return getParaToInt("ps", 15);
|
||||
}
|
||||
public int getPn(int pn){
|
||||
return getParaToInt("pn", pn);
|
||||
}
|
||||
public int getPs(int ps){
|
||||
return getParaToInt("ps", ps);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置动态属性
|
||||
* @param page
|
||||
* @param dynAttr
|
||||
*/
|
||||
public void setDynAttr(Page<Model> page, E.DynamicAttr dynAttr, String ... s){
|
||||
if (page == null || page.getList().size() == 0 || dynAttr == null) return;
|
||||
List ids = new ArrayList<>();
|
||||
page.getList().forEach(x->ids.add(x.getInt(dynAttr.id_k())));
|
||||
if (!ids.isEmpty()) {
|
||||
String ids_ = ids.toString();
|
||||
ids_ = ids_.substring(1, ids_.length() - 1);
|
||||
|
||||
//查询动态属性
|
||||
Kv kv = Kv.by("table", dynAttr.table()).set("id_k", dynAttr.gk()).set("id_v", ids_).set("cate", dynAttr.getCate());
|
||||
if (s.length > 0)
|
||||
kv.set("attr", arrToStr(s));
|
||||
|
||||
SqlPara sqlPara = Db.getSqlPara("m.dyn_attr", kv);
|
||||
List<Record> attrs = Db.find(sqlPara);
|
||||
|
||||
Map<Integer, Kv> attrMap = new HashMap();
|
||||
attrs.forEach(x->{
|
||||
Kv nAttr = attrMap.getOrDefault(x.getInt(dynAttr.gk()), Kv.create());
|
||||
nAttr.set(x.get("attr"), x.get("value"));
|
||||
attrMap.put(x.getInt(dynAttr.gk()), nAttr);
|
||||
});
|
||||
page.getList().forEach(x->{
|
||||
attrMap.getOrDefault(x.getInt(dynAttr.id_k()), Kv.create()).forEach((k, v)->x.put(k+"", v));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置动态属性
|
||||
* @param model
|
||||
* @param dynAttr
|
||||
*/
|
||||
public void setDynAttr(Model model, E.DynamicAttr dynAttr, String ... ss){
|
||||
Kv kv = Kv.by("table", dynAttr.table()).set("id_k", dynAttr.gk()).set("id_v", model.getInt(dynAttr.id_k())).set("cate", dynAttr.getCate());
|
||||
if (ss.length > 0) kv.set("attr", arrToStr(ss));
|
||||
|
||||
SqlPara sqlPara = Db.getSqlPara("m.dyn_attr", kv);
|
||||
List<Record> attrs = Db.find(sqlPara);
|
||||
attrs.forEach(x-> model.put(x.get("attr"), x.get("value")));
|
||||
}
|
||||
|
||||
private String arrToStr(String ... ss){
|
||||
String str = "";
|
||||
for (String x : ss){
|
||||
str += "'"+x+"',";
|
||||
}
|
||||
if (str.length() > 0)
|
||||
str = str.substring(0, str.length()-1);
|
||||
return str;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user