话 不 多 说 ,直 接 上 石马 ~
需求来源:https://jfinal.com/feedback/8021
先自定义一个指令:EvalDirective
public class EvalDirective extends Directive { public void exec(Env env, Scope scope, Writer writer) { new Parser(env, new StringBuilder(// exprList.eval(scope).toString()),null)// .parse().exec(env, scope, writer); } }
再测试:
public static void main(String[] args) { DruidPlugin dp = YunjiaowuConfig.getDruidPlugin(); ActiveRecordPlugin arp = new ActiveRecordPlugin(dp); Engine engine = arp.getEngine(); //注册指令 engine.addDirective("eval", EvalDirective.class); dp.start(); arp.start(); try { String str = "select id from account where id = #para(id)"; String sql = "select (#eval(str)) myId from account where id = #para(id2)"; SqlKit sqlKit = Db.use().getConfig().getSqlKit(); SqlPara sqlPara = sqlKit.getSqlParaByString(sql, // Kv.by("id", 1).set("id2", 2).set("str", str)); System.out.println(sqlPara.toString()); } finally { dp.stop(); } }
执行结果:
这个指令不光可以拼接SQL时参数当模板代码执行,做其他业务也可以用~
有参考价值就点个赞吧~