rest开发步骤
rest开发步骤
1)代码工厂自动生成REST CRUD代码。
自动生成的代码:
unit rest.tunit; //代码由代码工厂自动生成 //2023-01-28 13:20:07 {$I def.inc} interface uses {$IFDEF firedac} db.firedac, db.firedacPool, {$ENDIF} {$IFDEF unidac}db.unidac, db.unidacpool, {$ENDIF} classes, db, System.NetEncoding, serialize, rtti.execfunc, system.JSON.Serializers, yn.log, SysUtils; type Ttunit = record [Serialize(1)] unitid: string; [Serialize(2)] unitname: string; end; TtunitArray = record [Serialize(1)] status: integer; [Serialize(2)] exception: string; [Serialize(3)] message: string; [Serialize(4)] tunits: TArray<Ttunit>; end; TRes = record [Serialize(1)] status: integer; [Serialize(2)] exception: string; [Serialize(3)] message: string; end; type TFunc2872 = class(TFunc) function select(url: string; body: rawbytestring): string; function insert(url: string; body: rawbytestring): string; function update(url: string; body: rawbytestring): string; function delete(url: string; body: rawbytestring): string; end; implementation function TFunc2872.select(url: string; body: rawbytestring): string; var db: tdb; pool: tdbpool; rows: TtunitArray; i: integer; res: TRes; begin try try pool := GetDBPool('1'); db := pool.Lock; db.qry.Close; db.qry.SQL.Clear; db.qry.SQL.Text := 'select * from tunit'; db.qry.Open; SetLength(rows.tunits, db.qry.RecordCount); db.qry.First; i := 0; while not db.qry.Eof do begin rows.tunits[i].unitid := db.qry.fieldbyname('unitid').asstring; rows.tunits[i].unitname := db.qry.fieldbyname('unitname').asstring; inc(i); db.qry.Next; end; rows.status := 200; rows.message := 'success'; result := Tserial.marshal<TtunitArray>(rows); except on E: Exception do begin res.status := 500; res.exception := E.message; result := Tserial.marshal<TRes>(res); end; end; finally pool.Unlock(db); end; end; function TFunc2872.insert(url: string; body: rawbytestring): string; var db: tdb; pool: tdbpool; arr: tarray<string>; res: TRes; begin try try var rows: TtunitArray; rows := Tserial.unmarshal<TtunitArray>(body); arr := url.Split(['/']); pool := GetDBPool('1'); db := pool.Lock; db.startTrans; for var row: Ttunit in rows.tunits do begin db.qry.Close; db.qry.SQL.Clear; db.qry.SQL.Text := 'insert into tunit (unitid,unitname) values (:unitid,:unitname)'; db.qry.ParamByName('unitid').AsString := row.unitid; db.qry.ParamByName('unitname').AsString := row.unitname; db.qry.ExecSQL; end; db.commitTrans; res.status := 200; res.message := 'success'; Result := Tserial.marshal<TRes>(res); except on E: Exception do begin db.rollbackTrans; res.status := 500; res.exception := E.Message; Result := Tserial.marshal<TRes>(res); end; end; finally pool.Unlock(db); end; end; function TFunc2872.update(url: string; body: rawbytestring): string; var db: tdb; pool: tdbpool; arr: tarray<string>; res: TRes; begin try try var rows: TtunitArray; rows := Tserial.unmarshal<TtunitArray>(body); arr := url.Split(['/']); pool := GetDBPool('1'); db := pool.Lock; db.startTrans; for var row: Ttunit in rows.tunits do begin db.qry.Close; db.qry.SQL.Clear; db.qry.SQL.Text := 'update tunit set unitid=:unitid,unitname=:unitname where unitid=:key0'; db.qry.ParamByName('unitid').AsString := row.unitid; db.qry.ParamByName('key0').value := row.unitid; db.qry.ParamByName('unitname').AsString := row.unitname; db.qry.ExecSQL; end; db.commitTrans; res.status := 200; res.message := 'success'; Result := Tserial.marshal<TRes>(res); except on E: Exception do begin db.rollbackTrans; res.status := 500; res.exception := E.Message; Result := Tserial.marshal<TRes>(res); end; end; finally pool.Unlock(db); end; end; function TFunc2872.delete(url: string; body: rawbytestring): string; var db: tdb; pool: tdbpool; arr: tarray<string>; res: TRes; begin try try arr := url.Split(['/']); pool := GetDBPool('1'); db := pool.Lock; db.qry.Close; db.qry.SQL.Clear; var where: string := ' where ' + TNetEncoding.URL.Decode(arr[3]); db.qry.SQL.Text := 'delete from tunit' + where; db.qry.ExecSQL; res.status := 200; res.message := 'success'; Result := Tserial.marshal<TRes>(res); except on E: Exception do begin res.status := 500; res.exception := E.Message; Result := Tserial.marshal<TRes>(res); end; end; finally pool.Unlock(db); end; end; initialization RegisterClass(TFunc2872); end.
2)添加路由配置
3)将自动生成的单元添加进工程里面,并编译工程。
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/17070169.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?