问答雕虫3

问题:
有一张表 a 字段如下
id content sid。
sid 是 id 的外键,content 是内容。
当要存储比较长的内容时,先将字符串按照特定的长度切割,然后在进行sql存储,
类似链表,例如需要存储字符串str,先将str拆成字符串 a,b,然后将a,b进行
存储 如下图所示,只需记录a的id就可以,
图片说明
当需要str字符串时,根据记录的id查到a的信息,然后判断sid是否为空或者是否等于自身
的id,不等于就取 sid 接着查,最后拼接成最终的字符串。
在这里插入图片描述

        现在的问题是: 我只想写一条sql进行查询拼接

解答:

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;

import com.fly.demo.common.ServiceException;
import com.fly.demo.core.BaseDAO;
import com.fly.demo.entity.LongText;
import com.fly.demo.service.impl.LongTextServiceImpl;

/**
* 
* Main
* 
* @author 00fly
* @version [版本号, 2018-09-29]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class Main
{
  /**
   * Main
   * 
   * @param args
   * @throws ServiceException
   * @throws SQLException
   * @see [类、类#方法、类#成员]
   */
  public static void main(String[] args)
      throws ServiceException, SQLException
  {
      String input = RandomStringUtils.randomAlphanumeric(100);
      int length = 20;
      System.out.println(input);
      System.out.println(length);
      String temp = input;

      if (input.length() > length)
      {
          // 计算分几组
          int arrLength = (input.length() - 1) / length + 1;
          String[] textArr = new String[arrLength];
          int index = 0;
          // 计算每组的字符串
          do
          {
              textArr[index] = input.substring(0, length);
              input = input.substring(length);
              index++;
          } while (input.length() > length);
          textArr[index] = input;

          // 打印分组数据
          for (int i = 0; i < arrLength; i++)
          {
              System.out.println(i + "---> " + textArr[i]);
          }

          // 插入数据库
          String id = "";
          LongText text;
          String parentId = "";
          String lastSid = "";
          for (int i = 0; i < arrLength; i++)
          {
              text = new LongText();
              if (lastSid.isEmpty())
              {
                  text.setId(UUID.randomUUID().toString());
                  id = text.getId();
              }
              else
              {
                  text.setId(lastSid);
              }
              if (parentId.isEmpty())
              {
                  parentId = text.getId() + "_";
              }
              // 左补齐0为3位,限定分组长度不能超过1000
              text.setIdPath(parentId + StringUtils.leftPad(String.valueOf(i), 3, "0"));
              text.setContent(textArr[i]);
              text.setSid(UUID.randomUUID().toString());
              lastSid = text.getSid();
              LongTextServiceImpl.getInstance().insert(text);
          }

          // 打印id
          System.out.println("id: " + id + " ---> " + temp);

          // SQL
          String sql = "select content from long_text where id_path like ? order by id_path";
          List<LongText> list = new BaseDAO().query(LongText.class, sql, new Object[] {id + "%"});
          for (LongText it : list)
          {
              System.out.print(it.getContent());
          }
      }
  }
}

数据库设计
数据库结构
运行结果
运行结果
数据库表数据
数据库表

完整的代码见 https://gitee.com/00fly/effict-side/tree/master/questions_701419

猜你喜欢

转载自blog.csdn.net/qq_16127313/article/details/82924516
今日推荐