JECloud中的微服务项目中模块接口开发
JECloud中的微服务项目搭建好之后,需要开发一些简单的增删改查接口,在前端业务中进行调用。
开发步骤
1、拉取代码
进入JECloud平台开源Gitee仓库: https://gitee.com/ketr
例如以下代码都写在jecloud-demo案例项目中,所以直接下载这个项目代码即可:https://gitee.com/ketr/jecloud-demo
2、熟悉代码结构
每个项目都有README.md文件,可以看下项目的简单介绍
3、平台基础类介绍
1、AbstractPlatformController类
这个类是父类的controller,自己建的Controller都可以继承这个类,使用父类的方法;
2、MetaService类
这个类是操作数据库的,里面提供一些增删改查的方法,条件是用ConditionsWrapper拼接的;
3、CommonService类
通用的实现类,就是将AbstractPlatformController类的通用方法进行实现;
4、增删改查接口代码
package com.je.demo.test.controller;
import com.je.common.base.DynaBean;
import com.je.common.base.mvc.AbstractPlatformController;
import com.je.common.base.mvc.BaseMethodArgument;
import com.je.common.base.result.BaseRespResult;
import com.je.common.base.service.CommonService;
import com.je.common.base.service.MetaService;
import com.je.common.base.service.rpc.SystemSettingRpcService;
import com.je.common.base.util.SecurityUserHolder;
import com.je.common.base.util.StringUtil;
import com.je.ibatis.extension.conditions.ConditionsWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
/**
* 此类为demo服务下的一个测试类
* 1、包含增删改查方法接口,供前端调运
* 2、平台基础操作方法、比如获取当前登录人信息;获取系统设置数据;
*/
@RestController
@RequestMapping(value = "/je/demo")
public class DemoTestController extends AbstractPlatformController {
/**
* 新增数据
* @param request
* @return
*/
@ResponseBody
@RequestMapping(value = "/doDemoSave", method = RequestMethod.POST)
public BaseRespResult doDemoSave(BaseMethodArgument param, HttpServletRequest request) {
//获取前端传参
String JKDY_XMMC = getStringParameter(request, "JKDY_XMMC");
String JKDY_XMZT_CODE = getStringParameter(request, "JKDY_XMZT_CODE");
String JKDY_XMZT_NAME = getStringParameter(request, "JKDY_XMZT_NAME");
String JKDY_XMKSSJ = getStringParameter(request, "JKDY_XMKSSJ");
String JKDY_XMJSSJ = getStringParameter(request, "JKDY_XMJSSJ");
String JKDY_XMGS = getStringParameter(request, "JKDY_XMGS");
//编号生成规则
String codeGenFieldInfo = getStringParameter(request, "codeGenFieldInfo");
String tableCode = param.getTableCode();
DynaBean dynaBean = new DynaBean(tableCode, true);
//设置系统字段默认值,设置sy_系统字段默认值
commonService.buildModelCreateInfo(dynaBean);
//构建编号
if (StringUtil.isNotEmpty(codeGenFieldInfo)) {
commonService.buildCode(codeGenFieldInfo, dynaBean);
}
dynaBean.setStr("JKDY_XMMC", JKDY_XMMC);
dynaBean.setStr("JKDY_XMZT_CODE", JKDY_XMZT_CODE);
dynaBean.setStr("JKDY_XMZT_NAME", JKDY_XMZT_NAME);
dynaBean.setStr("JKDY_XMKSSJ", JKDY_XMKSSJ);
dynaBean.setStr("JKDY_XMJSSJ", JKDY_XMJSSJ);
dynaBean.setStr("JKDY_XMGS", JKDY_XMGS);
metaService.insert(dynaBean);
return BaseRespResult.successResult(dynaBean);
}
/**
* 根据条件删除数据
* @param param
* @param request
* @return
*/
@RequestMapping(value = "/doDeleteById", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
public BaseRespResult doDeleteById(BaseMethodArgument param, HttpServletRequest request) {
//取得本次数据的主键
int count = 0;
String[] Ids = getStringParameter(request, "ids").split(",");
String tableCode = param.getTableCode();
for (String id : Ids) {
count = metaService.delete(tableCode, ConditionsWrapper.builder().eq("JE_DEMO_JKDY_ID", id));
}
return BaseRespResult.successResult(String.format("%s 条记录被删除", count));
}
/**
* 根据条件修改数据
* @param request
* @return
*/
@ResponseBody
@RequestMapping(value = "/doDemoUpdate", method = RequestMethod.POST)
public BaseRespResult doDemoUpdate(BaseMethodArgument param, HttpServletRequest request) {
//获取主键
String JE_DEMO_JKDY_ID = getStringParameter(request, "JE_DEMO_JKDY_ID");
//获取要修改的数据
String JKDY_XMMC = getStringParameter(request, "JKDY_XMMC");
String JKDY_XMGS = getStringParameter(request, "JKDY_XMGS");
//获取表名
String tableCode = param.getTableCode();
//根据主键查询查询当前修改数据
DynaBean dynaBean = metaService.selectOne(tableCode, ConditionsWrapper.builder().eq("JE_DEMO_JKDY_ID", JE_DEMO_JKDY_ID));
dynaBean.setStr("JKDY_XMMC", JKDY_XMMC);
dynaBean.setStr("JKDY_XMGS", JKDY_XMGS);
metaService.update(dynaBean);
return BaseRespResult.successResult(dynaBean);
}
/**
* 查询数据
* @return
*/
@ResponseBody
@RequestMapping(value = "/getSelectData", method = RequestMethod.POST)
public BaseRespResult getSelectData(BaseMethodArgument param, HttpServletRequest request) {
//获取表名
String tableCode = param.getTableCode();
String JKDY_XMMC = getStringParameter(request, "JKDY_XMMC");
//如果有参数 就根据参数查,如果无参数 就查所有数据
if (StringUtil.isNotEmpty(JKDY_XMMC)) {
List<DynaBean> list = metaService.select(tableCode, ConditionsWrapper.builder().eq("JKDY_XMMC", JKDY_XMMC));
return BaseRespResult.successResult(list);
}
List<Map<String, Object>> list1 = metaService.selectSql("SELECT * FROM JE_DEMO_JKDY");
return BaseRespResult.successResult(list1);
}
/**
* 获取当前用户信息
* @return
*/
@ResponseBody
@RequestMapping(value = "/getCurrentUser", method = RequestMethod.POST)
public BaseRespResult getCurrentUser() {
return BaseRespResult.successResult(SecurityUserHolder.getCurrentAccount());
}
/**
* 获取系统设置的数据
* @return
*/
@ResponseBody
@RequestMapping(value = "/getSystemSetting", method = RequestMethod.POST)
public BaseRespResult getSystemSetting() {
Map<String, String> maplist = systemSettingRpcService.requireAllSettingValue();
return BaseRespResult.successResult(maplist);
}
}
5、接口在平台上如何调用
在第一个服务下创建功能,在功能上加按钮,进行按钮调用,实现前后联动
//新增数据
const {
$func, // 功能对象
button, // 按钮对象
} = EventOptions;
const grid = $func.getFuncGrid();
JE.ajax({
url: '/je/demo/doDemoSave',
params: {
funcCode: "JE_DEMO_JKDY",
tableCode: "JE_DEMO_JKDY",
JKDY_XMMC:"低代码平台开发",
JKDY_XMZT_CODE:'ZXZ',
JKDY_XMZT_NAME:'执行中',
JKDY_XMKSSJ:JE.dateFormat(new Date(),'YYYY-MM-DD'),
JKDY_XMJSSJ:JE.dateFormat(new Date(),'YYYY-MM-DD'),
JKDY_XMGS:'低代码平台开发',
codeGenFieldInfo:JSON.stringify([{"code":"JKDY_XMBH","configInfo":"[{\"qzfs\":\"CL\",\"dyz\":\"XM\",\"gs\":\"\",\"jqws\":\"\",\"cd\":\"\",\"qsh\":\"\",\"bc\":\"\",\"zq\":\"\",\"index\":1},{\"qzfs\":\"LSH\",\"dyz\":\"FUNC\",\"gs\":\"\",\"jqws\":\"\",\"cd\":4,\"qsh\":\"0001\",\"bc\":1,\"zq\":\"YEAR\",\"index\":2}]","funcId":"DnYo9kwoTFT5fCInMKe","funcCode":"JE_DEMO_JKDY","funcName":"接口调运案例","tableCode":"JE_DEMO_JKDY"}])
},
headers: {pd:"demo"}
}).then((data)=>{
console.log(data);
if(data.success){
grid.store.reload();
JE.alert(data.message,"success");
}else{
JE.alert(data.message,"error");
}
});
//删除数据
const {
$func, // 功能对象
button, // 按钮对象
} = EventOptions;
//获取选中的数据
const records = $func.getFuncGrid().getSelectedRecords();
const grid = $func.getFuncGrid();
if(records && records.length>0){
//封装id
const delids = [];
records.forEach((item)=>{
delids.push(item.JE_DEMO_JKDY_ID);
});
JE.ajax({
url:'/je/demo/doDeleteById',
// contentType:"application/json",
params:{
tableCode:"JE_DEMO_JKDY",
funcCode:"JE_DEMO_JKDY",
ids:delids.join(",")
},
headers:{pd:"demo"}
}).then((data)=>{
if(data.success){
// 清除列表选中的数据
grid.clearSelectedRecords();
//刷新列表
grid.store.reload();
JE.alert(data.message, "success");
}else{
JE.alert(data.message, "error");
}
})
}else{
JE.alert('请先选择需要删除的数据','info');
}
//修改数据
const {
$func, // 功能对象
button, // 按钮对象
} = EventOptions;
//获取选中的数据
const records = $func.getFuncGrid().getSelectedRecords();
const grid = $func.getFuncGrid();
if(records && records.length==1){
JE.ajax({
url:"/je/demo/doDemoUpdate",
params:{
funcCode: "JE_DEMO_JKDY",
tableCode: "JE_DEMO_JKDY",
pkCode:"JE_DEMO_JKDY_ID",
JE_DEMO_JKDY_ID:records[0].JE_DEMO_JKDY_ID,
JKDY_XMMC:'JECloud低代码平台开发',
JKDY_XMGS:'JECloud低代码平台开发',
},
headers:{pd:"demo"}
}).then((data)=>{
if(data.success){
// 清除列表选中的数据
grid.clearSelectedRecords();
//刷新数据
grid.store.reload();
// 弹窗成功提示
JE.alert(data.message, "success");
}else{
JE.alert(data.message, "error");
}
});
}else{
JE.alert('请先选择需要修改的一条数据','info');
}
//查询数据
const {
$func, // 功能对象
button, // 按钮对象
} = EventOptions;
/**
* JKDY_XMMC 参数作为条件进行查询,不传查全部数据,传参查符合条件的
*/
JE.ajax({
url:"/je/demo/getSelectData",
params:{
tableCode:"JE_DEMO_JKDY",
funcCode:"JE_DEMO_JKDY"
,JKDY_XMMC:"JECloud低代码平台开发"
},
headers:{pd:"demo"}
}).then((data)=>{
if(data.success){
console.log(data.data);
JE.alert("查询成功!数据请打开F12在console中查看","success");
}else{
JE.alert(data.message,"error");
}
});
//获取系统变量
const {
$func, // 功能对象
button, // 按钮对象
} = EventOptions;
JE.ajax({
url:"/je/meta/setting/loadFrontendSetting",
headers:{pd:"meta"}
}).then((data)=>{
if(data.success){
console.log(data.data);
JE.alert("查询成功!数据请打开F12在console中查看","success");
}else{
JE.alert(data.message,"error");
}
});
//获取系统设置数据
const {
$func, // 功能对象
button, // 按钮对象
} = EventOptions;
// 异步事件,支持 await语法,实现同步处理。
JE.ajax({
url:"/je/demo/getSystemSetting",
headers:{pd:"demo"}
}).then((resObj)=>{
console.log(resObj);
});
//获取当前登录人信息
const {
$func, // 功能对象
button, // 按钮对象
} = EventOptions;
JE.ajax({
url:"/je/demo/getCurrentUser",
headers:{pd:"demo"}
}).then((resObj)=>{
if(resObj.success){
console.log(resObj);
JE.alert("获取当前用户信息成功!请在F12控制台查看!","success");
}else{
JE.alert(resObj.message,"error");
}
});
最后编辑: 呼丽华 文档更新时间: 2025-01-15 16:00 作者:呼丽华