找回密码
 立即注册
首页 业界区 业界 伙伴匹配系统(移动端 H5 网站(APP 风格)基于Spring Bo ...

伙伴匹配系统(移动端 H5 网站(APP 风格)基于Spring Boot 后端 + Vue3 - 03

染悄 2025-9-28 16:44:15
伙伴匹配系统(移动端 H5 网站(APP 风格)基于Spring Boot 后端 + Vue3 - 03

1.gif

项目地址:

  • Github:https://github.com/China-Rainbow-sea/yupao
  • Gitee:https://gitee.com/Rainbow--Sea/yupao
开发用户修改页面:后端


开发用户登录功能:后端

to do

3.png

更新 脱敏,bean(注意没有映射,以及逻辑删除注解),以及 xml
前端:抽象通用列表组件

4.png

批量导入数据 | 多种方案介绍对比

尽量不要一次性,导入太多数据,而是一点一点的加量导入数据。比如:10,100,1000,10000
导入数据


  • 用可视化界面:适合一次性导入,数据量可控。
  • 写程序: for 循环,建议分批,不要一把梭哈(可以用接口来控制),要保证可控,幂等,注意:线上环境和测试环境是有区别的
编写一次性任务:

for 循环插入数据的问题:

  • 建立和释放数据连接:
  • for 循环是绝对线性的:(并发)
补充:Spring 启动定时任务,@EnableScheduling // 启动定时任务
  1. @EnableScheduling // 启动定时任务
复制代码
  1. package com.rainbowsea.yupao;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.scheduling.annotation.EnableScheduling;
  6. @SpringBootApplication
  7. @MapperScan("com.rainbowsea.yupao.mapper")
  8. @EnableScheduling // 启动定时任务
  9. public class YuPaoApplication {
  10.     public static void main(String[] args) {
  11. <template>
  12.   <user-card-list :user-list="userList"/>
  13.   
  14.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  15. </template>  SpringApplication.run(YuPaoApplication.class, args);
  16.     }
  17. }
复制代码
并发编程(多线程池)
并发要注意执行的先后顺序无所谓,不要用到非并发类的集合(eg: 不要用 List 默认是线程不安全的),要转成线程安全的集合。
for 循环插入数据的特点:

  • 频繁建立和释放数据库连接(用批量查询解决)
  • for 循环是绝对线性的(可以并发提速)
使用简单的 for 循环进行一个并发提速插入,批量元素内容。
  1. package com.rainbowsea.yupao.one;
  2. import com.rainbowsea.yupao.mapper.UserMapper;
  3. import com.rainbowsea.yupao.model.User;
  4. import com.rainbowsea.yupao.service.UserService;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import org.springframework.util.StopWatch;
  10. import javax.annotation.Resource;
  11. import java.util.ArrayList;
  12. @Component
  13. @RunWith(SpringRunner.class)
  14. public class InsertUsers {
  15.     @Resource
  16.     private UserService userService;
  17.     /**
  18.      * 批量插入用户
  19.      */
  20.     // @Scheduled(fixedDelay = 5000, fixedRate = Long.MAX_VALUE)
  21.     @Test
  22.     public void doInsertUsers() {
  23. <template>
  24.   <user-card-list :user-list="userList"/>
  25.   
  26.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  27. </template>  StopWatch stopWatch = new StopWatch();  // 监视查看内容信息
  28. <template>
  29.   <user-card-list :user-list="userList"/>
  30.   
  31.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  32. </template>  stopWatch.start();  // 开始时间点
  33. <template>
  34.   <user-card-list :user-list="userList"/>
  35.   
  36.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  37. </template>  ArrayList<User> userList = new ArrayList<>(); // ArrayList<>() 支持并发,不存在多并发问题
  38. <template>
  39.   <user-card-list :user-list="userList"/>
  40.   
  41.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  42. </template>  final int INSERT_NUM = 100000;  // shift + ctrl + u 切换为大写
  43. <template>
  44.   <user-card-list :user-list="userList"/>
  45.   
  46.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  47. </template>  for (int i = 0; i < INSERT_NUM; i++) {
  48. <template>
  49.   <user-card-list :user-list="userList"/>
  50.   
  51.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  52. </template><template>
  53.   <user-card-list :user-list="userList"/>
  54.   
  55.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  56. </template>User user = new User();
  57. <template>
  58.   <user-card-list :user-list="userList"/>
  59.   
  60.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  61. </template><template>
  62.   <user-card-list :user-list="userList"/>
  63.   
  64.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  65. </template>user.setUsername("假鱼皮");
  66. <template>
  67.   <user-card-list :user-list="userList"/>
  68.   
  69.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  70. </template><template>
  71.   <user-card-list :user-list="userList"/>
  72.   
  73.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  74. </template>user.setUserAccount("fakeuyupi");
  75. <template>
  76.   <user-card-list :user-list="userList"/>
  77.   
  78.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  79. </template><template>
  80.   <user-card-list :user-list="userList"/>
  81.   
  82.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  83. </template>user.setAvatarUrl("https://profile-avatar.csdnimg.cn/2f8ef0ed68a647bcad04088152e00c69_weixin_61635597.jpg!1");
  84. <template>
  85.   <user-card-list :user-list="userList"/>
  86.   
  87.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  88. </template><template>
  89.   <user-card-list :user-list="userList"/>
  90.   
  91.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  92. </template>user.setGender(0);
  93. <template>
  94.   <user-card-list :user-list="userList"/>
  95.   
  96.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  97. </template><template>
  98.   <user-card-list :user-list="userList"/>
  99.   
  100.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  101. </template>user.setUserPassword("12345678");
  102. <template>
  103.   <user-card-list :user-list="userList"/>
  104.   
  105.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  106. </template><template>
  107.   <user-card-list :user-list="userList"/>
  108.   
  109.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  110. </template>user.setPhone("123");
  111. <template>
  112.   <user-card-list :user-list="userList"/>
  113.   
  114.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  115. </template><template>
  116.   <user-card-list :user-list="userList"/>
  117.   
  118.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  119. </template>user.setEmail("123@qq.com");
  120. <template>
  121.   <user-card-list :user-list="userList"/>
  122.   
  123.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  124. </template><template>
  125.   <user-card-list :user-list="userList"/>
  126.   
  127.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  128. </template>user.setTags("[]");
  129. <template>
  130.   <user-card-list :user-list="userList"/>
  131.   
  132.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  133. </template><template>
  134.   <user-card-list :user-list="userList"/>
  135.   
  136.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  137. </template>user.setUserStatus(0);
  138. <template>
  139.   <user-card-list :user-list="userList"/>
  140.   
  141.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  142. </template><template>
  143.   <user-card-list :user-list="userList"/>
  144.   
  145.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  146. </template>user.setIsDelete(0);
  147. <template>
  148.   <user-card-list :user-list="userList"/>
  149.   
  150.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  151. </template><template>
  152.   <user-card-list :user-list="userList"/>
  153.   
  154.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  155. </template>user.setPlanetCode("1111111");
  156. <template>
  157.   <user-card-list :user-list="userList"/>
  158.   
  159.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  160. </template><template>
  161.   <user-card-list :user-list="userList"/>
  162.   
  163.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  164. </template>userList.add(user);
  165. <template>
  166.   <user-card-list :user-list="userList"/>
  167.   
  168.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  169. </template>  }
  170. <template>
  171.   <user-card-list :user-list="userList"/>
  172.   
  173.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  174. </template>  userService.saveBatch(userList, 1000);
  175. <template>
  176.   <user-card-list :user-list="userList"/>
  177.   
  178.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  179. </template>  stopWatch.stop(); // 结束时间点
  180. <template>
  181.   <user-card-list :user-list="userList"/>
  182.   
  183.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  184. </template>  System.out.println(stopWatch.getTotalTimeMillis()); // 打印显示提示信息
  185.     }
  186. }
复制代码
使用定时任务,完成并发处理
  1. package com.rainbowsea.yupao.one;
  2. import com.rainbowsea.yupao.mapper.UserMapper;
  3. import com.rainbowsea.yupao.model.User;
  4. import com.rainbowsea.yupao.service.UserService;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import org.springframework.util.StopWatch;
  10. import javax.annotation.Resource;
  11. import java.util.ArrayList;
  12. @Component
  13. @RunWith(SpringRunner.class)
  14. public class InsertUsers {
  15.     @Resource
  16.     private UserService userService;
  17.     /**
  18.      * 批量插入用户
  19.      */
  20.     @Scheduled(fixedDelay = 5000, fixedRate = Long.MAX_VALUE)// 注意主启动类上要加上,@EnableScheduling // 启动定时任务
  21.     // @Test
  22.     public void doInsertUsers() {
  23. <template>
  24.   <user-card-list :user-list="userList"/>
  25.   
  26.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  27. </template>  StopWatch stopWatch = new StopWatch();  // 监视查看内容信息
  28. <template>
  29.   <user-card-list :user-list="userList"/>
  30.   
  31.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  32. </template>  stopWatch.start();  // 开始时间点
  33. <template>
  34.   <user-card-list :user-list="userList"/>
  35.   
  36.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  37. </template>  ArrayList<User> userList = new ArrayList<>();
  38. <template>
  39.   <user-card-list :user-list="userList"/>
  40.   
  41.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  42. </template>  final int INSERT_NUM = 100000;  // shift + ctrl + u 切换为大写
  43. <template>
  44.   <user-card-list :user-list="userList"/>
  45.   
  46.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  47. </template>  for (int i = 0; i < INSERT_NUM; i++) {
  48. <template>
  49.   <user-card-list :user-list="userList"/>
  50.   
  51.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  52. </template><template>
  53.   <user-card-list :user-list="userList"/>
  54.   
  55.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  56. </template>User user = new User();
  57. <template>
  58.   <user-card-list :user-list="userList"/>
  59.   
  60.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  61. </template><template>
  62.   <user-card-list :user-list="userList"/>
  63.   
  64.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  65. </template>user.setUsername("假鱼皮");
  66. <template>
  67.   <user-card-list :user-list="userList"/>
  68.   
  69.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  70. </template><template>
  71.   <user-card-list :user-list="userList"/>
  72.   
  73.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  74. </template>user.setUserAccount("fakeuyupi");
  75. <template>
  76.   <user-card-list :user-list="userList"/>
  77.   
  78.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  79. </template><template>
  80.   <user-card-list :user-list="userList"/>
  81.   
  82.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  83. </template>user.setAvatarUrl("https://profile-avatar.csdnimg.cn/2f8ef0ed68a647bcad04088152e00c69_weixin_61635597.jpg!1");
  84. <template>
  85.   <user-card-list :user-list="userList"/>
  86.   
  87.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  88. </template><template>
  89.   <user-card-list :user-list="userList"/>
  90.   
  91.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  92. </template>user.setGender(0);
  93. <template>
  94.   <user-card-list :user-list="userList"/>
  95.   
  96.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  97. </template><template>
  98.   <user-card-list :user-list="userList"/>
  99.   
  100.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  101. </template>user.setUserPassword("12345678");
  102. <template>
  103.   <user-card-list :user-list="userList"/>
  104.   
  105.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  106. </template><template>
  107.   <user-card-list :user-list="userList"/>
  108.   
  109.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  110. </template>user.setPhone("123");
  111. <template>
  112.   <user-card-list :user-list="userList"/>
  113.   
  114.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  115. </template><template>
  116.   <user-card-list :user-list="userList"/>
  117.   
  118.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  119. </template>user.setEmail("123@qq.com");
  120. <template>
  121.   <user-card-list :user-list="userList"/>
  122.   
  123.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  124. </template><template>
  125.   <user-card-list :user-list="userList"/>
  126.   
  127.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  128. </template>user.setTags("[]");
  129. <template>
  130.   <user-card-list :user-list="userList"/>
  131.   
  132.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  133. </template><template>
  134.   <user-card-list :user-list="userList"/>
  135.   
  136.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  137. </template>user.setUserStatus(0);
  138. <template>
  139.   <user-card-list :user-list="userList"/>
  140.   
  141.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  142. </template><template>
  143.   <user-card-list :user-list="userList"/>
  144.   
  145.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  146. </template>user.setIsDelete(0);
  147. <template>
  148.   <user-card-list :user-list="userList"/>
  149.   
  150.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  151. </template><template>
  152.   <user-card-list :user-list="userList"/>
  153.   
  154.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  155. </template>user.setPlanetCode("1111111");
  156. <template>
  157.   <user-card-list :user-list="userList"/>
  158.   
  159.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  160. </template><template>
  161.   <user-card-list :user-list="userList"/>
  162.   
  163.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  164. </template>userList.add(user);
  165. <template>
  166.   <user-card-list :user-list="userList"/>
  167.   
  168.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  169. </template>  }
  170. <template>
  171.   <user-card-list :user-list="userList"/>
  172.   
  173.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  174. </template>  userService.saveBatch(userList, 1000);
  175. <template>
  176.   <user-card-list :user-list="userList"/>
  177.   
  178.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  179. </template>  stopWatch.stop(); // 结束时间点
  180. <template>
  181.   <user-card-list :user-list="userList"/>
  182.   
  183.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  184. </template>  System.out.println(stopWatch.getTotalTimeMillis()); // 打印显示提示信息
  185.     }
  186. }
复制代码
采用异步执行,并发执行的方式
  1. package com.rainbowsea.yupao.one;
  2. import com.rainbowsea.yupao.mapper.UserMapper;
  3. import com.rainbowsea.yupao.model.User;
  4. import com.rainbowsea.yupao.service.UserService;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import org.springframework.util.StopWatch;
  10. import javax.annotation.Resource;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.concurrent.CompletableFuture;
  14. @Component
  15. @RunWith(SpringRunner.class)
  16. public class InsertUsers {
  17.     @Resource
  18.     private UserService userService;
  19.     /**
  20.      * 批量插入用户
  21.      */
  22.     // @Scheduled(fixedDelay = 5000, fixedRate = Long.MAX_VALUE)
  23.     @Test
  24.     public void doInsertUsers() {
  25. <template>
  26.   <user-card-list :user-list="userList"/>
  27.   
  28.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  29. </template>  StopWatch stopWatch = new StopWatch();  // 监视查看内容信息
  30. <template>
  31.   <user-card-list :user-list="userList"/>
  32.   
  33.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  34. </template>  stopWatch.start();  // 开始时间点
  35. <template>
  36.   <user-card-list :user-list="userList"/>
  37.   
  38.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  39. </template>  final int INSERT_NUM = 100000;  // shift + ctrl + u 切换为大写
  40. <template>
  41.   <user-card-list :user-list="userList"/>
  42.   
  43.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  44. </template>  List<CompletableFuture<Void>> futureList = new ArrayList<>();  // 列表
  45. <template>
  46.   <user-card-list :user-list="userList"/>
  47.   
  48.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  49. </template>  // 分 10 组
  50. <template>
  51.   <user-card-list :user-list="userList"/>
  52.   
  53.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  54. </template>  int j = 0;
  55. <template>
  56.   <user-card-list :user-list="userList"/>
  57.   
  58.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  59. </template>  for (int i = 0; i < INSERT_NUM; i++) {
  60. <template>
  61.   <user-card-list :user-list="userList"/>
  62.   
  63.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  64. </template><template>
  65.   <user-card-list :user-list="userList"/>
  66.   
  67.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  68. </template>List<User> userList = new ArrayList<>();
  69. <template>
  70.   <user-card-list :user-list="userList"/>
  71.   
  72.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  73. </template><template>
  74.   <user-card-list :user-list="userList"/>
  75.   
  76.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  77. </template>while (true) {
  78. <template>
  79.   <user-card-list :user-list="userList"/>
  80.   
  81.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  82. </template><template>
  83.   <user-card-list :user-list="userList"/>
  84.   
  85.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  86. </template>    j++;
  87. <template>
  88.   <user-card-list :user-list="userList"/>
  89.   
  90.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  91. </template><template>
  92.   <user-card-list :user-list="userList"/>
  93.   
  94.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  95. </template>    User user = new User();
  96. <template>
  97.   <user-card-list :user-list="userList"/>
  98.   
  99.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  100. </template><template>
  101.   <user-card-list :user-list="userList"/>
  102.   
  103.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  104. </template>    user.setUsername("假鱼皮");
  105. <template>
  106.   <user-card-list :user-list="userList"/>
  107.   
  108.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  109. </template><template>
  110.   <user-card-list :user-list="userList"/>
  111.   
  112.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  113. </template>    user.setUserAccount("fakeuyupi");
  114. <template>
  115.   <user-card-list :user-list="userList"/>
  116.   
  117.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  118. </template><template>
  119.   <user-card-list :user-list="userList"/>
  120.   
  121.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  122. </template>    user.setAvatarUrl("https://profile-avatar.csdnimg.cn/2f8ef0ed68a647bcad04088152e00c69_weixin_61635597.jpg!1");
  123. <template>
  124.   <user-card-list :user-list="userList"/>
  125.   
  126.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  127. </template><template>
  128.   <user-card-list :user-list="userList"/>
  129.   
  130.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  131. </template>    user.setGender(0);
  132. <template>
  133.   <user-card-list :user-list="userList"/>
  134.   
  135.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  136. </template><template>
  137.   <user-card-list :user-list="userList"/>
  138.   
  139.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  140. </template>    user.setUserPassword("12345678");
  141. <template>
  142.   <user-card-list :user-list="userList"/>
  143.   
  144.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  145. </template><template>
  146.   <user-card-list :user-list="userList"/>
  147.   
  148.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  149. </template>    user.setPhone("123");
  150. <template>
  151.   <user-card-list :user-list="userList"/>
  152.   
  153.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  154. </template><template>
  155.   <user-card-list :user-list="userList"/>
  156.   
  157.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  158. </template>    user.setEmail("123@qq.com");
  159. <template>
  160.   <user-card-list :user-list="userList"/>
  161.   
  162.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  163. </template><template>
  164.   <user-card-list :user-list="userList"/>
  165.   
  166.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  167. </template>    user.setTags("[]");
  168. <template>
  169.   <user-card-list :user-list="userList"/>
  170.   
  171.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  172. </template><template>
  173.   <user-card-list :user-list="userList"/>
  174.   
  175.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  176. </template>    user.setUserStatus(0);
  177. <template>
  178.   <user-card-list :user-list="userList"/>
  179.   
  180.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  181. </template><template>
  182.   <user-card-list :user-list="userList"/>
  183.   
  184.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  185. </template>    user.setIsDelete(0);
  186. <template>
  187.   <user-card-list :user-list="userList"/>
  188.   
  189.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  190. </template><template>
  191.   <user-card-list :user-list="userList"/>
  192.   
  193.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  194. </template>    user.setPlanetCode("1111111");
  195. <template>
  196.   <user-card-list :user-list="userList"/>
  197.   
  198.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  199. </template><template>
  200.   <user-card-list :user-list="userList"/>
  201.   
  202.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  203. </template>    userList.add(user);
  204. <template>
  205.   <user-card-list :user-list="userList"/>
  206.   
  207.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  208. </template><template>
  209.   <user-card-list :user-list="userList"/>
  210.   
  211.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  212. </template>    if(j % 10000 == 0) {
  213. <template>
  214.   <user-card-list :user-list="userList"/>
  215.   
  216.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  217. </template><template>
  218.   <user-card-list :user-list="userList"/>
  219.   
  220.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  221. </template><template>
  222.   <user-card-list :user-list="userList"/>
  223.   
  224.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  225. </template>  break;
  226. <template>
  227.   <user-card-list :user-list="userList"/>
  228.   
  229.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  230. </template><template>
  231.   <user-card-list :user-list="userList"/>
  232.   
  233.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  234. </template>    }
  235. <template>
  236.   <user-card-list :user-list="userList"/>
  237.   
  238.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  239. </template><template>
  240.   <user-card-list :user-list="userList"/>
  241.   
  242.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  243. </template>}
  244. <template>
  245.   <user-card-list :user-list="userList"/>
  246.   
  247.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  248. </template><template>
  249.   <user-card-list :user-list="userList"/>
  250.   
  251.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  252. </template>// 异步执行
  253. <template>
  254.   <user-card-list :user-list="userList"/>
  255.   
  256.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  257. </template><template>
  258.   <user-card-list :user-list="userList"/>
  259.   
  260.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  261. </template>CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
  262. <template>
  263.   <user-card-list :user-list="userList"/>
  264.   
  265.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  266. </template><template>
  267.   <user-card-list :user-list="userList"/>
  268.   
  269.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  270. </template>    System.out.println("threadName:" + Thread.currentThread().getName());
  271. <template>
  272.   <user-card-list :user-list="userList"/>
  273.   
  274.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  275. </template><template>
  276.   <user-card-list :user-list="userList"/>
  277.   
  278.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  279. </template>    userService.saveBatch(userList, 1000);
  280. <template>
  281.   <user-card-list :user-list="userList"/>
  282.   
  283.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  284. </template><template>
  285.   <user-card-list :user-list="userList"/>
  286.   
  287.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  288. </template>});
  289. <template>
  290.   <user-card-list :user-list="userList"/>
  291.   
  292.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  293. </template><template>
  294.   <user-card-list :user-list="userList"/>
  295.   
  296.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  297. </template>futureList.add(future);
  298. <template>
  299.   <user-card-list :user-list="userList"/>
  300.   
  301.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  302. </template>  }
  303. <template>
  304.   <user-card-list :user-list="userList"/>
  305.   
  306.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  307. </template>  CompletableFuture.allOf(futureList.toArray(new CompletableFuture[]{})).join();
  308. <template>
  309.   <user-card-list :user-list="userList"/>
  310.   
  311.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  312. </template>  stopWatch.stop(); // 结束时间点
  313. <template>
  314.   <user-card-list :user-list="userList"/>
  315.   
  316.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  317. </template>  System.out.println(stopWatch.getTotalTimeMillis()); // 打印显示提示信息
  318.     }
  319. }
复制代码
注意:使用并发时要注意数据的插入先后顺序是否无所谓。
并发时不要用到非并发类的集合。
建立执行器(线程池),自己创建线程池,进行一个获取自己创建的线程池,执行并发,获取线程池连接。
  1. private ExecutorService executorService = new ThreadPoolExecutor(16, 1000, 10000, TimeUnit.MINUTES, new ArrayBlockingQueue<>(10000));
复制代码
5.png
  1. package com.rainbowsea.yupao.one;import com.rainbowsea.yupao.mapper.UserMapper;import com.rainbowsea.yupao.model.User;import com.rainbowsea.yupao.service.UserService;import org.apache.tomcat.util.threads.ThreadPoolExecutor;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.stereotype.Component;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.util.StopWatch;import javax.annotation.Resource;import java.util.ArrayList;import java.util.List;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.CompletableFuture;import java.util.concurrent.ExecutorService;import java.util.concurrent.TimeUnit;@Component@RunWith(SpringRunner.class)public class InsertUsers {    @Resource    private UserService userService;    // CPU 密集型,分配的核心线程数 = CPU -1    // IO 密集型,分配的核心线程数可以大于 CPU 核数    private ExecutorService executorService = new ThreadPoolExecutor(16, 1000, 10000, TimeUnit.MINUTES, new ArrayBlockingQueue<>(10000));    /**     * 批量插入用户     */    // @Scheduled(fixedDelay = 5000, fixedRate = Long.MAX_VALUE)    @Test    public void doInsertUsers() {<template>
  2.   <user-card-list :user-list="userList"/>
  3.   
  4.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  5. </template>  StopWatch stopWatch = new StopWatch();  // 监视查看内容信息<template>
  6.   <user-card-list :user-list="userList"/>
  7.   
  8.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  9. </template>  stopWatch.start();  // 开始时间点<template>
  10.   <user-card-list :user-list="userList"/>
  11.   
  12.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  13. </template>  final int INSERT_NUM = 100000;  // shift + ctrl + u 切换为大写<template>
  14.   <user-card-list :user-list="userList"/>
  15.   
  16.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  17. </template>  List futureList = new ArrayList();  // 列表<template>
  18.   <user-card-list :user-list="userList"/>
  19.   
  20.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  21. </template>  // 分 10 组<template>
  22.   <user-card-list :user-list="userList"/>
  23.   
  24.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  25. </template>  int j = 0;<template>
  26.   <user-card-list :user-list="userList"/>
  27.   
  28.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  29. </template>  for (int i = 0; i < INSERT_NUM; i++) {<template>
  30.   <user-card-list :user-list="userList"/>
  31.   
  32.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  33. </template><template>
  34.   <user-card-list :user-list="userList"/>
  35.   
  36.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  37. </template>List userList = new ArrayList();<template>
  38.   <user-card-list :user-list="userList"/>
  39.   
  40.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  41. </template><template>
  42.   <user-card-list :user-list="userList"/>
  43.   
  44.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  45. </template>while (true) {<template>
  46.   <user-card-list :user-list="userList"/>
  47.   
  48.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  49. </template><template>
  50.   <user-card-list :user-list="userList"/>
  51.   
  52.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  53. </template>    j++;<template>
  54.   <user-card-list :user-list="userList"/>
  55.   
  56.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  57. </template><template>
  58.   <user-card-list :user-list="userList"/>
  59.   
  60.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  61. </template>    User user = new User();<template>
  62.   <user-card-list :user-list="userList"/>
  63.   
  64.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  65. </template><template>
  66.   <user-card-list :user-list="userList"/>
  67.   
  68.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  69. </template>    user.setUsername("假鱼皮");<template>
  70.   <user-card-list :user-list="userList"/>
  71.   
  72.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  73. </template><template>
  74.   <user-card-list :user-list="userList"/>
  75.   
  76.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  77. </template>    user.setUserAccount("fakeuyupi");<template>
  78.   <user-card-list :user-list="userList"/>
  79.   
  80.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  81. </template><template>
  82.   <user-card-list :user-list="userList"/>
  83.   
  84.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  85. </template>    user.setAvatarUrl("https://profile-avatar.csdnimg.cn/2f8ef0ed68a647bcad04088152e00c69_weixin_61635597.jpg!1");<template>
  86.   <user-card-list :user-list="userList"/>
  87.   
  88.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  89. </template><template>
  90.   <user-card-list :user-list="userList"/>
  91.   
  92.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  93. </template>    user.setGender(0);<template>
  94.   <user-card-list :user-list="userList"/>
  95.   
  96.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  97. </template><template>
  98.   <user-card-list :user-list="userList"/>
  99.   
  100.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  101. </template>    user.setUserPassword("12345678");<template>
  102.   <user-card-list :user-list="userList"/>
  103.   
  104.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  105. </template><template>
  106.   <user-card-list :user-list="userList"/>
  107.   
  108.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  109. </template>    user.setPhone("123");<template>
  110.   <user-card-list :user-list="userList"/>
  111.   
  112.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  113. </template><template>
  114.   <user-card-list :user-list="userList"/>
  115.   
  116.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  117. </template>    user.setEmail("123@qq.com");<template>
  118.   <user-card-list :user-list="userList"/>
  119.   
  120.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  121. </template><template>
  122.   <user-card-list :user-list="userList"/>
  123.   
  124.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  125. </template>    user.setTags("[]");<template>
  126.   <user-card-list :user-list="userList"/>
  127.   
  128.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  129. </template><template>
  130.   <user-card-list :user-list="userList"/>
  131.   
  132.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  133. </template>    user.setUserStatus(0);<template>
  134.   <user-card-list :user-list="userList"/>
  135.   
  136.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  137. </template><template>
  138.   <user-card-list :user-list="userList"/>
  139.   
  140.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  141. </template>    user.setIsDelete(0);<template>
  142.   <user-card-list :user-list="userList"/>
  143.   
  144.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  145. </template><template>
  146.   <user-card-list :user-list="userList"/>
  147.   
  148.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  149. </template>    user.setPlanetCode("1111111");<template>
  150.   <user-card-list :user-list="userList"/>
  151.   
  152.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  153. </template><template>
  154.   <user-card-list :user-list="userList"/>
  155.   
  156.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  157. </template>    userList.add(user);<template>
  158.   <user-card-list :user-list="userList"/>
  159.   
  160.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  161. </template><template>
  162.   <user-card-list :user-list="userList"/>
  163.   
  164.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  165. </template>    if(j % 10000 == 0) {<template>
  166.   <user-card-list :user-list="userList"/>
  167.   
  168.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  169. </template><template>
  170.   <user-card-list :user-list="userList"/>
  171.   
  172.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  173. </template><template>
  174.   <user-card-list :user-list="userList"/>
  175.   
  176.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  177. </template>  break;<template>
  178.   <user-card-list :user-list="userList"/>
  179.   
  180.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  181. </template><template>
  182.   <user-card-list :user-list="userList"/>
  183.   
  184.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  185. </template>    }<template>
  186.   <user-card-list :user-list="userList"/>
  187.   
  188.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  189. </template><template>
  190.   <user-card-list :user-list="userList"/>
  191.   
  192.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  193. </template>}<template>
  194.   <user-card-list :user-list="userList"/>
  195.   
  196.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  197. </template><template>
  198.   <user-card-list :user-list="userList"/>
  199.   
  200.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  201. </template>// 异步执行<template>
  202.   <user-card-list :user-list="userList"/>
  203.   
  204.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  205. </template><template>
  206.   <user-card-list :user-list="userList"/>
  207.   
  208.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  209. </template>CompletableFuture future = CompletableFuture.runAsync(() -> {<template>
  210.   <user-card-list :user-list="userList"/>
  211.   
  212.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  213. </template><template>
  214.   <user-card-list :user-list="userList"/>
  215.   
  216.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  217. </template>    System.out.println("threadName:" + Thread.currentThread().getName());<template>
  218.   <user-card-list :user-list="userList"/>
  219.   
  220.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  221. </template><template>
  222.   <user-card-list :user-list="userList"/>
  223.   
  224.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  225. </template>    userService.saveBatch(userList, 1000);<template>
  226.   <user-card-list :user-list="userList"/>
  227.   
  228.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  229. </template><template>
  230.   <user-card-list :user-list="userList"/>
  231.   
  232.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  233. </template>},executorService);<template>
  234.   <user-card-list :user-list="userList"/>
  235.   
  236.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  237. </template><template>
  238.   <user-card-list :user-list="userList"/>
  239.   
  240.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  241. </template>futureList.add(future);<template>
  242.   <user-card-list :user-list="userList"/>
  243.   
  244.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  245. </template>  }<template>
  246.   <user-card-list :user-list="userList"/>
  247.   
  248.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  249. </template>  CompletableFuture.allOf(futureList.toArray(new CompletableFuture[]{})).join();<template>
  250.   <user-card-list :user-list="userList"/>
  251.   
  252.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  253. </template>  stopWatch.stop(); // 结束时间点<template>
  254.   <user-card-list :user-list="userList"/>
  255.   
  256.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  257. </template>  System.out.println(stopWatch.getTotalTimeMillis()); // 打印显示提示信息    }}
复制代码
连接池参数设置:
  1. // CPU 密集型:分配的核心线程数 = CPU - 1
  2. // IO 密集型:分配的核心线程数可以大于 CPU 核数
复制代码
页面加载太慢


  • 优化:数据库慢,用户不登录,用户查看的数据是一样的,既然是一样的,我们就不需要重复查多次了,那我们就可以预先把数据查出来,放到一个更快读取的地方,不用再查数据库了。缓存。为了防止第一个用户,查询的时候,没有走缓存——>预加载缓存,定时更新缓存。(可以采用定时任务)
  • 多个机器都要执行任务呢?(分布式锁:控制同一时间只有一台机器去执行定时任务,其他机器不用重复执行了)
  • 比较慢的地方,可以想想可不可以复用,减少慢的次数。
多个数据,前端显示,进行分页处理,如果一下子将 10W 个数据信息给前端进行渲染,前端是无法渲染处理的,是会直接报错处理的,所以这里进行一个 MyBatis-Plus 的一个分页插件进行处理,运行观察执行的 SQL 日志,并不是直接一次性将 10W / 所有数据查询出来,而是通过一个分页查询,查询其中所需的一部分,发送给前端,提高查询效率,同时避免前端接收过多数据,渲染加载数据失败。
  1.   /**
  2.      * 首页 recommend 推荐显示内容,同时进行了分页处理
  3.      *
  4.      * @param request
  5.      * @return
  6.      */
  7.     @GetMapping("/recommend")
  8.     public BaseResponse<Page<User>> recommendUsers(long pageSize, long pageNum,HttpServletRequest request) {
  9. <template>
  10.   <user-card-list :user-list="userList"/>
  11.   
  12.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  13. </template>  QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  14. <template>
  15.   <user-card-list :user-list="userList"/>
  16.   
  17.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  18. </template>  Page<User> userList = userService.page(new Page<>(pageNum, pageSize), queryWrapper);
  19. <template>
  20.   <user-card-list :user-list="userList"/>
  21.   
  22.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  23. </template>  //List<User> userList = userService.list(queryWrapper);
  24. <template>
  25.   <user-card-list :user-list="userList"/>
  26.   
  27.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  28. </template>  //List<User> list = userList.stream().
  29. <template>
  30.   <user-card-list :user-list="userList"/>
  31.   
  32.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  33. </template>  //<template>
  34.   <user-card-list :user-list="userList"/>
  35.   
  36.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  37. </template>  map(user -> userService.getSafetyUser(user)).
  38. <template>
  39.   <user-card-list :user-list="userList"/>
  40.   
  41.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  42. </template>  //<template>
  43.   <user-card-list :user-list="userList"/>
  44.   
  45.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  46. </template>  collect(Collectors.toList());
  47. <template>
  48.   <user-card-list :user-list="userList"/>
  49.   
  50.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  51. </template>  return ResultUtils.success(userList);
  52.     }
复制代码
同时需要在 在 Spring Boot 项目中,你可以通过 Java 配置来添加分页插件:
6.png
  1. @Configuration
  2. @MapperScan("scan.your.mapper.package")
  3. public class MybatisPlusConfig {
  4.     /**
  5.      * 添加分页插件
  6.      */
  7.     @Bean
  8.     public MybatisPlusInterceptor mybatisPlusInterceptor() {
  9. <template>
  10.   <user-card-list :user-list="userList"/>
  11.   
  12.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  13. </template>  MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  14. <template>
  15.   <user-card-list :user-list="userList"/>
  16.   
  17.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  18. </template>  interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
  19. <template>
  20.   <user-card-list :user-list="userList"/>
  21.   
  22.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  23. </template>  // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
  24. <template>
  25.   <user-card-list :user-list="userList"/>
  26.   
  27.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  28. </template>  return interceptor;
  29.     }
  30. }
复制代码
7.png

前端添加上相关,分页携带的数据信息:
8.png
  1. <template>
  2.   <user-card-list :user-list="userList"/>
  3.   
  4.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  5. </template>
复制代码
运行观察执行的 SQL 日志,并不是直接一次性将 10W / 所有数据查询出来,而是通过一个分页查询,查询其中所需的一部分,发送给前端,提高查询效率,同时避免前端接收过多数据,渲染加载数据失败。

  • 首先,模拟插入 50W 条数据,测试。这里我们创建一个用于测试的数据库名为 "usercentertest"
  1. create table tag(    id<template>
  2.   <user-card-list :user-list="userList"/>
  3.   
  4.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  5. </template>   bigint auto_increment comment 'id'<template>
  6.   <user-card-list :user-list="userList"/>
  7.   
  8.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  9. </template>  primary key,    tagName    varchar(256)<template>
  10.   <user-card-list :user-list="userList"/>
  11.   
  12.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  13. </template><template>
  14.   <user-card-list :user-list="userList"/>
  15.   
  16.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  17. </template><template>
  18.   <user-card-list :user-list="userList"/>
  19.   
  20.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  21. </template>     null comment '标签名称',    userId     bigint<template>
  22.   <user-card-list :user-list="userList"/>
  23.   
  24.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  25. </template><template>
  26.   <user-card-list :user-list="userList"/>
  27.   
  28.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  29. </template><template>
  30.   <user-card-list :user-list="userList"/>
  31.   
  32.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  33. </template><template>
  34.   <user-card-list :user-list="userList"/>
  35.   
  36.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  37. </template>     null comment '用户id',    parenId    bigint<template>
  38.   <user-card-list :user-list="userList"/>
  39.   
  40.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  41. </template><template>
  42.   <user-card-list :user-list="userList"/>
  43.   
  44.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  45. </template><template>
  46.   <user-card-list :user-list="userList"/>
  47.   
  48.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  49. </template><template>
  50.   <user-card-list :user-list="userList"/>
  51.   
  52.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  53. </template>     null comment '父标签 id',    isParent   tinyint<template>
  54.   <user-card-list :user-list="userList"/>
  55.   
  56.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  57. </template><template>
  58.   <user-card-list :user-list="userList"/>
  59.   
  60.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  61. </template><template>
  62.   <user-card-list :user-list="userList"/>
  63.   
  64.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  65. </template><template>
  66.   <user-card-list :user-list="userList"/>
  67.   
  68.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  69. </template>    null comment '0 - 不是, 1- 父标签',    createTime datetime default CURRENT_TIMESTAMP null comment '创建时间',    updateTime datetime default CURRENT_TIMESTAMP null comment '更新时间',    isDelete   tinyint  default 0<template>
  70.   <user-card-list :user-list="userList"/>
  71.   
  72.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  73. </template><template>
  74.   <user-card-list :user-list="userList"/>
  75.   
  76.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  77. </template>     not null comment '是否删除 0 1(逻辑删除)',    constraint uniIdx_tagName<template>
  78.   <user-card-list :user-list="userList"/>
  79.   
  80.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  81. </template>  unique (tagName))    comment '标签';create index idx_userId    on tag (userId);create table user(    username     varchar(256)<template>
  82.   <user-card-list :user-list="userList"/>
  83.   
  84.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  85. </template><template>
  86.   <user-card-list :user-list="userList"/>
  87.   
  88.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  89. </template><template>
  90.   <user-card-list :user-list="userList"/>
  91.   
  92.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  93. </template>     null comment '用户昵称',    id<template>
  94.   <user-card-list :user-list="userList"/>
  95.   
  96.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  97. </template>     bigint auto_increment comment 'id'<template>
  98.   <user-card-list :user-list="userList"/>
  99.   
  100.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  101. </template>  primary key,    userAccount  varchar(256)<template>
  102.   <user-card-list :user-list="userList"/>
  103.   
  104.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  105. </template><template>
  106.   <user-card-list :user-list="userList"/>
  107.   
  108.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  109. </template><template>
  110.   <user-card-list :user-list="userList"/>
  111.   
  112.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  113. </template>     null comment '账号',    avatarUrl    varchar(1024)<template>
  114.   <user-card-list :user-list="userList"/>
  115.   
  116.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  117. </template><template>
  118.   <user-card-list :user-list="userList"/>
  119.   
  120.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  121. </template><template>
  122.   <user-card-list :user-list="userList"/>
  123.   
  124.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  125. </template>    null comment '用户头像',    gender<template>
  126.   <user-card-list :user-list="userList"/>
  127.   
  128.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  129. </template> tinyint<template>
  130.   <user-card-list :user-list="userList"/>
  131.   
  132.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  133. </template><template>
  134.   <user-card-list :user-list="userList"/>
  135.   
  136.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  137. </template><template>
  138.   <user-card-list :user-list="userList"/>
  139.   
  140.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  141. </template><template>
  142.   <user-card-list :user-list="userList"/>
  143.   
  144.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  145. </template>    null comment '性别',    userPassword varchar(512)<template>
  146.   <user-card-list :user-list="userList"/>
  147.   
  148.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  149. </template><template>
  150.   <user-card-list :user-list="userList"/>
  151.   
  152.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  153. </template><template>
  154.   <user-card-list :user-list="userList"/>
  155.   
  156.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  157. </template>     not null comment '密码',    phone<template>
  158.   <user-card-list :user-list="userList"/>
  159.   
  160.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  161. </template>  varchar(128)<template>
  162.   <user-card-list :user-list="userList"/>
  163.   
  164.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  165. </template><template>
  166.   <user-card-list :user-list="userList"/>
  167.   
  168.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  169. </template><template>
  170.   <user-card-list :user-list="userList"/>
  171.   
  172.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  173. </template>     null comment '电话',    email<template>
  174.   <user-card-list :user-list="userList"/>
  175.   
  176.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  177. </template>  varchar(512)<template>
  178.   <user-card-list :user-list="userList"/>
  179.   
  180.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  181. </template><template>
  182.   <user-card-list :user-list="userList"/>
  183.   
  184.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  185. </template><template>
  186.   <user-card-list :user-list="userList"/>
  187.   
  188.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  189. </template>     null comment '邮箱',    userStatus   int<template>
  190.   <user-card-list :user-list="userList"/>
  191.   
  192.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  193. </template>default 0<template>
  194.   <user-card-list :user-list="userList"/>
  195.   
  196.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  197. </template><template>
  198.   <user-card-list :user-list="userList"/>
  199.   
  200.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  201. </template>     not null comment '状态-0-正常',    createTime   datetime default CURRENT_TIMESTAMP null comment '创建时间',    updateTime   datetime default CURRENT_TIMESTAMP null comment '更新时间',    isDelete     tinyint  default 0<template>
  202.   <user-card-list :user-list="userList"/>
  203.   
  204.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  205. </template><template>
  206.   <user-card-list :user-list="userList"/>
  207.   
  208.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  209. </template>     not null comment '是否删除 0 1(逻辑删除)',    userRole     int<template>
  210.   <user-card-list :user-list="userList"/>
  211.   
  212.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  213. </template>default 0<template>
  214.   <user-card-list :user-list="userList"/>
  215.   
  216.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  217. </template><template>
  218.   <user-card-list :user-list="userList"/>
  219.   
  220.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  221. </template>     not null comment '用户角色 0- 普通用户 1 - 管理员 2 - vip',    planetCode   varchar(512)<template>
  222.   <user-card-list :user-list="userList"/>
  223.   
  224.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  225. </template><template>
  226.   <user-card-list :user-list="userList"/>
  227.   
  228.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  229. </template><template>
  230.   <user-card-list :user-list="userList"/>
  231.   
  232.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  233. </template>     null comment '用户的编号',    tags<template>
  234.   <user-card-list :user-list="userList"/>
  235.   
  236.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  237. </template>   varchar(1024)<template>
  238.   <user-card-list :user-list="userList"/>
  239.   
  240.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  241. </template><template>
  242.   <user-card-list :user-list="userList"/>
  243.   
  244.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  245. </template><template>
  246.   <user-card-list :user-list="userList"/>
  247.   
  248.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  249. </template>    null comment '标签列表 json',    profile<template>
  250.   <user-card-list :user-list="userList"/>
  251.   
  252.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  253. </template>varchar(512)<template>
  254.   <user-card-list :user-list="userList"/>
  255.   
  256.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  257. </template><template>
  258.   <user-card-list :user-list="userList"/>
  259.   
  260.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  261. </template><template>
  262.   <user-card-list :user-list="userList"/>
  263.   
  264.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  265. </template>     null comment '个人简介')    comment '用户';
复制代码
这里我们利用 for 批量插入 50W 条数据,注意:对于这种大数据插入的测试,必须分组,分批进行,防止插入时中间发生报错处理。
  1. package com.yupi.yupao.once.importuser;import com.yupi.yupao.mapper.UserMapper;import com.yupi.yupao.model.domain.User;import org.springframework.stereotype.Component;import org.springframework.util.StopWatch;import javax.annotation.Resource;/** * 导入用户任务 * * @author 程序员鱼皮 * @from 编程导航知识星球 */@Componentpublic class InsertUsers {    @Resource    private UserMapper userMapper;    /**     * 批量插入用户     *///    @Scheduled(initialDelay = 5000, fixedRate = Long.MAX_VALUE)    public void doInsertUsers() {<template>
  2.   <user-card-list :user-list="userList"/>
  3.   
  4.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  5. </template>  StopWatch stopWatch = new StopWatch();<template>
  6.   <user-card-list :user-list="userList"/>
  7.   
  8.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  9. </template>  System.out.println("goodgoodgood");<template>
  10.   <user-card-list :user-list="userList"/>
  11.   
  12.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  13. </template>  stopWatch.start();<template>
  14.   <user-card-list :user-list="userList"/>
  15.   
  16.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  17. </template>  final int INSERT_NUM = 1000;<template>
  18.   <user-card-list :user-list="userList"/>
  19.   
  20.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  21. </template>  for (int i = 0; i < INSERT_NUM; i++) {<template>
  22.   <user-card-list :user-list="userList"/>
  23.   
  24.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  25. </template><template>
  26.   <user-card-list :user-list="userList"/>
  27.   
  28.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  29. </template>User user = new User();<template>
  30.   <user-card-list :user-list="userList"/>
  31.   
  32.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  33. </template><template>
  34.   <user-card-list :user-list="userList"/>
  35.   
  36.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  37. </template>user.setUsername("假鱼皮");<template>
  38.   <user-card-list :user-list="userList"/>
  39.   
  40.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  41. </template><template>
  42.   <user-card-list :user-list="userList"/>
  43.   
  44.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  45. </template>user.setUserAccount("fakeyupi");<template>
  46.   <user-card-list :user-list="userList"/>
  47.   
  48.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  49. </template><template>
  50.   <user-card-list :user-list="userList"/>
  51.   
  52.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  53. </template>user.setAvatarUrl("https://636f-codenav-8grj8px727565176-1256524210.tcb.qcloud.la/img/logo.png");<template>
  54.   <user-card-list :user-list="userList"/>
  55.   
  56.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  57. </template><template>
  58.   <user-card-list :user-list="userList"/>
  59.   
  60.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  61. </template>user.setGender(0);<template>
  62.   <user-card-list :user-list="userList"/>
  63.   
  64.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  65. </template><template>
  66.   <user-card-list :user-list="userList"/>
  67.   
  68.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  69. </template>user.setUserPassword("12345678");<template>
  70.   <user-card-list :user-list="userList"/>
  71.   
  72.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  73. </template><template>
  74.   <user-card-list :user-list="userList"/>
  75.   
  76.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  77. </template>user.setPhone("123");<template>
  78.   <user-card-list :user-list="userList"/>
  79.   
  80.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  81. </template><template>
  82.   <user-card-list :user-list="userList"/>
  83.   
  84.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  85. </template>user.setEmail("123@qq.com");<template>
  86.   <user-card-list :user-list="userList"/>
  87.   
  88.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  89. </template><template>
  90.   <user-card-list :user-list="userList"/>
  91.   
  92.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  93. </template>user.setTags("[]");<template>
  94.   <user-card-list :user-list="userList"/>
  95.   
  96.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  97. </template><template>
  98.   <user-card-list :user-list="userList"/>
  99.   
  100.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  101. </template>user.setUserStatus(0);<template>
  102.   <user-card-list :user-list="userList"/>
  103.   
  104.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  105. </template><template>
  106.   <user-card-list :user-list="userList"/>
  107.   
  108.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  109. </template>user.setUserRole(0);<template>
  110.   <user-card-list :user-list="userList"/>
  111.   
  112.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  113. </template><template>
  114.   <user-card-list :user-list="userList"/>
  115.   
  116.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  117. </template>user.setPlanetCode("11111111");<template>
  118.   <user-card-list :user-list="userList"/>
  119.   
  120.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  121. </template><template>
  122.   <user-card-list :user-list="userList"/>
  123.   
  124.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  125. </template>userMapper.insert(user);<template>
  126.   <user-card-list :user-list="userList"/>
  127.   
  128.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  129. </template>  }<template>
  130.   <user-card-list :user-list="userList"/>
  131.   
  132.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  133. </template>  stopWatch.stop();<template>
  134.   <user-card-list :user-list="userList"/>
  135.   
  136.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  137. </template>  System.out.println(stopWatch.getTotalTimeMillis());    }}
复制代码
  1. package com.yupi.yupao.service;import com.yupi.yupao.model.domain.User;import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.util.StopWatch;import javax.annotation.Resource;import java.util.ArrayList;import java.util.List;import java.util.concurrent.*;/** * 导入用户测试 * * @author 程序员鱼皮 * @from 编程导航知识星球 */@SpringBootTestpublic class InsertUsersTest {    @Resource    private UserService userService;    private ExecutorService executorService = new ThreadPoolExecutor(40, 1000, 10000, TimeUnit.MINUTES, new ArrayBlockingQueue(10000));    /**     * 批量插入用户     */    @Test    public void doInsertUsers() {<template>
  2.   <user-card-list :user-list="userList"/>
  3.   
  4.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  5. </template>  StopWatch stopWatch = new StopWatch();<template>
  6.   <user-card-list :user-list="userList"/>
  7.   
  8.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  9. </template>  stopWatch.start();<template>
  10.   <user-card-list :user-list="userList"/>
  11.   
  12.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  13. </template>  final int INSERT_NUM = 100000;<template>
  14.   <user-card-list :user-list="userList"/>
  15.   
  16.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  17. </template>  List userList = new ArrayList();<template>
  18.   <user-card-list :user-list="userList"/>
  19.   
  20.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  21. </template>  for (int i = 0; i < INSERT_NUM; i++) {<template>
  22.   <user-card-list :user-list="userList"/>
  23.   
  24.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  25. </template><template>
  26.   <user-card-list :user-list="userList"/>
  27.   
  28.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  29. </template>User user = new User();<template>
  30.   <user-card-list :user-list="userList"/>
  31.   
  32.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  33. </template><template>
  34.   <user-card-list :user-list="userList"/>
  35.   
  36.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  37. </template>user.setUsername("原_创 【鱼_皮】https://t.zsxq.com/0emozsIJh");<template>
  38.   <user-card-list :user-list="userList"/>
  39.   
  40.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  41. </template><template>
  42.   <user-card-list :user-list="userList"/>
  43.   
  44.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  45. </template>user.setUserAccount("fakeyupi");<template>
  46.   <user-card-list :user-list="userList"/>
  47.   
  48.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  49. </template><template>
  50.   <user-card-list :user-list="userList"/>
  51.   
  52.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  53. </template>user.setAvatarUrl("https://636f-codenav-8grj8px727565176-1256524210.tcb.qcloud.la/img/logo.png");<template>
  54.   <user-card-list :user-list="userList"/>
  55.   
  56.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  57. </template><template>
  58.   <user-card-list :user-list="userList"/>
  59.   
  60.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  61. </template>user.setGender(0);<template>
  62.   <user-card-list :user-list="userList"/>
  63.   
  64.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  65. </template><template>
  66.   <user-card-list :user-list="userList"/>
  67.   
  68.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  69. </template>user.setUserPassword("12345678");<template>
  70.   <user-card-list :user-list="userList"/>
  71.   
  72.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  73. </template><template>
  74.   <user-card-list :user-list="userList"/>
  75.   
  76.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  77. </template>user.setPhone("123");<template>
  78.   <user-card-list :user-list="userList"/>
  79.   
  80.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  81. </template><template>
  82.   <user-card-list :user-list="userList"/>
  83.   
  84.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  85. </template>user.setEmail("123@qq.com");<template>
  86.   <user-card-list :user-list="userList"/>
  87.   
  88.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  89. </template><template>
  90.   <user-card-list :user-list="userList"/>
  91.   
  92.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  93. </template>user.setTags("[]");<template>
  94.   <user-card-list :user-list="userList"/>
  95.   
  96.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  97. </template><template>
  98.   <user-card-list :user-list="userList"/>
  99.   
  100.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  101. </template>user.setUserStatus(0);<template>
  102.   <user-card-list :user-list="userList"/>
  103.   
  104.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  105. </template><template>
  106.   <user-card-list :user-list="userList"/>
  107.   
  108.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  109. </template>user.setUserRole(0);<template>
  110.   <user-card-list :user-list="userList"/>
  111.   
  112.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  113. </template><template>
  114.   <user-card-list :user-list="userList"/>
  115.   
  116.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  117. </template>user.setPlanetCode("11111111");<template>
  118.   <user-card-list :user-list="userList"/>
  119.   
  120.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  121. </template><template>
  122.   <user-card-list :user-list="userList"/>
  123.   
  124.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  125. </template>userList.add(user);<template>
  126.   <user-card-list :user-list="userList"/>
  127.   
  128.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  129. </template>  }<template>
  130.   <user-card-list :user-list="userList"/>
  131.   
  132.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  133. </template>  // 20 秒 10 万条<template>
  134.   <user-card-list :user-list="userList"/>
  135.   
  136.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  137. </template>  userService.saveBatch(userList, 10000);<template>
  138.   <user-card-list :user-list="userList"/>
  139.   
  140.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  141. </template>  stopWatch.stop();<template>
  142.   <user-card-list :user-list="userList"/>
  143.   
  144.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  145. </template>  System.out.println(stopWatch.getTotalTimeMillis());    }    /**     * 并发批量插入用户     */    @Test    public void doConcurrencyInsertUsers() {<template>
  146.   <user-card-list :user-list="userList"/>
  147.   
  148.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  149. </template>  StopWatch stopWatch = new StopWatch();<template>
  150.   <user-card-list :user-list="userList"/>
  151.   
  152.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  153. </template>  stopWatch.start();<template>
  154.   <user-card-list :user-list="userList"/>
  155.   
  156.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  157. </template>  // 分十组<template>
  158.   <user-card-list :user-list="userList"/>
  159.   
  160.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  161. </template>  int batchSize = 5000;<template>
  162.   <user-card-list :user-list="userList"/>
  163.   
  164.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  165. </template>  int j = 0;<template>
  166.   <user-card-list :user-list="userList"/>
  167.   
  168.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  169. </template>  List futureList = new ArrayList();<template>
  170.   <user-card-list :user-list="userList"/>
  171.   
  172.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  173. </template>  for (int i = 0; i < 100; i++) {<template>
  174.   <user-card-list :user-list="userList"/>
  175.   
  176.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  177. </template><template>
  178.   <user-card-list :user-list="userList"/>
  179.   
  180.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  181. </template>List userList = new ArrayList();<template>
  182.   <user-card-list :user-list="userList"/>
  183.   
  184.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  185. </template><template>
  186.   <user-card-list :user-list="userList"/>
  187.   
  188.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  189. </template>while (true) {<template>
  190.   <user-card-list :user-list="userList"/>
  191.   
  192.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  193. </template><template>
  194.   <user-card-list :user-list="userList"/>
  195.   
  196.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  197. </template>    j++;<template>
  198.   <user-card-list :user-list="userList"/>
  199.   
  200.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  201. </template><template>
  202.   <user-card-list :user-list="userList"/>
  203.   
  204.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  205. </template>    User user = new User();<template>
  206.   <user-card-list :user-list="userList"/>
  207.   
  208.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  209. </template><template>
  210.   <user-card-list :user-list="userList"/>
  211.   
  212.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  213. </template>    user.setUsername("假鱼皮");<template>
  214.   <user-card-list :user-list="userList"/>
  215.   
  216.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  217. </template><template>
  218.   <user-card-list :user-list="userList"/>
  219.   
  220.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  221. </template>    user.setUserAccount("fakeyupi");<template>
  222.   <user-card-list :user-list="userList"/>
  223.   
  224.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  225. </template><template>
  226.   <user-card-list :user-list="userList"/>
  227.   
  228.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  229. </template>    user.setAvatarUrl("https://636f-codenav-8grj8px727565176-1256524210.tcb.qcloud.la/img/logo.png");<template>
  230.   <user-card-list :user-list="userList"/>
  231.   
  232.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  233. </template><template>
  234.   <user-card-list :user-list="userList"/>
  235.   
  236.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  237. </template>    user.setGender(0);<template>
  238.   <user-card-list :user-list="userList"/>
  239.   
  240.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  241. </template><template>
  242.   <user-card-list :user-list="userList"/>
  243.   
  244.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  245. </template>    user.setUserPassword("12345678");<template>
  246.   <user-card-list :user-list="userList"/>
  247.   
  248.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  249. </template><template>
  250.   <user-card-list :user-list="userList"/>
  251.   
  252.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  253. </template>    user.setPhone("123");<template>
  254.   <user-card-list :user-list="userList"/>
  255.   
  256.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  257. </template><template>
  258.   <user-card-list :user-list="userList"/>
  259.   
  260.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  261. </template>    user.setEmail("123@qq.com");<template>
  262.   <user-card-list :user-list="userList"/>
  263.   
  264.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  265. </template><template>
  266.   <user-card-list :user-list="userList"/>
  267.   
  268.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  269. </template>    user.setTags("[]");<template>
  270.   <user-card-list :user-list="userList"/>
  271.   
  272.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  273. </template><template>
  274.   <user-card-list :user-list="userList"/>
  275.   
  276.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  277. </template>    user.setUserStatus(0);<template>
  278.   <user-card-list :user-list="userList"/>
  279.   
  280.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  281. </template><template>
  282.   <user-card-list :user-list="userList"/>
  283.   
  284.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  285. </template>    user.setUserRole(0);<template>
  286.   <user-card-list :user-list="userList"/>
  287.   
  288.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  289. </template><template>
  290.   <user-card-list :user-list="userList"/>
  291.   
  292.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  293. </template>    user.setPlanetCode("11111111");<template>
  294.   <user-card-list :user-list="userList"/>
  295.   
  296.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  297. </template><template>
  298.   <user-card-list :user-list="userList"/>
  299.   
  300.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  301. </template>    userList.add(user);<template>
  302.   <user-card-list :user-list="userList"/>
  303.   
  304.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  305. </template><template>
  306.   <user-card-list :user-list="userList"/>
  307.   
  308.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  309. </template>    if (j % batchSize == 0) {<template>
  310.   <user-card-list :user-list="userList"/>
  311.   
  312.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  313. </template><template>
  314.   <user-card-list :user-list="userList"/>
  315.   
  316.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  317. </template><template>
  318.   <user-card-list :user-list="userList"/>
  319.   
  320.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  321. </template>  break;<template>
  322.   <user-card-list :user-list="userList"/>
  323.   
  324.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  325. </template><template>
  326.   <user-card-list :user-list="userList"/>
  327.   
  328.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  329. </template>    }<template>
  330.   <user-card-list :user-list="userList"/>
  331.   
  332.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  333. </template><template>
  334.   <user-card-list :user-list="userList"/>
  335.   
  336.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  337. </template>}<template>
  338.   <user-card-list :user-list="userList"/>
  339.   
  340.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  341. </template><template>
  342.   <user-card-list :user-list="userList"/>
  343.   
  344.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  345. </template>// 异步执行<template>
  346.   <user-card-list :user-list="userList"/>
  347.   
  348.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  349. </template><template>
  350.   <user-card-list :user-list="userList"/>
  351.   
  352.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  353. </template>CompletableFuture future = CompletableFuture.runAsync(() -> {<template>
  354.   <user-card-list :user-list="userList"/>
  355.   
  356.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  357. </template><template>
  358.   <user-card-list :user-list="userList"/>
  359.   
  360.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  361. </template>    System.out.println("threadName: " + Thread.currentThread().getName());<template>
  362.   <user-card-list :user-list="userList"/>
  363.   
  364.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  365. </template><template>
  366.   <user-card-list :user-list="userList"/>
  367.   
  368.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  369. </template>    userService.saveBatch(userList, batchSize);<template>
  370.   <user-card-list :user-list="userList"/>
  371.   
  372.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  373. </template><template>
  374.   <user-card-list :user-list="userList"/>
  375.   
  376.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  377. </template>}, executorService);<template>
  378.   <user-card-list :user-list="userList"/>
  379.   
  380.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  381. </template><template>
  382.   <user-card-list :user-list="userList"/>
  383.   
  384.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  385. </template>futureList.add(future);<template>
  386.   <user-card-list :user-list="userList"/>
  387.   
  388.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  389. </template>  }<template>
  390.   <user-card-list :user-list="userList"/>
  391.   
  392.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  393. </template>  CompletableFuture.allOf(futureList.toArray(new CompletableFuture[]{})).join();<template>
  394.   <user-card-list :user-list="userList"/>
  395.   
  396.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  397. </template>  // 20 秒 10 万条<template>
  398.   <user-card-list :user-list="userList"/>
  399.   
  400.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  401. </template>  stopWatch.stop();<template>
  402.   <user-card-list :user-list="userList"/>
  403.   
  404.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  405. </template>  System.out.println(stopWatch.getTotalTimeMillis());    }}
复制代码
访问前端,查看前端是否会因为 50 W 数据,渲染加载失败。
这里未使用 MyBatis-Plus 进行分页查询,前端渲染失败。
9.png


11.png

使用了 MyBatis-Plus 分页插件,测试的效果
12.png

关于:页面加载太慢的问题,如下:使用缓存和分布式缓存策略,解决。
缓存和分布式缓存

数据查询慢怎么办?


  • 用缓存:提前把数据取出来保存好(通常保存到读写更快的介质,eg 内存),就可以更快地读写。
缓存:

  • Redis(分布式缓存)
  • memcached(分布式)
  • Etcd( 云原生架构的一个分布式存储,存储配置,扩容能力)
进程(单机)缓存

  • ehcache
  • Java 内存集合,如 HashMap
  • Caffeine ( Java 内存缓存性能之王,高性能)
  • Google Guava

  • 分库分表
  • 提前查询
Java 操作 Redis

Spriing Data Redis(推荐)

地址:https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis
Spring  Data : 通用的数据访问框架,定义了一组 增删改查的接口。
还可以操作:MySQL,Redis,JPA
使用方式如下:

  • 引入
  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     spring-boot-starter-data-redis</artifactId>
  4.     <version>2.6.4</version>
  5. </dependency>
复制代码
也可以添加上:Lettuce是一个高级redis客户端,支持高级的redis特性,比如Sentinel、集群、流水线、自动重新连接和redis数据
  1. <dependency>
  2.     <groupId>io.lettuce</groupId>
  3.     lettuce-core</artifactId>
  4.     <version>6.1.6.RELEASE</version>
  5. </dependency>
复制代码

  • 配置 Redis 地址
  1. spring:
  2.   # redis 配置
  3.   redis:
  4.     port: 6379
  5.     host: localhost
  6.     database: 0 # 显式使用的是 Redis 0 号数据库
复制代码
Redis 数据结构:

  • String 字符串类型:name:"yupi"
  • List 列表:names:["yupi","dogyupi"]。 List 和 数组的区别。数组的大小是固定的,而 List 大小是动态的
  • Set 集合:names:["yupi","dogyupi"](值不重复)
  • Hash 哈希:nameAge:
  • Zset 集合:names: '{yupi - 9,dogyupi - 12 }' 适合排行榜
高级:

  • bloomfilter (布隆过滤器,主要从大量的数据中快速过滤值,比如邮件黑名单拦截)
  • geo(计算地理位置)
  • hyperloglog (pv / uv)
  • pub / sub(发布订阅,类似消息队列)
  • BitMap (100101010101010101010101)
引入一个新库时,记得先写测试类
测试:使用成功连接上了 Redis ,是否可以成功操作 Redis 数据库
  1. package com.rainbowsea.yupao.service;import com.rainbowsea.yupao.model.User;import org.junit.jupiter.api.Assertions;import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import javax.annotation.Resource;@SpringBootTestpublic class RedisTest {    @Resource    private RedisTemplate redisTemplate; // 实例化 操作 Redis 的实例模板    @Test    void test() {<template>
  2.   <user-card-list :user-list="userList"/>
  3.   
  4.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  5. </template>  ValueOperations valueOperations = redisTemplate.opsForValue();<template>
  6.   <user-card-list :user-list="userList"/>
  7.   
  8.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  9. </template>  // 增<template>
  10.   <user-card-list :user-list="userList"/>
  11.   
  12.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  13. </template>  valueOperations.set("yupiString","dog");<template>
  14.   <user-card-list :user-list="userList"/>
  15.   
  16.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  17. </template>  valueOperations.set("yupiInt",1);<template>
  18.   <user-card-list :user-list="userList"/>
  19.   
  20.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  21. </template>  valueOperations.set("yupiDouble",2.0);<template>
  22.   <user-card-list :user-list="userList"/>
  23.   
  24.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  25. </template>  User user = new User();<template>
  26.   <user-card-list :user-list="userList"/>
  27.   
  28.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  29. </template>  user.setId(1L);<template>
  30.   <user-card-list :user-list="userList"/>
  31.   
  32.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  33. </template>  user.setUsername("yupi");<template>
  34.   <user-card-list :user-list="userList"/>
  35.   
  36.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  37. </template>  valueOperations.set("yupiUser",user);<template>
  38.   <user-card-list :user-list="userList"/>
  39.   
  40.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  41. </template>  // 查<template>
  42.   <user-card-list :user-list="userList"/>
  43.   
  44.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  45. </template>  Object yupi = valueOperations.get("yupiString");<template>
  46.   <user-card-list :user-list="userList"/>
  47.   
  48.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  49. </template>  Assertions.assertTrue("dog".equals((String)yupi));<template>
  50.   <user-card-list :user-list="userList"/>
  51.   
  52.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  53. </template>  yupi = valueOperations.get("yupiInt");<template>
  54.   <user-card-list :user-list="userList"/>
  55.   
  56.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  57. </template>  Assertions.assertTrue(1 == (Integer) yupi);<template>
  58.   <user-card-list :user-list="userList"/>
  59.   
  60.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  61. </template>  yupi = valueOperations.get("yupiDouble");<template>
  62.   <user-card-list :user-list="userList"/>
  63.   
  64.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  65. </template>  Assertions.assertTrue(2.0 == (Double) yupi);<template>
  66.   <user-card-list :user-list="userList"/>
  67.   
  68.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  69. </template>  System.out.println(valueOperations.get("yupiUser"));    }}
复制代码
Reids 自定义序列化:RedisTemplateConfig

为了防止写入的 Redis 的数据乱码,浪费空间等,可以自定义序列化器。示例代码如下:
  1. package com.yupi.yupao.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.RedisSerializer;@Configurationpublic class RedisTemplateConfig {    @Bean    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {<template>
  2.   <user-card-list :user-list="userList"/>
  3.   
  4.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  5. </template>  RedisTemplate redisTemplate = new RedisTemplate();<template>
  6.   <user-card-list :user-list="userList"/>
  7.   
  8.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  9. </template>  redisTemplate.setConnectionFactory(connectionFactory);<template>
  10.   <user-card-list :user-list="userList"/>
  11.   
  12.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  13. </template>  redisTemplate.setKeySerializer(RedisSerializer.string());<template>
  14.   <user-card-list :user-list="userList"/>
  15.   
  16.   <van-empty v-if="!userList || userList.length < 1" description="数据为空"/>
  17. </template>  return redisTemplate;    }}
复制代码


设计缓存 key

不同用户看到的数据不同。
建议格式:
systemID:moduleID:funcu: (注意:就是不要和别的冲突,因为可能存在多个人/项目共用一个 Redis 缓存)
eg:这里我们设计:**yupao:user:recommed:userId**

**注意:Redis 内存不能无限增加,一定要设置过期时间。Redis 内存是有限的,而且在服务器当中特别贵。非常重要<strong><strong>
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
6 小时前

举报

用心讨论,共获提升!
您需要登录后才可以回帖 登录 | 立即注册