Authored by mlge

监控----sql操作日志

package com.monitor.model.request;
import com.monitor.model.page.PageRequest;
import lombok.Data;
/**
* Created by meiling.ge on 2017/9/21.
*/
@Data
public class SqlOpLogsReq extends PageRequest {
private String userName;
}
... ...
package com.monitor.mysql.mapper;
import com.model.SqlOperateLogs;
import com.monitor.model.domain.PageBean;
import com.monitor.model.domain.SqlOperateModel;
import java.util.List;
/**
* Created by craig.qinchao on 2017/06/29.
... ... @@ -8,4 +12,8 @@ import com.model.SqlOperateLogs;
public interface SqlOperateLogsMapper {
int insert(SqlOperateLogs sqlOperateLogs);
int selectCount(PageBean page);
List<SqlOperateModel> selectPageByParams(PageBean page);
}
... ...
... ... @@ -35,4 +35,21 @@
now())
</insert>
<select id="selectCount" resultType="java.lang.Integer">
select count(*) from sql_operate_logs
<if test="params.userName != null &amp;&amp; params.userName != ''">
where user_name like CONCAT('%',#{params.userName,jdbcType=VARCHAR},'%')
</if>
order by create_time desc
</select>
<select id="selectPageByParams" resultMap="BaseResultMap">
select * from sql_operate_logs
where 1=1
<if test="params.userName != null &amp;&amp; params.userName != ''">
and user_name like CONCAT('%',#{params.userName,jdbcType=VARCHAR},'%')
</if>
order by create_time desc
limit #{startIndex},#{pageSize}
</select>
</mapper>
\ No newline at end of file
... ...
package com.monitor.other.sqlOperate.ctrl;
import com.monitor.model.domain.SqlOperateModel;
import com.monitor.model.request.SqlOpLogsReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.PageResponse;
import com.monitor.other.sqlOperate.service.SqlOperateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
... ... @@ -49,4 +51,12 @@ public class SqlOperateCtrl {
}
return res;
}
@RequestMapping("/queryLogs")
@ResponseBody
public BaseResponse<PageResponse<SqlOperateModel>> querySqlLogs(@RequestBody SqlOpLogsReq req){
BaseResponse<PageResponse<SqlOperateModel>> resp = sqlOperateService.queryLogsByPage(req);
return resp;
}
}
... ...
package com.monitor.other.sqlOperate.service;
import com.monitor.model.domain.SqlOperateModel;
import com.monitor.model.request.SqlOpLogsReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.PageResponse;
import java.util.List;
import java.util.Map;
/**
... ... @@ -32,4 +35,5 @@ public interface SqlOperateService {
BaseResponse queryDbList(SqlOperateModel sqlOperateModel);
BaseResponse<PageResponse<SqlOperateModel>> queryLogsByPage(SqlOpLogsReq req);
}
... ...
... ... @@ -3,8 +3,11 @@ package com.monitor.other.sqlOperate.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.model.SqlOperateLogs;
import com.monitor.model.domain.PageBean;
import com.monitor.model.domain.SqlOperateModel;
import com.monitor.model.request.SqlOpLogsReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.PageResponse;
import com.monitor.mysql.mapper.SqlOperateLogsMapper;
import com.monitor.other.sqlOperate.service.SqlOperateService;
import org.apache.commons.lang3.StringUtils;
... ... @@ -12,6 +15,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.sql.*;
import java.util.ArrayList;
... ... @@ -490,6 +494,39 @@ public class SqlOperateServiceImpl implements SqlOperateService {
}
}
/**
* 分页查询sql操作日志
* @param request
* @return
*/
@Override
public BaseResponse<PageResponse<SqlOperateModel>> queryLogsByPage(SqlOpLogsReq request) {
try{
PageBean page = PageBean.initPageInfo(request.getCurrentPage(), request.getPageSize(), request);
// 先查询符合条件的总数量
int total = sqlOperateLogsMapper.selectCount(page);
// 数量为0 直接返回
if (total == 0) {
return new BaseResponse<>();
}
// 获取列表
List<SqlOperateModel> sqlOperateModels = sqlOperateLogsMapper.selectPageByParams(page);
if (CollectionUtils.isEmpty(sqlOperateModels)) {
return new BaseResponse<>();
}
PageResponse<SqlOperateModel> pageResp = new PageResponse<>();
pageResp.setCurrentPage(request.getCurrentPage());
pageResp.setPageSize(request.getPageSize());
pageResp.setTotal(total);
pageResp.setRows(sqlOperateModels);
return new BaseResponse<>(pageResp);
}catch(Exception e){
log.error("queryLogsByPage error", e);
return new BaseResponse<>(e.getMessage());
}
}
/* public static void main(String[] args){
... ...