1 Star 0 Fork 1

FanX / SqlTemplate

forked from sky灬xiaoxi / SqlTemplate 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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", "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());
MIT License Copyright (c) 2023 sky灬xiaoxi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

从GitHub镜像过来的项目。 该项目主要:可以通过自己传递 sql 代码进行对数据库的操作。支持mybatis语法和sql语法 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/shinestmt/SqlTemplate.git
git@gitee.com:shinestmt/SqlTemplate.git
shinestmt
SqlTemplate
SqlTemplate
master

搜索帮助