1.目录结构
2.创建分页Bean
packagecom.ruoyi.web.controller.common; importorg.ssssssss.magicapi.model.JsonCode; /*** @ClassName JsonBackBean* @Author Lin* @Date 2021/7/16 10:55 AM* @Version V1.0* @Description**/publicclassPageJsonBackBean<T> { privateintcode; privatelongtotal; privateStringmsg; privateTrows; privatelongtimestamp; privateIntegerexecuteTime; publicPageJsonBackBean(intcode, Stringmsg) { this.code=1; this.msg="success"; this.timestamp=System.currentTimeMillis(); this.code=code; this.msg=msg; } publicPageJsonBackBean(intcode, Stringmsg, Trows, IntegerexecuteTime) { this(code, msg, rows); this.executeTime=executeTime; } publicPageJsonBackBean(longtotal,intcode, Stringmsg, Trows, IntegerexecuteTime) { this.total=total; this.code=1; this.msg="success"; this.timestamp=System.currentTimeMillis(); this.code=code; this.msg=msg; this.rows=rows; this.executeTime=executeTime; } publicPageJsonBackBean(intcode, Stringmsg, Trows) { this.code=1; this.msg="success"; this.timestamp=System.currentTimeMillis(); this.code=code; this.msg=msg; this.rows=rows; } publicPageJsonBackBean() { this.code=1; this.msg="success"; this.timestamp=System.currentTimeMillis(); } publicPageJsonBackBean(JsonCodejsonCode) { this(jsonCode, (T) null); } publicPageJsonBackBean(JsonCodejsonCode, Trows) { this(jsonCode.getCode(), jsonCode.getMessage(), rows); } publicPageJsonBackBean(Trows) { this.code=1; this.msg="success"; this.timestamp=System.currentTimeMillis(); this.rows=rows; } publiclonggetTotal() { returntotal; } publicvoidsetTotal(longtotal) { this.total=total; } publicintgetCode() { returnthis.code; } publicvoidsetCode(intcode) { this.code=code; } publicStringgetMsg() { returnthis.msg; } publicvoidsetMsg(Stringmsg) { this.msg=msg; } publicTgetrows() { returnthis.rows; } publicvoidsetrows(Trows) { this.rows=rows; } publiclonggetTimestamp() { returnthis.timestamp; } publicvoidsetTimestamp(longtimestamp) { this.timestamp=timestamp; } publicIntegergetExecuteTime() { returnthis.executeTime; } publicvoidsetExecuteTime(IntegerexecuteTime) { this.executeTime=executeTime; } }
2.自定义json返回结果
packagecom.ruoyi.web.controller.provider; importcom.ruoyi.web.controller.common.JsonBackBean; importcom.ruoyi.web.controller.common.PageJsonBackBean; importorg.springframework.stereotype.Component; importorg.ssssssss.magicapi.model.JsonBean; importorg.ssssssss.magicapi.model.JsonCodeConstants; importorg.ssssssss.magicapi.model.PageResult; importorg.ssssssss.magicapi.model.RequestEntity; importorg.ssssssss.magicapi.provider.ResultProvider; /*** @Author Lin* @Description 自定义返回结果* @Date 2021/7/16 4:16 PM* @Param * @return **/publicclassCustomJsonValueProviderimplementsResultProvider { /*** 构建请求结果* @param requestEntity 请求相关信息* @param code 状态码* @param message 状态说明* @param data 数据内容,可以通过data的类型判断是否是分页结果进行区分普通结果集和分页结果集* @return*/publicObjectbuildResult(RequestEntityrequestEntity, intcode, Stringmessage, Objectdata) { // 分页结果if (datainstanceofPageResult) { PageResult<?>pageResult= (PageResult<?>) data; longtotal=pageResult.getTotal(); returnnewPageJsonBackBean(total,0,message, pageResult.getList(),1); } // if (code == JsonCodeConstants.SUCCESS.getCode()) {// return new JsonBackBean<>(0, message, data);// }returnnewJsonBean<>(code, message, data); } }
3.自定义分页
packagecom.ruoyi.web.controller.provider; /*** @ClassName MyPageProvider* @Author Lin* @Date 2021/7/16 11:07 AM* @Version V1.0* @Description **/importorg.springframework.stereotype.Component; importorg.ssssssss.magicapi.model.Page; importorg.ssssssss.magicapi.provider.PageProvider; importorg.ssssssss.script.MagicScriptContext; /*** 分页对象默认提取接口*/publicclassMyPageProviderimplementsPageProvider { /*** 此方法需要根据实际情况替换*/publicPagegetPage(MagicScriptContextcontext) { longpage=Long.valueOf(context.get("pageNum").toString()); longpageSize=Long.valueOf(context.get("pageSize").toString()); // 计算limit以及offsetreturnnewPage(pageSize, (page-1) *pageSize); } }
4.增加magic-api swagger配置
#Swagger配置swagger: #是否开启swaggerenabled: truemagic-api: web: /magic/webresource: type: database#配置接口存储方式,这里选择存在数据库中table-name: magic_api_file#数据库中的表名prefix: /magic-api#前缀swagger-config: #资源名称name: MagicAPI#资源描述description: MagicAPIv1.0接口文档#资源位置location: /v2/api-docs/magic-api/swagger2.json#文档版本version: 1.0#文档标题title: MagicAPIv1.0接口文档
5.magic-api接口增删改查demo
新增:
varsql="INSERT INTO cgx_xcx_user( country, province, city, gender, nick_name, phone, wx_num, type, time, scqymc, tyshxydm, fadbr, idstatus, user_name) VALUES (#{country},#{province},#{city},#{gender},#{nickName},#{phone},#{wxNum},#{type},#{time},#{scqymc},#{tyshxydm},#{fadbr},#{idstatus},#{userName})"vari=db.update(sql) returnresponse.json({ code : 0, msg : '新增成功', result : i});
删除:
//#{id}表示从请求参数或PathVariable中获取参数varsql="delete from cgx_xcx_user where id in (${ids})"; vari=db.update(sql) returnresponse.json({ code : 0, msg : '删除成功', result : i});
编辑:
varsql="UPDATE cgx_xcx_user SET country = #{country}, province = #{province}, city = #{city}, gender = #{gender}, nick_name = #{nickName}, phone = #{phone}, wx_num = #{wxNum}, type = #{type}, time = #{time}, scqymc = #{scqymc}, tyshxydm = #{tyshxydm}, fadbr = #{fadbr}, idstatus = #{idstatus}, user_name = #{userName} WHERE id = #{id}"vari=db.update(sql) returnresponse.json({ code : 0, msg : '更新成功', result : i});
分页查询: 分页用db.page(sql) 不分页用db.sql(sql),传参:pageNum当前页面,pageSize 显示条数
varsql="select * from cgx_xcx_user"; varlist=db.page(sql); //返回Listreturnlist
6.打开若依项目生成代码菜单按如下配置生成代码,zip可生成数据库菜单sql
7.修改生成的前端页面保证和后台magic-api接口访问路径统一
8.测试功能正常,数据库表字段推荐驼峰命名法
9.查看接口生成文档
http://127.0.0.1:5200/swagger-ui/index.html