背景: 因为比较喜欢Layui,因为多个项目都是从零开始就使用的layui开发的,并且开发过程中借鉴了很多其他项目(如Ruoyi、Pear Admin),因此最终选用大部分Pear Admin的项目中使用的一系列解决方案,并再次记录一些对开发非常有帮助的一些用例
1. layui框架表格默认接收格式,以及默认的分页规则(为了方便开发,节省代码,因此统一列表返回值,为了节省代码使用 PageHelper作为分页插件)
Controller.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
public PageInfo<SysLog> getLogList(SysLog s) {PageHelper.startPage(s.getPage(), s.getLimit());List<SysLog> list = logDao.getLogList(s);return new PageInfo<>(list); //操作日志中专门解析了分页插件获取的数据}
mapper.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) >= substring(#{createTime}, 0, 11)" //数据库中的时间10位之前即可,前台传的有空格所以是11+ " AND substring(createTime::text, 0, 11) <= 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); //查询操作日志}
2. 操作日志(获取接口返回值时,因为等保的原因公司规定需要把每一项操作的结果返回,因此这时就体现出了统一返回值的好处了)
if (result instanceof ResultTable) {//分页插件返回layui 格式数据returnResult = operType+"了" + ((ResultTable) result).getCount()+"条数据" ;
}
这样在操作日志切入类中根据返回值类型就能拿到实际的操作结果