• 将长内容分割,用双主键进行存储


    有时候需要保存的内容很长很长,数据库的一个字段无法全部存完,我们就可以使用双主键,将内容分割后再进行存储

     

    //存储内容的主要方法
    //Entity为假设的,使用双主键的实体,其主键实体为EntityPK
    //content为需要进行存储的内容
    private List<Entity> genEntityList(Entity entity,String content) {
      List<Entity > entity= new ArrayList<Entity >();
      Map<String, Object> query=new HashMap<String, Object>();
      // 将字符串按4000的长度进行分割
      int maxLen = 1300;
      // 需要分割的次数
      int twices = content.length() / maxLen;
      twices++;
      //getEntityId方法用于获取id(插入方法中就不要配置生成id了)
      //语句如下:SELECT SEQ_ENTITY.NEXTVAL FROM DUAL
      List idlist=entityService.queryList("getEntityId", null);
      String id=idlist.get(0).toString();
      for (int i = 0; i < twices; i++) {
        int beginIdx = i * maxLen;
        int endIdx = (i + 1) * maxLen;
        String EntCon = content.substring(beginIdx, endIdx > content.length() ? content.length() : endIdx);
        Entity bEntity  = newEntity ();
        EntityPK pk = newEntityPK ();
        pk.setId(id);
        pk.setSeqnum(i + "");
        bEntity .setPk(pk);
        bEntity .setEntCon(EntCon );
        entity.add(bCfgMessageIn);
      }
    
      return entity;
    }
    

     

        //在方法中进行调用
        //相关表中用一个字段关联一下Entity的Id
        public void setContent() {
      ……
      //do something   List<Entity> list=genEntityList(entity, content);   entityService.insertInBatch(list); }

     

     

    //dao中的insertInBatch方法
    @Override
    public int insertInBatch(List<T> entityList) {
      if (entityList == null || entityList.isEmpty())
      return 0;
      int i=0;
      for (T entity : entityList) {
        i+=this.insert(entity);
       }
      return i;
    }
  • 相关阅读:
    POJ-3176 Cow Bowling
    01背包、完全背包、多重背包
    最后的几天暑假学习
    暑假的学习
    凸包算法(Graham扫描法)详解
    微软版的SqlHelper.cs类
    SQL语句分组排序,多表关联排序
    SQL存储过程分页(通用的拼接SQL语句思路实现)
    增加删除字段修改字段名,修改表结构,非常用SQL语句技巧总结
    Asp.net mvc返回Xml结果,扩展Controller实现XmlResult以返回XML格式数据
  • 原文地址:https://www.cnblogs.com/IceBlueBrother/p/8421701.html
Copyright © 2020-2023  润新知