公司中Java使用Freemarker动态脚本查询Sql的坑

本文记录了在公司中使用Java结合Freemarker动态生成SQL时遇到的问题,包括:<#if specedParam>条件不可省略,因为模板会参考此条件;SQL语句不应包含Limit限制,可能引发错误;同时提到了<#t>标签用于合并空格的细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里只是记录一下,并不是通用的

1.<#if specedParam><#else>不可省略,由于会参考模板,

#--查询10条免费的数字教材-->
<#switch switchName>
    <#case "QUERY_FREE">
                <#if specedParam>
                    这里写请求参数
                <#else>
                    <#--查询10条免费的数字教材-->
                    SELECT <#t>
                    jcwx_ebooks.id, <#t>
                    jcwx_ebooks.url, <#t>
                    jcwx_ebooks.cover, <#t>
                    jcwx_ebooks.title, <#t>
                    jcwx_ebooks.catalog, <#t>
                    jcwx_ebooks.price, <#t>
                    jcwx_ebooks.introduction, <#t>
                    jcwx_ebooks.likes, <#t>
                    jcwx_ebooks.isfree, <#t>
                    jcwx_ebooks.isbn, <#t>
                    jcwx_ebooks.type, <#t>
                    jcwx_ebooks.word_count, <#t>
                    jcwx_ebooks.author, <#t>
                    jcwx_ebooks.press, <#t>
                    jcwx_ebooks.addtime, <#t>
                    jcwx_ebooks.isdel, <#t>
                    jcwx_ebooks.download_count, <#t>
                    jcwx_ebooks.views_count <#t>
                    FROM <#t>
                    jcwx_ebooks <#t>
                    WHERE <#t>
                    1=1 <#t>
                    AND <#t>
                    jcwx_ebooks.isfree = 0 <#t>
                    ORDER BY <#t>
                    jcwx_ebooks.addtime DESC <#t>
                </#if>
    <#default>

2.Sql语句中不可以使用Limit条件。

3.其它说明

1).<#t>代表合并空格
SELECT <#t>
jcwx_ebooks.id

等同于

SELECT jcwx_ebooks.id
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尼诺和尼可

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值