SQL模板引擎:SqlTemplate

jopen 11年前

遇到过动态拼接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 = sqlTemplateEngin.getSqlTemplate(sqlTpl) ;        Bindings bind = new Bindings().bind("email", "wenzuojing@gmail.com");        SqlMeta sqlMeta = sqlTemplate.process(bind) ; //可传map对象或javabean对象        //System.out.println(sqlMeta.getSql());        Assert.assertEquals("select * from user_info  WHERE  email = ?   ", sqlMeta.getSql());      List<Object> parameter = sqlMeta.getParameter(); //取出参数      Assert.assertEquals(1, parameter.size());            bind.bind("username", "wenzuojing") ;      sqlMeta = sqlTemplate.process(bind) ;      Assert.assertEquals("select * from user_info  WHERE  username = ?   and  email = ?   ", sqlMeta.getSql());      List<Object> parameter2 = sqlMeta.getParameter();//取出参数      Assert.assertEquals( 2  ,parameter2.size() );

例2:动态更新 

SqlTemplateEngin sqlTemplateEngin = new SqlTemplateEngin();        Map<String ,Object > userInfo = new HashMap<String,Object>() ;        userInfo.put("id", "123456") ;    userInfo.put("email", "wenzuojing@126.com") ;        String sqlTpl =" update userinfo <set> <if test ='email != null '> email = #{email} </if>, <if test='age'> age = #{age} </if> </set> where id = #{id}" ;        SqlTemplate sqlTemplate = sqlTemplateEngin.getSqlTemplate(sqlTpl) ;        SqlMeta sqlMeta = sqlTemplate.process(userInfo) ;        Assert.assertEquals(" update userinfo  SET email = ?    where id = ? ", sqlMeta.getSql());        List<Object> parameter = sqlMeta.getParameter(); //取出参数    Assert.assertEquals(2, parameter.size());