Commit 956b1c219d35cdf8dbf1d9887fd787173d441fda

Authored by zhangdaiscott
1 parent b3f11d93

增加动态数据源示例。xxjon例子

jeecg-boot/jeecg-boot-module-demo/src/main/java/org/jeecg/modules/demo/test/controller/JeecgDemoController.java
1 1 package org.jeecg.modules.demo.test.controller;
2 2  
  3 +import com.alibaba.fastjson.JSON;
3 4 import com.alibaba.fastjson.JSONArray;
4 5 import com.alibaba.fastjson.JSONObject;
5 6 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
... ... @@ -17,6 +18,7 @@ import org.jeecg.common.system.base.controller.JeecgController;
17 18 import org.jeecg.common.system.query.QueryGenerator;
18 19 import org.jeecg.common.util.DateUtils;
19 20 import org.jeecg.common.util.RedisUtil;
  21 +import org.jeecg.common.util.UUIDGenerator;
20 22 import org.jeecg.modules.demo.test.entity.JeecgDemo;
21 23 import org.jeecg.modules.demo.test.service.IJeecgDemoService;
22 24 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -325,4 +327,133 @@ public class JeecgDemoController extends JeecgController<JeecgDemo, IJeecgDemoSe
325 327 return "hello world!";
326 328 }
327 329  
  330 + // =====Vue3 Native 原生页面示例===============================================================================================
  331 + @GetMapping(value = "/oneNative/list")
  332 + public Result oneNativeList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
  333 + Object oneNative = redisUtil.get("one-native");
  334 + JSONArray data = new JSONArray();
  335 + if(null != oneNative){
  336 + JSONObject nativeObject = (JSONObject) oneNative;
  337 + data = nativeObject.getJSONArray("data");
  338 + }
  339 + IPage<JSONObject> objectPage = queryDataPage(data, pageNo, pageSize);
  340 + return Result.OK(objectPage);
  341 + }
  342 +
  343 + @PostMapping("/oneNative/add")
  344 + public Result<String> oneNativeAdd(@RequestBody JSONObject jsonObject){
  345 + Object oneNative = redisUtil.get("one-native");
  346 + JSONObject nativeObject = new JSONObject();
  347 + JSONArray data = new JSONArray();
  348 + if(null != oneNative){
  349 + nativeObject = (JSONObject) oneNative;
  350 + data = nativeObject.getJSONArray("data");
  351 + }
  352 + jsonObject.put("id", UUIDGenerator.generate());
  353 + data.add(jsonObject);
  354 + nativeObject.put("data",data);
  355 + redisUtil.set("one-native",nativeObject);
  356 + return Result.OK("添加成功");
  357 + }
  358 +
  359 + @PutMapping("/oneNative/edit")
  360 + public Result<String> oneNativeEdit(@RequestBody JSONObject jsonObject){
  361 + JSONObject oneNative = (JSONObject)redisUtil.get("one-native");
  362 + JSONArray data = oneNative.getJSONArray("data");
  363 + data = getNativeById(data,jsonObject);
  364 + oneNative.put("data", data);
  365 + redisUtil.set("one-native", oneNative);
  366 + return Result.OK("修改成功");
  367 + }
  368 +
  369 + @DeleteMapping("/oneNative/delete")
  370 + public Result<String> oneNativeDelete(@RequestParam(name = "ids") String ids){
  371 + Object oneNative = redisUtil.get("one-native");
  372 + if(null != oneNative){
  373 + JSONObject nativeObject = (JSONObject) oneNative;
  374 + JSONArray data = nativeObject.getJSONArray("data");
  375 + data = deleteNativeById(data,ids);
  376 + nativeObject.put("data",data);
  377 + redisUtil.set("one-native",nativeObject);
  378 + }
  379 + return Result.OK("删除成功");
  380 + }
  381 +
  382 + /**
  383 + * 获取redis对应id的数据
  384 + * @param data
  385 + * @param jsonObject
  386 + * @return
  387 + */
  388 + public JSONArray getNativeById(JSONArray data,JSONObject jsonObject){
  389 + String dbId = "id";
  390 + String id = jsonObject.getString(dbId);
  391 + for (int i = 0; i < data.size(); i++) {
  392 + if(id.equals(data.getJSONObject(i).getString(dbId))){
  393 + data.set(i,jsonObject);
  394 + break;
  395 + }
  396 + }
  397 + return data;
  398 + }
  399 +
  400 + /**
  401 + * 删除redis中包含的id数据
  402 + * @param data
  403 + * @param ids
  404 + * @return
  405 + */
  406 + public JSONArray deleteNativeById(JSONArray data,String ids){
  407 + String dbId = "id";
  408 + for (int i = 0; i < data.size(); i++) {
  409 + //如果id包含直接清除data中的数据
  410 + if(ids.contains(data.getJSONObject(i).getString(dbId))){
  411 + data.fluentRemove(i);
  412 + }
  413 + //判断data的长度是否还剩1位
  414 + if(data.size() == 1 && ids.contains(data.getJSONObject(0).getString(dbId))){
  415 + data.fluentRemove(0);
  416 + }
  417 + }
  418 + return data;
  419 + }
  420 +
  421 + /**
  422 + * 模拟查询数据,可以根据父ID查询,可以分页
  423 + *
  424 + * @param dataList 数据列表
  425 + * @param pageNo 页码
  426 + * @param pageSize 页大小
  427 + * @return
  428 + */
  429 + private IPage<JSONObject> queryDataPage(JSONArray dataList, Integer pageNo, Integer pageSize) {
  430 + // 根据父级id查询子级
  431 + JSONArray dataDb = dataList;
  432 + // 模拟分页(实际中应用SQL自带的分页)
  433 + List<JSONObject> records = new ArrayList<>();
  434 + IPage<JSONObject> page;
  435 + long beginIndex, endIndex;
  436 + // 如果任意一个参数为null,则不分页
  437 + if (pageNo == null || pageSize == null) {
  438 + page = new Page<>(0, dataDb.size());
  439 + beginIndex = 0;
  440 + endIndex = dataDb.size();
  441 + } else {
  442 + page = new Page<>(pageNo, pageSize);
  443 + beginIndex = page.offset();
  444 + endIndex = page.offset() + page.getSize();
  445 + }
  446 + for (long i = beginIndex; (i < endIndex && i < dataDb.size()); i++) {
  447 + JSONObject data = dataDb.getJSONObject((int) i);
  448 + data = JSON.parseObject(data.toJSONString());
  449 + // 不返回 children
  450 + data.remove("children");
  451 + records.add(data);
  452 + }
  453 + page.setRecords(records);
  454 + page.setTotal(dataDb.size());
  455 + return page;
  456 + }
  457 + // =====Vue3 Native 原生页面示例===============================================================================================
  458 +
328 459 }
... ...
jeecg-boot/jeecg-boot-module-demo/src/main/java/org/jeecg/modules/demo/test/controller/JeecgDynamicDataController.java 0 → 100644
  1 +package org.jeecg.modules.demo.test.controller;
  2 +
  3 +import io.lettuce.core.dynamic.annotation.Param;
  4 +import io.swagger.annotations.Api;
  5 +import io.swagger.annotations.ApiOperation;
  6 +import lombok.extern.slf4j.Slf4j;
  7 +import org.jeecg.common.api.vo.Result;
  8 +import org.jeecg.common.aspect.annotation.AutoLog;
  9 +import org.jeecg.common.system.base.controller.JeecgController;
  10 +import org.jeecg.modules.demo.test.entity.JeecgDemo;
  11 +import org.jeecg.modules.demo.test.service.IJeecgDemoService;
  12 +import org.jeecg.modules.demo.test.service.IJeecgDynamicDataService;
  13 +import org.springframework.beans.factory.annotation.Autowired;
  14 +import org.springframework.web.bind.annotation.*;
  15 +
  16 +import java.util.List;
  17 +
  18 +/**
  19 + * @Description: 动态数据源测试
  20 + * @Author: zyf
  21 + * @Date:2020-04-21
  22 + */
  23 +@Slf4j
  24 +@Api(tags = "动态数据源测试")
  25 +@RestController
  26 +@RequestMapping("/test/dynamic")
  27 +public class JeecgDynamicDataController extends JeecgController<JeecgDemo, IJeecgDemoService> {
  28 +
  29 + @Autowired
  30 + private IJeecgDynamicDataService jeecgDynamicDataService;
  31 +
  32 +
  33 + /**
  34 + * 动态切换数据源
  35 +
  36 + * @return
  37 + */
  38 + @PostMapping(value = "/test1")
  39 + @AutoLog(value = "动态切换数据源")
  40 + @ApiOperation(value = "动态切换数据源", notes = "动态切换数据源")
  41 + public Result<List<JeecgDemo>> selectSpelByKey(@RequestParam(required = false) String dsName) {
  42 + List<JeecgDemo> list = jeecgDynamicDataService.selectSpelByKey(dsName);
  43 + return Result.OK(list);
  44 + }
  45 +
  46 +
  47 +}
... ...
jeecg-boot/jeecg-boot-module-demo/src/main/java/org/jeecg/modules/demo/test/entity/JeecgOrderMain.java
... ... @@ -47,4 +47,6 @@ public class JeecgOrderMain implements Serializable {
47 47 @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
48 48 @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
49 49 private java.util.Date updateTime;
  50 +
  51 + private String bpmStatus;
50 52 }
... ...
jeecg-boot/jeecg-boot-module-demo/src/main/java/org/jeecg/modules/demo/test/service/IJeecgDynamicDataService.java 0 → 100644
  1 +package org.jeecg.modules.demo.test.service;
  2 +
  3 +import org.jeecg.common.system.base.service.JeecgService;
  4 +import org.jeecg.modules.demo.test.entity.JeecgDemo;
  5 +
  6 +import java.util.List;
  7 +
  8 +/**
  9 + * @Description: 动态数据源测试
  10 + * @Author: zyf
  11 + * @Date:2020-04-21
  12 + */
  13 +public interface IJeecgDynamicDataService extends JeecgService<JeecgDemo> {
  14 +
  15 + /**
  16 + * 测试从header获取数据源
  17 + * @return
  18 + */
  19 + public List<JeecgDemo> selectSpelByHeader();
  20 +
  21 + /**
  22 + * 使用spel从参数获取
  23 + * @param dsName
  24 + * @return
  25 + */
  26 + public List<JeecgDemo> selectSpelByKey(String dsName);
  27 +
  28 +}
... ...
jeecg-boot/jeecg-boot-module-demo/src/main/java/org/jeecg/modules/demo/test/service/impl/JeecgDynamicDataServiceImpl.java 0 → 100644
  1 +package org.jeecg.modules.demo.test.service.impl;
  2 +
  3 +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  4 +import org.jeecg.modules.demo.test.entity.JeecgDemo;
  5 +import org.jeecg.modules.demo.test.mapper.JeecgDemoMapper;
  6 +import org.jeecg.modules.demo.test.service.IJeecgDynamicDataService;
  7 +import org.springframework.stereotype.Service;
  8 +
  9 +import java.util.List;
  10 +
  11 +/**
  12 + * @Description: 动态数据源测试
  13 + * @Author: zyf
  14 + * @Date:2020-04-21
  15 + */
  16 +@Service
  17 +public class JeecgDynamicDataServiceImpl extends ServiceImpl<JeecgDemoMapper, JeecgDemo> implements IJeecgDynamicDataService {
  18 +
  19 + @Override
  20 + public List<JeecgDemo> selectSpelByHeader() {
  21 + return list();
  22 + }
  23 +
  24 + @Override
  25 + public List<JeecgDemo> selectSpelByKey(String dsName) {
  26 + return list();
  27 + }
  28 +}
... ...
jeecg-boot/jeecg-boot-module-demo/src/main/java/org/jeecg/modules/demo/test/vo/JeecgOrderMainPage.java
... ... @@ -44,5 +44,7 @@ public class JeecgOrderMainPage {
44 44 private List<JeecgOrderCustomer> jeecgOrderCustomerList;
45 45 @ExcelCollection(name="机票")
46 46 private List<JeecgOrderTicket> jeecgOrderTicketList;
  47 +
  48 + private String bpmStatus;
47 49  
48 50 }
... ...
jeecg-boot/jeecg-boot-module-demo/src/main/java/org/jeecg/modules/demo/xxljob/TestJobHandler.java 0 → 100644
  1 +//
  2 +//package org.jeecg.modules.demo.xxljob;
  3 +//
  4 +//import com.xxl.job.core.biz.model.ReturnT;
  5 +//import com.xxl.job.core.handler.annotation.XxlJob;
  6 +//import lombok.extern.slf4j.Slf4j;
  7 +//import org.jeecg.common.config.mqtoken.UserTokenContext;
  8 +//import org.jeecg.common.constant.CommonConstant;
  9 +//import org.jeecg.common.system.api.ISysBaseAPI;
  10 +//import org.jeecg.common.system.util.JwtUtil;
  11 +//import org.jeecg.common.util.RedisUtil;
  12 +//import org.jeecg.common.util.SpringContextUtils;
  13 +//import org.springframework.beans.factory.annotation.Autowired;
  14 +//import org.springframework.stereotype.Component;
  15 +//
  16 +//
  17 +///**
  18 +// * xxl-job定时任务测试
  19 +// */
  20 +//@Component
  21 +//@Slf4j
  22 +//public class TestJobHandler {
  23 +// @Autowired
  24 +// ISysBaseAPI sysBaseApi;
  25 +//
  26 +// /**
  27 +// * 简单任务
  28 +// *
  29 +// * 测试:无token调用feign接口
  30 +// *
  31 +// * @param params
  32 +// * @return
  33 +// */
  34 +//
  35 +// @XxlJob(value = "testJob")
  36 +// public ReturnT<String> demoJobHandler(String params) {
  37 +// //1.生成临时令牌Token到线程中
  38 +// UserTokenContext.setToken(getTemporaryToken());
  39 +//
  40 +// log.info("我是 jeecg-demo 服务里的定时任务 testJob , 我执行了...............................");
  41 +// log.info("我调用 jeecg-system 服务的字典接口:{}",sysBaseApi.queryAllDict());
  42 +// //。。。此处可以写多个feign接口调用
  43 +//
  44 +// //2.使用完,删除临时令牌Token
  45 +// UserTokenContext.remove();
  46 +// return ReturnT.SUCCESS;
  47 +// }
  48 +//
  49 +// public void init() {
  50 +// log.info("init");
  51 +// }
  52 +//
  53 +// public void destroy() {
  54 +// log.info("destory");
  55 +// }
  56 +//
  57 +// /**
  58 +// * 获取临时令牌
  59 +// *
  60 +// * 模拟登陆接口,获取模拟 Token
  61 +// * @return
  62 +// */
  63 +// public static String getTemporaryToken() {
  64 +// RedisUtil redisUtil = SpringContextUtils.getBean(RedisUtil.class);
  65 +// // 模拟登录生成Token
  66 +// String token = JwtUtil.sign("??", "??");
  67 +// // 设置Token缓存有效时间为 5 分钟
  68 +// redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
  69 +// redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, 5 * 60 * 1000);
  70 +// return token;
  71 +// }
  72 +//
  73 +//}
  74 +//
... ...