中间件新增一个业务包
中间件新增一个业务包
1)新增一个package包
打开中间件工程组文件,使用delphi ide package包向导新建一个包。

设置包的路径:

设置为运行时包:

设置引用核心包:

新建一个业务单元:

业务接口样例:
type TDanWei = record procedure Select(AContext: TContext); procedure Insert(AContext: TContext); procedure Update(AContext: TContext); procedure Delete(AContext: TContext); end;
注册API路由:
var _: TDanWei; initialization Post('/danwei/select', _.Select); Post('/danwei/insert', _.Insert); Post('/danwei/update', _.Update); Post('/danwei/delete', _.Delete);
附上一个完整的业务单元:
unit table.crud; // cxg 2025-4-22 interface uses core.firedac, core.global, core.firedacpool, core.router, core.log, core.json, core.dbjson, firedac.Stan.Param, DB, Classes, SysUtils; type //业务接口声明 TDanWei = record procedure Select(AContext: TContext); procedure Insert(AContext: TContext); procedure Update(AContext: TContext); procedure Delete(AContext: TContext); end; implementation { Tdanwei } procedure TDanWei.Select(AContext: TContext); var LDB: TDB; LPool: TDBPool; LRequest, LResponse: TJsonO; begin LRequest := SO(AContext.InContent); if LRequest = nil then Exit; LResponse := TJsonO.Create; try try LPool := GetDBPool(LRequest.S[TConst.DBId]); LDB := LPool.Lock; LDB.Select('select * from tunit'); LResponse.B[TConst.Success] := True; LResponse.S[TConst.Msg] := '查询成功'; LResponse.A[TConst.Fields] := LDB.qry.FieldsToJsonArray; LResponse.A[TConst.Data] := LDB.qry.DataToJsonArray; AContext.Send(LResponse); except on E: Exception do begin LResponse.B[TConst.Success] := False; LResponse.S[TConst.Msg] := E.Message; AContext.Send(LResponse); WriteLog('TDanWei.Select()' + E.Message); end; end; finally LPool.Unlock(LDB); LResponse.Free; LRequest.Free; end; end; procedure TDanWei.Delete(AContext: TContext); var LDB: TDB; LPool: TDBPool; LRequest, LResponse, LRow: TJsonO; LData: TJsonA; i: Integer; begin LRequest := SO(AContext.InContent); if LRequest = nil then Exit; LResponse := TJsonO.Create; try try LPool := GetDBPool(LRequest.S[TConst.DBId]); LDB := LPool.Lock; LData := LRequest.A['dataset0']; LDB.StartTrans; for i := 0 to LData.Count - 1 do begin LRow := TJsonO(LData.Items[i]); LDB.qry.Close; LDB.qry.SQL.Clear; LDB.qry.SQL.Text := 'delete from tunit where unitid=:unitid'; LDB.qry.ParamByName('unitid').AsString := LRow.S['unitid']; LDB.qry.ExecSQL; end; LDB.CommitTrans; LResponse.B[TConst.Success] := True; LResponse.S[TConst.Msg] := '删除成功'; AContext.Send(LResponse); except on E: Exception do begin LDB.RollbackTrans; LResponse.B[TConst.Success] := False; LResponse.S[TConst.Msg] := E.Message; AContext.Send(LResponse); WriteLog('TDanWei.Delete()' + E.Message); end; end; finally LPool.Unlock(LDB); LResponse.Free; LRequest.Free; end; end; procedure TDanWei.Insert(AContext: TContext); var LDB: TDB; LPool: TDBPool; LRequest, LResponse, LRow: TJsonO; LData: TJsonA; i: Integer; begin LRequest := SO(AContext.InContent); if LRequest = nil then Exit; LResponse := TJsonO.Create; try try LPool := GetDBPool(LRequest.S[TConst.DBId]); LDB := LPool.Lock; LData := LRequest.A['dataset0']; LDB.StartTrans; for i := 0 to LData.Count - 1 do begin LRow := TJsonO(LData.Items[i]); LDB.qry.Close; LDB.qry.SQL.Clear; LDB.qry.SQL.Text := 'insert into tunit(unitid,unitname) values (:unitid,:unitname)'; LDB.qry.ParamByName('unitid').AsString := LRow.S['unitid']; LDB.qry.ParamByName('unitname').AsString := LRow.S['unitname']; LDB.qry.ExecSQL; end; LDB.CommitTrans; LResponse.B[TConst.Success] := True; LResponse.S[TConst.Msg] := '新增成功'; AContext.Send(LResponse); except on E: Exception do begin LDB.RollbackTrans; LResponse.B[TConst.Success] := False; LResponse.S[TConst.Msg] := E.Message; AContext.Send(LResponse); WriteLog('TDanWei.Insert()' + E.Message); end; end; finally LPool.Unlock(LDB); LResponse.Free; LRequest.Free; end; end; procedure TDanWei.Update(AContext: TContext); var LDB: TDB; LPool: TDBPool; LRequest, LResponse, LRow: TJsonO; LData: TJsonA; i: Integer; begin LRequest := SO(AContext.InContent); if LRequest = nil then Exit; LResponse := TJsonO.Create; try try LPool := GetDBPool(LRequest.S[TConst.DBId]); LDB := LPool.Lock; LData := LRequest.A['dataset0']; LDB.StartTrans; for i := 0 to LData.Count - 1 do begin LRow := TJsonO(LData.Items[i]); LDB.qry.Close; LDB.qry.SQL.Clear; LDB.qry.SQL.Text := 'update tunit set unitid=:unitid,unitname=:unitname where unitid=:key'; LDB.qry.ParamByName('unitid').AsString := LRow.S['unitid']; LDB.qry.ParamByName('unitname').AsString := LRow.S['unitname']; LDB.qry.ParamByName('key').AsString := LRow.S['unitid']; LDB.qry.ExecSQL; end; LDB.CommitTrans; LResponse.B[TConst.Success] := True; LResponse.S[TConst.Msg] := '修改成功'; AContext.Send(LResponse); except on E: Exception do begin LDB.RollbackTrans; LResponse.B[TConst.Success] := False; LResponse.S[TConst.Msg] := E.Message; AContext.Send(LResponse); WriteLog('TDanWei.Update()' + E.Message); end; end; finally LPool.Unlock(LDB); LResponse.Free; LRequest.Free; end; end; //注册API路由 var _: TDanWei; initialization Post('/danwei/select', _.Select); Post('/danwei/insert', _.Insert); Post('/danwei/update', _.Update); Post('/danwei/delete', _.Delete); end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/18853372

浙公网安备 33010602011771号