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

        现在的问题是: 我只想写一条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());
          }
      }
  }
}

数据库设计
问答雕虫3
运行结果
问答雕虫3
数据库表数据
问答雕虫3

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

相关文章:

  • 2022-01-18
  • 2021-10-02
  • 2022-12-23
  • 2021-11-11
  • 2021-12-24
  • 2021-08-14
猜你喜欢
  • 2021-09-15
  • 2021-11-12
  • 2022-12-23
  • 2021-12-01
  • 2021-10-27
  • 2021-07-01
  • 2021-12-02
相关资源
相似解决方案