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()+"条数据" ;
}
复制代码
这样在操作日志切入类中根据返回值类型就能拿到实际的操作结果
相关推荐
想摆烂的不会研究的研究生1 小时前
每日八股——Redis(1)
数据库·经验分享·redis·后端·缓存
毕设源码-郭学长1 小时前
【开题答辩全过程】以 基于SpringBoot技术的美妆销售系统为例,包含答辩的问题和答案
java·spring boot·后端
N***H4862 小时前
springcloud springboot nacos版本对应
spring boot·spring·spring cloud
追逐时光者2 小时前
精选 10 款 .NET 开源免费、功能强大的 Windows 效率软件
后端·.net
追逐时光者2 小时前
一款开源、免费的 WPF 自定义控件集
后端·.net
S***q3773 小时前
Spring Boot管理用户数据
java·spring boot·后端
BD_Marathon3 小时前
SpringBoot——辅助功能之切换web服务器
服务器·前端·spring boot
毕设源码-郭学长3 小时前
【开题答辩全过程】以 基于SpringBoot框架的民俗文化交流与交易平台的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
l***21783 小时前
SpringBoot Maven快速上手
spring boot·后端·maven
f***14774 小时前
SpringBoot实战:高效实现API限流策略
java·spring boot·后端