springboot项目使用Layui作为前端UI的一系列前后端交互的解决方法

背景: 因为比较喜欢Layui,因为多个项目都是从零开始就使用的layui开发的,并且开发过程中借鉴了很多其他项目(如Ruoyi、Pear Admin),因此最终选用大部分Pear Admin的项目中使用的一系列解决方案,并再次记录一些对开发非常有帮助的一些用例

  1. layui框架表格默认接收格式,以及默认的分页规则(为了方便开发,节省代码,因此统一列表返回值,为了节省代码使用 PageHelper作为分页插件)

Controller.java

java 复制代码
@SysLogAnnotation(operModul = "日志查询", operType = "查询", operDesc = "操作日志查询")
	@RequestMapping("/getSysLog")
	public ResultTable getSysLog(SysLog log) {
		PageInfo<SysLog> pageInfo = logService.getLogList(log);
		return pageTable(pageInfo.getList(), pageInfo.getTotal());
	}

serviceImpl.java

java 复制代码
public PageInfo<SysLog> getLogList(SysLog s) {
        PageHelper.startPage(s.getPage(), s.getLimit());
        List<SysLog> list = logDao.getLogList(s);
        return new PageInfo<>(list); //操作日志中专门解析了分页插件获取的数据
    }

mapper.java

java 复制代码
import java.util.List;

@Mapper   //要在java中写sql就需要这个注解, 如果使用mybatis plus的基础方法那 @Repository一个注解就搞定
@Repository
public interface LogMapper{

    @Select({"<script>select * from sys_log where 1=1  "
            + " <if test='createTime !=null and createTime != \"\" '>"
            + "    AND substring(createTime::text, 0, 11) &gt;= substring(#{createTime}, 0, 11)" //数据库中的时间10位之前即可,前台传的有空格所以是11
            + "    AND substring(createTime::text, 0, 11) &lt;= substring(#{createTime}, 14)"
            + " </if>"
            + " <if test='type !=null and type != \"\" '> AND type = #{type} </if> "
            + " <if test='result !=null and result != \"\" '> AND result like '%'|| #{result}||'%' </if> "
            + "<if test='username !=null and username != \"\" '> AND username like '%'|| #{username}||'%' </if> "
            + "order by createtime desc </script>"})
    List<SysLog> getLogList(SysLog l);  //查询操作日志

}
  1. 操作日志(获取接口返回值时,因为等保的原因公司规定需要把每一项操作的结果返回,因此这时就体现出了统一返回值的好处了)
java 复制代码
if (result instanceof ResultTable) {//分页插件返回layui 格式数据
            returnResult = operType+"了" + ((ResultTable) result).getCount()+"条数据" ;
}
复制代码
这样在操作日志切入类中根据返回值类型就能拿到实际的操作结果
相关推荐
ai小鬼头17 分钟前
百度秒搭发布:无代码编程如何让普通人轻松打造AI应用?
前端·后端·github
考虑考虑18 分钟前
@FilterRegistration和@ServletRegistration注解
spring boot·后端·spring
一只叫煤球的猫21 分钟前
🔥 同事混用@Transactional和TransactionTemplate被我怼了,三种事务管理到底怎么选?
java·spring boot·后端
你的人类朋友9 天前
(●'◡'●)从Dockerfile快速入门Docker Compose
后端
GetcharZp9 天前
「神器推荐」Rclone:轻松玩转云端存储,FTP 也能飞起来!
后端
华子w9089258599 天前
基于 SpringBoot+JSP 的医疗预约与诊断系统设计与实现
java·spring boot·后端
舒一笑9 天前
工作流会使用到Webhook是什么
后端·程序员
止观止9 天前
Rust智能指针演进:从堆分配到零复制的内存管理艺术
开发语言·后端·rust
学無芷境9 天前
Cargo 与 Rust 项目
开发语言·后端·rust
ai小鬼头9 天前
AIStarter开发者熊哥分享|低成本部署AI项目的实战经验
后端·算法·架构