SQL 模板神器

时光斑驳了记忆 2021-05-06 17:40:23 ⋅ 1512 阅读

SqlTemplate - MyBatis

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

  • if

  • choose (when, otherwise)

  • trim , where, set

  • foreach

 

一、快速开始

 


@Test
public void testIf() {

Configuration configuration = new Configuration();

SqlTemplate template = configuration.getTemplate("select *, ${col} from user where <if test='id != null ' > id = #{id} </if>");

HashMap<String, Object> map = new HashMap<String, Object>();

map.put("col", "name");
map.put("id", "11");
map.put("b", "b");

SqlMeta meta = template.process(map);

System.out.println(meta);

// 如果你集成了 spring jdbc 你可以这样使用
// List list = jdbcTemplate.queryForList(meta.getSql(), meta.getParameter());

}

 

二、示例

 

2.1 where 条件

 


@Test
public void testWhere() {

Configuration configuration = new Configuration();

SqlTemplate template = configuration.getTemplate("select * from user <where> <if test='id != null ' > and id = #{id} </if> <if test=' name != null' >name =#{name}</if> </where>");

HashMap<String, Object> map = new HashMap<String, Object>();

map.put("name", "1fffdsfdf1");

SqlMeta process = template.process(map);

System.out.println(process);
}

 

2.2 for 循环

 


@Test
public void testForEach() {

Configuration configuration = new Configuration();

SqlTemplate template = configuration.getTemplate("select * from user <where> id in <foreach item=\"item\" index=\"index\" collection=\"list\" open=\"(\" separator=\",\" close=\")\"> ${item} ${index} </foreach></where>");

HashMap<String, Object> map = new HashMap<String, Object>();

HashMap<String, Object> map2 = new HashMap<String, Object>();

map2.put("11", "11-11");
map2.put("22", "22-22");

map.put("list", map2);

SqlMeta process = template.process(map);

System.out.println(process);

}

 

2.3 update

 


@Test
public void testUpdate() {
Configuration configuration = new Configuration(false, Charset.defaultCharset());

SqlTemplate template = configuration
.getTemplate("update sys_task\n" +
" <set >\n" +
" <if test=\"updatedAt != null\" >\n" +
" updated_at = #{updatedAt},\n" +
" </if>\n" +
" <if test=\"taskStatus != null\" >\n" +
" task_status = #{taskStatus},\n" +
" </if>\n" +
" <if test=\"taskMsg != null\" >\n" +
" task_msg = #{taskMsg},\n" +
" </if>\n" +
" </set>\n" +
" where id = #{id}");

HashMap<String, Object> map = new HashMap<String, Object>();

map.put("id", 123);
map.put("taskStatus", "1");

SqlMeta process = template.process(map);

System.out.println(process);
}

 

2.4 insert

 

   @Test
public void testInsert() {
Configuration configuration = new Configuration(false, Charset.defaultCharset());

SqlTemplate template = configuration
.getTemplate("insert into sys_mail\n" +
" <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\" >\n" +
" <if test=\"mailSenderId != null\" >\n" +
" mail_sender_id,\n" +
" </if>\n" +
" <if test=\"mailSenderName != null\" >\n" +
" mail_sender_username,\n" +
" </if>\n" +
" <if test=\"mailSenderHost != null\" >\n" +
" mail_sender_host,\n" +
" </if>\n" +
" <if test=\"mailSenderUsername != null\" >\n" +
" mail_sender_username,\n" +
" </if>\n" +
" <if test=\"mailSenderPassword != null\" >\n" +
" mail_sender_password,\n" +
" </if>\n" +
" <if test=\"mailSenderFrom != null\" >\n" +
" mail_sender_from,\n" +
" </if>\n" +
" </trim>\n" +
" <trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\" >\n" +
" <if test=\"mailSenderId != null\" >\n" +
" #{mailSenderId},\n" +
" </if>\n" +
" <if test=\"mailSenderName != null\" >\n" +
" #{mailSenderName},\n" +
" </if>\n" +
" <if test=\"mailSenderHost != null\" >\n" +
" #{mailSenderHost},\n" +
" </if>\n" +
" <if test=\"mailSenderUsername != null\" >\n" +
" #{mailSenderUsername},\n" +
" </if>\n" +
" <if test=\"mailSenderPassword != null\" >\n" +
" #{mailSenderPassword},\n" +
" </if>\n" +
" <if test=\"mailSenderFrom != null\" >\n" +
" #{mailSenderFrom},\n" +
" </if>\n" +
" </trim>");

HashMap<String, Object> map = new HashMap<String, Object>();

map.put("mailSenderId", 123);
map.put("mailSenderFrom", "zhangsan");

SqlMeta process = template.process(map);

System.out.println(process);
}

 

三、鸣谢

  • 部分代码摘自 mybatis

Mybatis 语法 请参考MyBatis

四、源码地址

https://code.aliyun.com/qiaoshiju/sqltemplate-mybatis.git


全部评论: 0

    我有话说:

    挖一挖那些让公司网站瘫痪的SQL“终结者”

    一条慢查询会造成什么后果?之前我一直觉得不就是返回数据会慢一些么,用户体验变差? 其实远远不止,我经历过几次线上事故,有一次就是由一条 SQL 慢查询导致的。 那次是一条 SQL 查询耗时达到 2

    【hammerspoon】被埋没的超强自动化

    hammerspoon这种都没人用?有点奇怪

    微信开发全能微信Java开发工具包

    必须分享的微信 weixin-java-tools

    「轻阅读」聊一聊6种常用的架构设计模式(上)

      许多现代应用都需要在企业级规模上进行构建,有时甚至需要在互联网规模上进行构建。这些应用都需要满足可扩展性、可用性、安全性、可靠性和弹性需求。 在本文中,我将谈论一些设计模式,这些模式

    GeoGebra 6.0.606.0 发布,绘图

    Geogebra 是动态数学软件,它将几何、代数、电子表格、绘图、统计和微积分集成在一个易于使用的软件包中。 GeoGebra 6.0.606.0 版本现已发布,具体更新内容如下: 符号输入框:为 Serif 添加选项 符号输入框:asin(x...

    验证码实在太过反人类?自动跳过验证码的

    原文:https://3w.huanqiu.com/a/7224b9/40hvQr4vA3J 目前网络上越来越多使用验证码了,验证码的本意是阻止机器刷流量挤占服务器资源,这本来无可厚非;但是验证码已经变得越来越过分,别说机器人了,连人也经常没法辨...

    Node实战篇:Express--jade模板引擎(七)

    Jade(Pug) — Node Template Engine,一个高性能的模板引擎,专为 Node 而做......

    强大的 Lambda 式转 Sql 类库 - SqlSugar 隐藏功能之 Lambda

    使用场景 1、Lambda to sql 一直是ORM中最难的功能之一,如果有现成的解析库那么自已写一个ORM难度将大大降低 2、通过Lambda作为KEY进行缓存操作,特别是仓储模式想要拿到表达式

    Node模块之Events模块(五)

    Node模块之Events模块(五)

    Beetl 3.2.4.RELEASE,Java 模板引擎 Beetl

    Beetl是一款全功能,高性能优秀的国产模板引擎,可以广泛用于动态页面生成,静态页面生成,代码生成,文本转换,脚本语言和规则引擎等,从2011年来,一直维护,并得到国内公司用户的肯定。 Beetl

    Node模块(四)

    模块化分工、各司其职

    8 种最坑的 SQL 错误用法,你有没有踩过坑?

    编写复杂SQL语句要养成使用 WITH 语句的习惯。简洁且思路清晰的SQL语句也能减小数据库的负担 。

    ObjectiveSQL 1.3.6 版本发布,过程化 SQL 编程&等价表达式

    ObjectiveSQL 第一个正式版本提供了自动生成代码的特性,替代了简单SQL 的编程的编码工作,紧接着推出复杂SQL的解决方案。 ObjectiveSQL 通过修改Java 编译器,实现了

    程序猿的周末|全网影视任你“盘”,1个磁力搜索+2个满速下载工具,

    程序猿珍藏版,全网电影一网打尽——周末不孤单...

    「小米技术」Soar一键优化工具--SQL优化和改写的自动化工具

    SOAR(SQL Optimizer And Rewriter)是一个对SQL进行优化和改写的自动化工具。由小米人工智能与云平台的数据库团队开发与维护。

    Druid 1.2.4 版本发布,增强 SQL Parser,支持 JDK8 日期类型

    Druid 1.2.4 版本现已发布,这个是一个小的 bug 修复版本,修复了一系列 SQL Parser 的问题,增强对 JDK 8 的支持。 Issues SQL Parser 增强对

    「开源资讯」JPress v3.2.1 发布,新增模板预览功能

    JPress 是一个使用 Java 开发的类似 WordPress 的产品,具有完善的模板和插件功能,并在此基础上新增了在线商城、会员中心以及和微信深度整合的功能。