sql拼接

SqlTemplate ——sql模板引擎

こ雲淡風輕ζ 提交于 2019-11-27 10:12:41
遇到过动态拼接sql字符的同学,一定深知其中痛苦,我本人也其中受害者之一。SqlTemplate 是通过模板方法解决动态sql, 把模板内容构建成完成的xml,这样可以解析成相关的数据结构, 再结合Ognl强大表达式计算条件。 设计上参考了MyBatis动态sql部分,为了减少学习成本,兼容Mybatis大部分用法。目前能支持以下标签: if choose (when, otherwise) trim , where, set foreach 详细用法,请参考 MyBatis 源码地址: https://github.com/wenzuojing/SqlTemplate 非常欢迎大家提建议和指正 例1:动态查询 SqlTemplateEngin sqlTemplateEngin = new SqlTemplateEngin(); String sqlTpl = "select * from user_info <where><if test=' username != null' > and username = #{username} </if><if test=' email != null' > and email = #{email} </if></where> "; //从字符串读取sql模板内容,还可以从单独的文件读取 SqlTemplate sqlTemplate =