zoukankan      html  css  js  c++  java
  • Mybatis按顺序获取数据

    sql语句select * from producttg where hospitalcode in (1,2,3)  获取到的数据并不是按照条件1,2,3的顺序排列,如果要成下面形式(mybatis语句)

    select * 
    from producttg 
    where productno in ${productnolist}
    order by CHARINDEX(','+ltrim(productno)+',',',${productnostr}')
    

      其中输入为productnolist(String类型),productnostr(String类型)

    productnolist的格式为:(1,2,3)
    productnostr的格式为:1,2,3,
    

      java中将List转成上述两种字符串格式的代码为:

            //拼接成:('00401','01001','00301','02001') 
    	public static String ListToString1(List<String> inputlist){
    		String result="('";
    		for (int i= 0; i<inputlist.size();i++) {
    			if(i==inputlist.size()-1){
    				result=result+inputlist.get(i)+"')";
    			}else{
    				result=result+inputlist.get(i)+"','";
    			}
    		}
    		return result;
    	}     
    

      

            //拼接成:00401,01001,00301,02001,
    	public static String ListToString2(List<String> inputlist){
    		String result ="";
    		for (String item : inputlist) {
    			result =result+item+",";
    		}
    		return result;
    	}    
    

      这样就完成了,按照productno in 多个条件的顺序依次查出的需求,注意:mybatis语句中取输入数据用的是${ },而不是#{ }

      1.#{ }可以防止注入,${ }不能防止注入

           2.#{ }传入占位符,${ }传入字符串

      3.order by需要使用${ }

  • 相关阅读:
    关于在函数中返回动态的内存
    C与C++中的const
    strcat函数的坑点
    面试题30.最小的k个数
    面试题29.数组中出现次数超过一半的数字
    面试题28.字符串的排列
    面试题27.二叉搜索树与双向链表
    C++中构造函数初始化成员列表总结
    Oracle merge into
    检索 COM 类工厂中 CLSID 解决办法
  • 原文地址:https://www.cnblogs.com/winv758241/p/7837388.html
Copyright © 2011-2022 走看看