zoukankan      html  css  js  c++  java
  • 简易Id切分工具类

    public class SqlIdSplit {
        
        public SqlIdSplit(int minId,int maxId,int size){
            this.maxId=maxId+1;
            this.size=size;
            this.ind=minId;
        }
    
        private int maxId;//最大id
        private int size;//每次数量
        private int ind;//当前id
        private boolean noFirst=false;//是否是第一笔
        
        /** 是否有值
         */
        public boolean next(){
            return next(0);
        }
        /** 是否有值,sleep 睡眠多少毫秒
         */
        public boolean next(int sleep){
            boolean result = ind<maxId;
            if(sleep!=0 && result){
                if(noFirst)
                    delay(sleep);
                noFirst=true;
            }
            ind+=size;
            return result;
        }
        public int getStart(){
            return ind-size;
        }
        public int getEnd(){
            return (ind>maxId?maxId:ind)-1;
        }
        /** 返回 idName>_ and idName<=_
         */
        public String getWhere(String idName){
            String result = idName+">="+(ind-size)+" and "+idName+"<="+getEnd();
            return result;
        }
        
        // TODO以下方法,可放置到工具类中
        /** 使线程delay(毫秒)
         */
        public static void delay(int times) {
            try {
                Thread.sleep(times);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
            }
        }
    }
    View Code

      使用示例如下

    public class Test{
        public static void main(String[] args) {
            //select max(Id) from tbl_userinfo,使用sql查询出最大id
            int maxId = 5000;//例如查出5000
            SqlIdSplit sp = new SqlIdSplit(1, maxId,2000);
            while(sp.next(100)){
                //执行sql,因为是案例,所以只是放置一条语句
                String sql = "select * from tbl_userInfo where "+sp.getWhere("id");
                System.out.println(sql);
            }
        }
    }
    View Code

    运行结果

    select * from tbl_userInfo where id>=1 and id<=2000

    select * from tbl_userInfo where id>=2001 and id<=4000

    select * from tbl_userInfo where id>=4001 and id<=5000

       上面的这个工具类主要用于id分段查询时使用。

    public class SqlListSplit {
        private List<? extends Object> ids_list;//id集合
        private int pageInd=1;//当前下标
        private String qh;//需要添加的内容,例如单引号
        private int size;//每次执行数量
        private int len;//总数量
        private String result;//组装好的数据
        
        public SqlListSplit(List<? extends Object> ids_list, String qh, int size){
            this.ids_list=ids_list;
            this.qh=qh;
            this.size=size;
            len=ids_list.size();
        }
        public SqlListSplit(Set<? extends Object> ids_set, String qh, int size){
            this.ids_list=new ArrayList<Object>(ids_set);;
            this.qh=qh;
            this.size=size;
            len=ids_set.size();
        }
        /**
         * 用长度是否为0判断有无数据
         * @param isNoSpecial 是否是字符
         * @param waitTime    休眠时间,小于0则不休眠
         * @return
         */
        private boolean next(boolean isNoSpecial,int waitTime){
            if(waitTime>0 && pageInd>1)
                delay(waitTime);
            int start = (pageInd-1)*size;
            int end = start+size;
            if(start>=len)
                return false;
            if(end>len)
                end=len;
            if(isNoSpecial)
                this.result = getIdsByList_noSpecial(start,end);
            else
                this.result = getIdsByList(start,end);
            pageInd++;
            return true;
        }
        /** 用长度是否为0判断有无数据
         */
        public boolean next(boolean isNoSpecial){
            return next(isNoSpecial,-1);
        }
        /** 用长度是否为0判断有无数据(每次间隔等待时间(毫秒))
         */
        public boolean next(int waitTime){
            return next(false,waitTime);
        }
        /** 用长度是否为0判断有无数据
         */
        public boolean next(){
            return next(false,-1);
        }
        /** 用长度是否为0判断有无数据 
         *  防止有特殊符号的方法(数字Id没必要用该方法)
         */
        public boolean next_noSpecial(){
            return next(true);
        }
        
        /**获取当前组装好的ids
         */
        public String getStrIds(){
            return result;
        }
        /**
         * @param start 开始数量
         * @param end    结束数量
         */
        private String getIdsByList(int start,int end){
            StringBuilder sb = new StringBuilder();
            if(qh==null){
                for(;start<end; start++){
                    if(sb.length()>0)
                        sb.append(",");
                    sb.append(String.valueOf(ids_list.get(start)));
                }
            }else{
                for(;start<end; start++){
                    if(sb.length()>0)
                        sb.append(",");
                    sb.append(qh).append(String.valueOf(ids_list.get(start))).append(qh);
                }
            }
            return sb.toString();
        }
        /**
         * 防止有特殊符号的方法(可根据需要添加过滤符号)
         * @param start 开始数量
         * @param end    结束数量
         */
        private String getIdsByList_noSpecial(int start,int end){
            StringBuilder sb = new StringBuilder();
            if(qh==null){
                for(;start<end; start++){
                    if(sb.length()>0)
                        sb.append(",");
                    sb.append(String.valueOf(ids_list.get(start)).replace("'", ""));
                }
            }else{
                for(;start<end; start++){
                    if(sb.length()>0)
                        sb.append(",");
                    sb.append(qh).append(String.valueOf(ids_list.get(start)).replace("'", "")).append(qh);
                }
            }
            return sb.toString();
        }
        /**重置下标
         */
        public void reSet(){
            this.pageInd=1;
        }
        
        
        // TODO以下方法,可放置到工具类中
        /** 使线程delay(毫秒)
         */
        public static void delay(int times) {
            try {
                Thread.sleep(times);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
            }
        }
    }
    View Code

      使用示例如下

    public class Test{
        public static void main(String[] args) {
            //例如names中存放了1000个用户名,需要根据用户名去查询用户信息
            Set<String> names = new HashSet<String>();
            names.add("我'");names.add("来");names.add("了");
            SqlListSplit sp = new SqlListSplit(names, "'", 200);
            while (sp.next_noSpecial()) {
                //执行sql,因为是案例,所以只是放置一条语句
                String sql = "select * from tbl_userInfo where userName in("+sp.getStrIds()+")";
                System.out.println(sql);//运行结果,select * from tbl_userInfo where userName in('我','了','来')
            }
            
            //用户名集合中因为存在特殊字符,如果用这个方法,返回的组装数据就会有问题
            sp = new SqlListSplit(names, "'", 200);
            while (sp.next(10)) {
                //执行sql,因为是案例,所以只是放置一条语句
                String sql = "select * from tbl_userInfo where userName in("+sp.getStrIds()+")";
                System.out.println(sql);//运行结果,select * from tbl_userInfo where userName in('我'','了','来')
            }
        }
    }
    View Code

      注:上面的这个工具类主要用于集合数据分段查询。

  • 相关阅读:
    信息收集
    beef框架使用
    网站程序CMS识别
    查找网站后台
    下载漏洞原理及利用
    后台拿webshell方法(2)
    如何学好web安全
    TIME_WAIT过多的解决方法(转)
    sudo
    keepalived+lvs子网掩码造成VIP切换故障 + vrrp_script+track_script
  • 原文地址:https://www.cnblogs.com/bl123/p/13722500.html
Copyright © 2011-2022 走看看