无极低码SQL模板引擎使用教程示例,自己手撸一个sql模板引擎进行动态sql生成。
无极低码SQL模板使用教程
一、模板结构与规则
无极低码SQL模板通过简洁的Markdown格式,使SQL语句具有更强的灵活性和适应性,简化了根据业务需求定制SQL的过程。
无极低码SQL模板是一种基于Markdown格式的特殊SQL语句表示方式,它利用∮
符号作为动态条件的标识符,使得SQL语句能够在运行时根据变量值动态生成。
下面是无极低码SQL模板的一般结构:
select方法名,用于定位执行语句
===分隔符
sql语句,为查询输出前端的值,where 1=1 结束 也可以直接跟参数
∮bypositioncode为动态参数
与下一个sql之间使用“;”分割
∮byIn_id表示非空时拼接如下sql
and id in('#byIn_id#')“#byIn_id#”会被替换为实际值
动态拼接时间查询条件
∮bystartpubTime&&byendpubTime
and pub_time between '#bystartpubTime#' and '#byendpubTime#'
动态拼接模糊查询
∮byname
and name like'%#byname#%'
二、动态条件的应用
- 对于每个
∮
开头的条件行,如果关联的变量存在且非空,那么该行条件将被包含在最终执行的SQL查询中。 - 如果变量不存在或为空,则对应的SQL条件将被忽略。
三、示例sql模板
select
===
select positioncode ,id,positionname,picurl ,pichref ,pictitle ,picdes ,sort from travelpositionpics where 1=1
∮bypositioncode
and positioncode=#bypositioncode#
∮bypositionname
and positionname='#bypositionname#'
∮bypicurl
and picurl='#bypicurl#'
∮bypichref
and pichref='#bypichref#'
∮bypictitle
and pictitle='#bypictitle#'
∮bypicdes
and picdes='#bypicdes#'
∮bysort
and sort=#bysort#
;
selectOneByid
===
select positioncode ,picurl ,pichref ,pictitle ,picdes ,sort from travelpositionpics where 1=1
∮bypositioncode
and positioncode=#bypositioncode#
∮bypicurl
and picurl='#bypicurl#'
∮bypichref
and pichref='#bypichref#'
∮bypictitle
and pictitle='#bypictitle#'
∮bypicdes
and picdes='#bypicdes#'
∮bysort
and sort=#bysort#
;
delete
===
delete from travelpositionpics where 1=1
∮bypositioncode
and positioncode=#bypositioncode#
∮bypicurl
and picurl='#bypicurl#'
∮bypichref
and pichref='#bypichref#'
∮bypictitle
and pictitle='#bypictitle#'
∮bypicdes
and picdes='#bypicdes#'
∮bysort
and sort=#bysort#
∮byIn_id
and id in('#byIn_id#')
;
insert
===
insert ignore into travelpositionpics(positioncode,positionname,picurl,pichref,pictitle,picdes,sort)
VALUES(#positioncode#,'#positionname#','∮#picurl#','#pichref#','#pictitle#','#picdes#',#sort#);
update
===
update travelpositionpics set
∮positioncode
positioncode=#positioncode#,
∮positionname
positionname='#positionname#',
∮picurl
picurl='#picurl#',
∮pichref
pichref='#pichref#',
∮pictitle
pictitle='#pictitle#',
∮picdes
picdes='#picdes#',
∮sort
sort=#sort#
where 1=1
∮bypositioncode
and positioncode='#bypositioncode#'
∮bypicurl
and picurl='#bypicurl#'
∮bypichref
and pichref='#bypichref#'
∮bypictitle
and pictitle='#bypictitle#'
∮bypicdes
and picdes='#bypicdes#'
∮byid
and id=#byid#
∮bysort
and sort='#bysort#'