Giter VIP home page Giter VIP logo

sqltemplate's Introduction

SqlTemplate

SqlTemplate 是sql模板引擎,主要解决动态拼接sql字符串 。原理是比较简单,把模板内容构建成完成的xml,这样可以解析成相关的数据结构, 再结合Ognl强大表达式计算条件。设计上参考了MyBatis动态sql部分,为了减少学习成本,兼容Mybatis大部分用法。目前能支持以下标签:

  • if
  • choose (when, otherwise)
  • trim , where, set
  • foreach

详细用法,请参考MyBatis

例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", "[email protected]");
	
	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", "[email protected]") ;
	
	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());

sqltemplate's People

Contributors

wenzuojing avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.