zoukankan      html  css  js  c++  java
  • 好用的代码示例

    1、在某个字段后面追加固定的一段数据

       mysql公式:update table_name set field=CONCAT('str',field) where .....

                    示例:update app_contract_ info set apply_type=CONCAT('123456',apply_type) where cont_code ='HFFQ'

       oracle公式: update table_name set field=field||'str' where .....

                    示例:update app_contract_ info set apply_type=apply_type||'123456' where cont_code ='HFFQ'

     

    linq使用代码示例:

    IEnumerable<Tuple3<Map<String, Object>, Map<String, Object>, Map<String, Object>>> join = Linq.asEnumerable(loanList).leftJoin(
            Linq.asEnumerable(payCardList),
            x -> Convert.toString(x.get("loanNo")),
            y -> Convert.toString(y.get("loanNo")),
            Tuple::create)
            .leftJoin(Linq.asEnumerable(resultList),
                    x -> Convert.toString(x.getItem1().get("loanNo")),
                    y -> Convert.toString(y.get("loanNo")),
                    (x, y) -> Tuple.create(x.getItem1(), x.getItem2(), y));
    
    for(Tuple3<Map<String, Object>, Map<String, Object>, Map<String, Object>> tuple : join) {
        Map<String, Object> map1=tuple .getItem1();
        Map<String, Object> map2=tuple .getItem2();
        Map<String, Object> map3=tuple .getItem3();
    }
    

     类型相互转换工具类:https://github.com/Pinshuducha/utils/tree/master/Convert.java

    连接处理

    Map<String, Object> adConfigByModelIdAndSizeType = this.getAdConfigByModelIdAndSizeType(params);
            if (!HttpUtil.isSuccess(adConfigByModelIdAndSizeType)) {
                return adConfigByModelIdAndSizeType;
            }
            Map<String, Object> body = FormatUtil.getChildMap(adConfigByModelIdAndSizeType, "body");
            List<Map<String, Object>> modelList = HCMapUtils.getChildList(body, "modelList");
            if (HCCollectionUtils.isEmpty(modelList)) {
                return fail("99", "没有返回参数列表");
            }
            Map<String, Object> result = new HashMap<>(2);
            // 将父级元素获取到
            IEnumerable<Map<String, Object>> father = Linq.asEnumerable(modelList).where(item -> "1".equals(item.get("homeLevel")));
            // 获取子级元素获取到
            IEnumerable<IGrouping<String, Map<String, Object>>> child = Linq.asEnumerable(modelList)
                    .where(item -> "2".equals(item.get("homeLevel")))
                    .groupBy(x -> Convert.toString(x.get("fatherId")));
            IEnumerable<Tuple2<Map<String, Object>, IGrouping<String, Map<String, Object>>>> joinResult = father.leftJoin(child,
                    x -> Convert.toString(x.get("id")),
                    y -> Convert.toString(y.getKey()),
                    Tuple::create);
            List<Map<String, Object>> childItem = joinResult.select(entry -> {
                Map<String, Object> left = entry.getItem1();
                left.put("child", entry.getItem2().toList());
                return left;
            }).toList();
            result.put("modelList", childItem);
            logger.info("最终结果:" + FormatUtil.toJson(result));
            return success(result);
    

    关于数据库表结构主键的操作

    可以新增单主键或联合主键;

    alter table tablename add constraint pk_tablename primary key (column1,column2,...);

    新增主键前要检查预建主键列是否有重复数据:

    select column1 from tablenamed group by column1 having count(column1)>1;

    linq 源码

    https://github.com/timandy/linq

    java压缩文件(一般压缩图片)

     注:此方法的入参File oldFile, File newFile 都是可以new File(path)

    public static void compressFile(File oldFile, File newFile, Double scale, Long maxSize, Integer widthMax, Integer heightMax) {
    	Assert.notNull(oldFile, "oldFile can not be null");
    	Assert.notNull(newFile, "oldFile can not be null");
    	logger.info(String.format("压缩照片==> srcPath:%s, descPath:%s", oldFile.getAbsolutePath(), newFile.getAbsolutePath()));
    	long startTime = System.currentTimeMillis();
    	Long compressDefaultSize = Convert.defaultLong(CommonProperties.get("file.compressDefaultSize"), 2 * 1024 * 1024);//2M
    	Double compressDefaultScale = Convert.defaultDouble(CommonProperties.get("file.compressDefaultScale"), 0.5);
    	maxSize = Convert.defaultLong(maxSize, compressDefaultSize);
    	scale = Convert.defaultDouble(scale, compressDefaultScale);
    	logger.info(String.format("原照片大小:%s, 压缩标准:%s 单位:byte(B)", oldFile.length(), maxSize));
    	widthMax = Convert.defaultInteger(widthMax, 4096);
    	heightMax = Convert.defaultInteger(heightMax, 4096);
    
    	Image srcFile = null;
    	int width;
    	int height;
    	try {
    		srcFile = ImageIO.read(oldFile);
    		width = srcFile.getWidth(null);
    		height = srcFile.getHeight(null);
    		logger.info(String.format("原照片%s, height:%s", width, height));
    	} catch (IOException e) {
    		logger.error(ThrowableUtils.getString(e));
    		throw new BusinessException(RestUtil.ERROR_INTERNAL_CODE, "压缩文件失败");
    	}
    
    	try {
    		int count = 0;
    		while (oldFile.length() > maxSize || width> widthMax || height> heightMax) {
    			logger.info("ompressCount = " + (++count));
    			int widthNew = (int) (width * scale); // 源图宽度
    			int heightNew = (int) (height * scale); // 源图高度
    
    			String subfix = StringUtils.getFilenameExtension(oldFile.getPath());
    			BufferedImage buffImg;
    			if (subfix.equals("png")) {
    				buffImg = new BufferedImage(widthNew, heightNew, BufferedImage.TYPE_INT_ARGB);
    			} else {
    				buffImg = new BufferedImage(widthNew, heightNew, BufferedImage.TYPE_INT_RGB);
    			}
    			Graphics2D graphics = buffImg.createGraphics();
    			graphics.setBackground(new Color(255, 255, 255));
    			graphics.setColor(new Color(255, 255, 255));
    			graphics.fillRect(0, 0, widthNew, heightNew);
    			graphics.drawImage(srcFile.getScaledInstance(widthNew, heightNew, Image.SCALE_SMOOTH), 0, 0, null);
    			graphics.dispose();
    			ImageIO.write(buffImg, subfix, newFile);
    			oldFile = newFile;
    			srcFile = ImageIO.read(oldFile);
    			width = srcFile.getWidth(null);
    			height = srcFile.getHeight(null);
    		}
    
    	} catch (Exception e) {
    		logger.error(ThrowableUtils.getString(e));
    		throw new BusinessException(RestUtil.ERROR_INTERNAL_CODE, "压缩文件失败");
    	}
    	Long timeSpan = System.currentTimeMillis() - startTime;
    	logger.debug(String.format("ompressTook:%sms", timeSpan));
    	logger.info(String.format("照片压缩后大小:%s 单位:byte(B)", newFile.length()));
    	logger.info(String.format("照片压缩后%s, height:%s", width, height));
    	return;
    }
    

     参数拼接

    按照某种规则(自定义优先级)排序

    public void sortLoanList(List<Map<String, Object>> list) {
    
            Map<String, Integer> hm = new HashMap<>();
            hm.put("1", 1);
            hm.put("2", 2);
            hm.put("3", 3);
            hm.put("01", 4);
            hm.put("02", 5);
            hm.put("03", 6);
            hm.put("04", 7);
            hm.put("05", 8);
            hm.put("06", 9);
            hm.put("20", 10);
            hm.put("22", 11);
            hm.put("23", 12);
            hm.put("24", 13);
            hm.put("AA", 14);
            hm.put("OD", 15);
            hm.put("WS", 16);
            hm.put("30", 17);
            hm.put("31", 18);
            hm.put("92", 19);
            hm.put("93", 20);
    
            if (list != null) {
                list.sort((Map<String, Object> s1, Map<String, Object> s2) -> {
                    String outSts1 = Convert.defaultString(s1.get(ServiceConstant.OUTSTS));
                    String outSts2 = Convert.defaultString(s2.get(ServiceConstant.OUTSTS));
                    if (hm.containsKey(outSts1) && hm.containsKey(outSts2)) {
                        return hm.get(outSts2) - hm.get(outSts1);
                    } else {
                        return 1;
                    }
                });
            }
        }
    

    修改表下字段的长度

     alter table 表名 modify (字段名 varchar2(长度 char));

    遍历时删其中的元素

     分段更新数据库表的字段示例

    begin
      declare
        cursor c_update is
         select rowid row_id
            from 表名 where 字段名 is null;
        v_counter number := 0;
      begin
        for one_row in c_update loop
          v_counter := v_counter + 1;
          update 表名 SET 字段名='01' where rowid = one_row.row_id;
          if (v_counter = 50000) then
            v_counter := 0;
            commit;
          end if;
        end loop;
        commit;
      end;
    end;
    /
    

      

      

  • 相关阅读:
    非常奇怪的VS2005无法调试的问题
    自已写的“动易PowerEasy2006暴库工具”
    用VBA去除Excel工作表保护密码
    mapgis同arcmap之间的数据转换, 投影变换,误差校正,坐标,基础资料,教程的相关信息的汇集
    HDU You Are the One (dp)
    HDU4291 A Short problem
    Mondriaan's Dream(poj2411)
    HDU 4277 USACO ORZ
    求欧拉回路的路径(usaco3.3Riding the Fences)
    poj2923 (状态压缩01背包)
  • 原文地址:https://www.cnblogs.com/lu51211314/p/10637775.html
Copyright © 2011-2022 走看看