惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
U
Unit 42
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
B
Blog RSS Feed
Vercel News
Vercel News
F
Fortinet All Blogs
Know Your Adversary
Know Your Adversary
T
Troy Hunt's Blog
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Jina AI
Jina AI
小众软件
小众软件
T
Threatpost
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
Scott Helme
Scott Helme
B
Blog
腾讯CDC
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
S
Schneier on Security
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
K
Kaspersky official blog
G
Google Developers Blog
T
Tor Project blog
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
罗磊的独立博客

博客园 - _万古如长夜

解析文件内容,匹配请求路径 WEB项目引入druid监控配置 java log4j 代码中 新增按日保存日志文件的功能 按钮居中固定 博文阅读密码验证 - 博客园 tomcat-设置jdk-设置标题-设置内存-配置项目路径 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 qrtz表初始化脚本_mysql 工具类 从jar包里复制要打生产的补丁(仅限T2) CPU使用过高问题/死锁 tomcat jvm 内存配置 文件下载-文件后缀 匹配content-type wsdl调用 不生成java文件方式 nginx 设置超时 常用的正则表达式 DB2各种命令 mysql 各种命令
工具类-老版hibernate 占位符模式(?) in 的问题解决方案
_万古如长夜 · 2022-06-10 · via 博客园 - _万古如长夜

问题

  老版本的hibernate。在不允许 占位符、命名参数 公用模式下。

  以下sql情况,in 的模式是不生效的

select * from a where a in (?); 
//以上sql。传入list或者固定分隔符的字符串。实际测试均不生效
List params = new ArrayList(); 
params.add("aaa,bbb,ccc");

解决方案
  1、升级hibernate版本,使得 ? : 公用
  2、通过程序重新组装sql及参数。把  in(?) 变为 in(?,?,?,?) 参数并行做扩展

 以下程序即 方案2

package com.bytter.core.base;

import com.bytter.core.utils.EmptyUtils;
import com.bytter.core.utils.JsonUtils;

import java.util.*;

/**
 * @author zhouyy
 * @version 1.0
 * @description: TODO
 * @date 2022/6/10 18:05
 */
public class CommonUtils {
    /**
     * @description: 处理 ? 占位符的 in问题。 自动根据参数的list或者,分隔来 拼接 ?
     * @author zhouyy
     * @date: 2022/6/10 15:48
     */
    private static String excuteInSql(String sql, List params) {
        StringBuffer newSql = new StringBuffer();
        List newParams = new ArrayList();
        if(EmptyUtils.isNotEmpty(params)){
            String oldSql = sql;
            String[] sqlArray = oldSql.split("\\?");
            //key=下标 value=?,?,?
            Iterator iterator = params.iterator();
            int paramIndex = 0;
            while(iterator.hasNext()){
                Object obj = iterator.next();
                newSql.append(sqlArray[paramIndex]).append("?");
                //此参数是一个list
                System.out.println((obj != null && obj.toString().indexOf(",") > 0));
          //兼容 list、固定分隔符的字符串
if((obj instanceof Collection<?>) || (obj != null && obj.toString().indexOf(",") > 0)){ List list = (obj instanceof Collection<?>) ? (List)obj : Arrays.asList(obj.toString().replaceAll("'","").split(",")); int lengths = list.size(); //取当前参数所对应的sql的下标 int _sqlIndex = 0; for (int j = 0; j < sqlArray.length; j++) { //当前参数的下标 if(j == paramIndex){ for (int k = 0; k < lengths; k++) { if(k <lengths-1){ newSql.append(",?"); } //参数拆分并加入到参数list中 // params.add(paramIndex+k+1,list.get(k)); newParams.add(list.get(k)); } break; } } }else{ newParams.add(obj); } paramIndex ++; } newSql.append(sqlArray[sqlArray.length-1]); }else{ newSql.append(sql); } System.out.println(newSql.toString()); //對象轉json System.out.println(JsonUtils.toJSONString(newParams)); return newSql.toString(); } /** * @description: * @author zhouyy * @date: 2022/6/10 16:49 */ public static void main(String[] args) { // 首in 中? 中in 尾in // 首? 中in 尾in // String sql = "select * from aa where d=? "; // String sql = "select * from aa where d in (?) "; // String sql = "select * from aa where d in (?) and a=? "; // String sql = "select * from aa where d in (?) and a=? and c in (?) "; String sql = "select * from aa where a=? and c in (?) "; List params = new ArrayList(); params.add("1"); // params.add("011,012,013,014"); params.add("21,22,23,24,25"); // params.add("3"); excuteInSql(sql,params); } }