So Jpql Template Engine - 复杂多条件SQL语句模板引擎

介绍了一个基于Velocity和XML的SQL模板引擎,简化复杂多条件SQL编写。通过在Spring Boot项目中配置XML文件,利用标签和属性定义SQL语句,结合velocity模板语法,实现动态SQL的生成。在代码中,通过so-jpql-template-engine框架解析SQL模板,与ORM框架或JDBC配合执行,避免手动拼接SQL的繁琐。项目已开源,未来计划发布到Maven中央仓库。

🚀 复杂多条件SQL语句模板引擎

velocity+xml解析的sql模板引擎,让复杂多条件的sql编写更简单、更快速

🐼 使用案例

  1. 在spring boot项目的resource目录下新建一个jpql文件夹,并在jpql文件夹下新建一个xml文件用来写sql语句。比如,这里我新建一个TestJpql.xml,目录结构如下:
...
--> resource
   --> jpql
       --> TestJpql.xml
...
  1. TestJpql.xml中编写sql语句。举个例子:
<?xml version="1.0" encoding="UTF-8" ?>
<jpa module="testJpql">
    <jpql id="customSQL">
        select * from so_tag where 1=1
        #if($title)
        and title like :title
        #end
        #if($readCount)
        and read_count &gt; :readCount
        #end
        #if($tagNames)
        and tag_name in (:tagNames)
        #end
        limit 10
    </jpql>
    <jpql id="xxx">
        ...
    </jpql>
    ...
// DeviceInfo.java (已有部分保持不变,新增关联关系) /* * Copyright 2019-2025 Zheng Jie * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package me.zhengjie.gen.domain; import lombok.Data; import cn.hutool.core.bean.BeanUtil; import io.swagger.annotations.ApiModelProperty; import cn.hutool.core.bean.copier.CopyOptions; import javax.persistence.*; import javax.validation.constraints.NotBlank; import java.sql.Timestamp; import java.io.Serializable; /** * @website https://eladmin.vip * @description / * @author chen jiayuan * @date 2025-09-16 **/ @Entity @Data @Table(name="device_info") public class DeviceInfo implements Serializable { // 设备状态常量 public static final Integer STATUS_OFFLINE = 0; // 下线 public static final Integer STATUS_ONLINE = 1; // 上线 @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @ApiModelProperty(value = "ID") private Integer id; @Column(name = "country") @NotBlank @ApiModelProperty(value = "国家码,例如:JP") private String country; @Column(name = "model", unique = true) @NotBlank @ApiModelProperty(value = "设备型号,例如:ER7206") private String model; @Column(name = "model_version") @ApiModelProperty(value = "设备版本,例如:1.0") private String modelVersion; @Column(name = "model_template_id") @ApiModelProperty(value = "设备模板ID") private Integer modelTemplateId; @Column(name = "image_info_id") @ApiModelProperty(value = "镜像信息ID") private Integer imageInfoId; @Column(name = "status") @ApiModelProperty(value = "状态:0-下线,1-上线") private Integer status = 1; // 默认上线 @Column(name = "created_at") @ApiModelProperty(value = "创建时间") private Timestamp createdAt; @Column(name = "updated_at") @ApiModelProperty(value = "更新时间") private Timestamp updatedAt; public void copy(DeviceInfo source){ BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true)); } }
10-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值