SSM框架把日志信息保存到數(shù)據(jù)庫(kù)過(guò)程詳解
1)在service層和mapper層中寫一個(gè)插入方法和查詢方法;
我們先寫一個(gè)日志類;定義屬性;并且要在數(shù)據(jù)庫(kù)中建好表;
package entity; public class Log { private Integer id; private Integer logtype; private String description; private String param; public Log(){ } public Log(Integer id, Integer logtype, String description, String param) { this.id = id; this.logtype = logtype; this.description = description; this.param = param; } @Override public String toString() { return "Log{" + "id=" + id + ", logtype=" + logtype + ", description='" + description + '\'' + ", param='" + param + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getLogtype() { return logtype; } public void setLogtype(Integer logtype) { this.logtype = logtype; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getParam() { return param; } public void setParam(String param) { this.param = param; } }
該寫方法了
1、logService.java頁(yè)面;
public interface LogService { int insert(Log log); List<Log> findAll(); }
2、logServiceImpl.java頁(yè)面;
@Service public class LogServiceImpl implements LogService { @Autowired private LogMapper logMapper; @Override public int insert(Log log) { int i=logMapper.insert(log); return i; } @Override public List<Log> findAll() { List<Log> logs=logMapper.findAll(); return logs; } }
3、logMapper.java頁(yè)面:
public interface LogMapper { int insert(Log log); List<Log> findAll(); }
4、logMapper.xml頁(yè)面;
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="mapper.LogMapper"> <insert id="insert"> <selectKey keyProperty="id" resultType="integer" order="BEFORE"> select seq_logaspect.nextval from dual </selectKey> insert into logaspect(id,logtype,description,param) values (#{id},#{logtype},#{description},#{param}) </insert> <select id="findAll" resultType="entity.Log"> select * from logaspect </select> </mapper>
5、由于我們打印日志是通過(guò)切面,所以我們寫一個(gè)切面類;
package aop; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import entity.Log; import entity.Student; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import service.LogService; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.Map; @Component//對(duì)象由spring管理 @Aspect//切面注解 public class LogAspect { @Autowired private LogService logService; private static final Logger LOGGER = LogManager.getLogger(LogAspect.class); //定義切入點(diǎn),切入到添加了LogData注解的方法上 @Pointcut("@annotation(aop.LogData)") public void pointCut(){ } /** * 記錄日志的切面方法 * 在該方法中定義統(tǒng)一的日志記錄邏輯 * @param joinPoint */ @Before("pointCut()") public void log(JoinPoint joinPoint){ System.out.println("進(jìn)入日志Aspect"); //獲取到方法簽名 MethodSignature signature= (MethodSignature) joinPoint.getSignature(); //獲取到連接點(diǎn)方法對(duì)象 Method method=signature.getMethod(); //獲取方法上面特定的注解 LogData annotation=method.getAnnotation(LogData.class); LogType logType=annotation.logType(); String description=annotation.description(); LOGGER.info("獲取到注解內(nèi)容:logType="+logType.getType() +",description:"+description); //aop中獲取request ServletRequestAttributes requestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request=requestAttributes.getRequest(); HttpSession session=request.getSession(); //獲取操作人 Student student= (Student) session.getAttribute("student"); //獲取請(qǐng)求數(shù)據(jù) Map<String,String[]> parameterMap=request.getParameterMap(); //將對(duì)象轉(zhuǎn)換成json字符串==>存儲(chǔ)到請(qǐng)求數(shù)據(jù)字段中 //jackSon json字符串操作 ObjectMapper objectMapper=new ObjectMapper(); try { String s=objectMapper.writeValueAsString(parameterMap); LOGGER.info("請(qǐng)求數(shù)據(jù):"+s); Log log = new Log(); log.setLogtype(logType.getType()); log.setDescription(description); log.setParam(s); logService.insert(log); } catch (JsonProcessingException e) { e.printStackTrace(); } //todo 將日志信息保存到數(shù)據(jù)庫(kù) LogController service mapper jsp } }
6、寫一個(gè)loglist.jsp頁(yè)面來(lái)展示信息;
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>用戶列表</title> </head> <body> <form action="${pageContext.request.contextPath}/user/selAll"> <input type="submit" value="查詢"> </form> <table border="1px"> <thead> <tr> <td>ID</td> <td>LOGTYPE</td> <td>DESCRIPTION</td> <td>PARAM</td> </tr> </thead> <tbody> <c:forEach var="log" items="${logs}"> <tr> <td>${log.id}</td> <td>${log.logtype}</td> <td>${log.description}</td> <td>${log.param}</td> </tr> </c:forEach> </tbody> </table> <a href="${pageContext.request.contextPath}/user/list" rel="external nofollow" >返回list頁(yè)面</a> </body> </html>
7、最后,我們寫一個(gè)控制層的方法;
package controller; import aop.LogData; import aop.LogType; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import entity.Log; import entity.Student; import mapper.StudentsMapper; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import service.LogService; import service.StudentService; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.HashMap; import java.util.List; /** * 控制層調(diào)service,service調(diào)dao層 */ @Controller @RequestMapping("/user") public class UserController { //定義日志對(duì)象 //private static final Logger logger= LogManager.getLogger(UserController.class); @Autowired private StudentService studentService; @Autowired private LogService logService; @RequestMapping("/list") //@ModelAttribute(name = "params") :向request域中存值 public String list(ModelMap modelMap,@RequestParam HashMap<String,Object> map){ //定義debug級(jí)別的日志 //logger.debug("前臺(tái)傳遞的查詢條件:"+map); //logger.info("info級(jí)別日志:"+map); System.out.println("前臺(tái)傳遞的查詢條件:"+map); //List<Student> students = studentService.findAll(); // List<Student> students = studentService.findByMap(map); // modelMap.put("students",students); PageInfo<Student> page = studentService.findByPage(map); //記錄error級(jí)別日志 //logger.error("查詢到分頁(yè)數(shù)據(jù):"+page); System.out.println("查詢到分頁(yè)數(shù)據(jù):"+page); modelMap.put("page",page); modelMap.put("params",map);//將查詢條件回傳到頁(yè)面,用于回顯查詢條件 return "list.jsp"; } @LogData(logType = LogType.DELETE,description = "學(xué)生信息刪除") @RequestMapping("/delete") public String delete(Integer id){ studentService.delete(id); return "redirect:list"; } @LogData(logType = LogType.UPDATE,description = "學(xué)生信息修改") @RequestMapping("/update2") public String update2(Integer id,ModelMap modelMap){ Student student = studentService.selectById(id); modelMap.put("student",student); return "update.jsp"; } //根據(jù)是否存在id值,來(lái)判斷是執(zhí)行新增還是修改操作 @RequestMapping("/update") public String update(Student student){ studentService.update(student); return "redirect:list"; } @LogData(logType = LogType.INSERT,description = "學(xué)生信息新增") @RequestMapping("/insert") public String insert(Student student){ studentService.insert(student); return "redirect:list"; } @Autowired private StudentsMapper studentsMapper; @RequestMapping("list2") public String list2(ModelMap modelMap){ PageHelper.startPage(1,5); List<Student> students=studentsMapper.selectAll(); modelMap.put("students",students); PageInfo<Student> pageInfo=new PageInfo<>(students); System.out.println(pageInfo); return "list.jsp"; } @RequestMapping("/selAll") public String findAll(ModelMap modelMap){ List<Log> logs = logService.findAll(); modelMap.put("logs",logs); return "loglist.jsp"; } }
測(cè)試結(jié)果,我們出來(lái)的頁(yè)面效果是:
即說(shuō)明打印日志成功了;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SQL Server免費(fèi)版的安裝以及使用SQL Server Management Studio(SSMS)連接數(shù)據(jù)庫(kù)的圖文方法
- SSM實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)賬號(hào)密碼密文登錄功能
- SQL Server Management Studio(SSMS)復(fù)制數(shù)據(jù)庫(kù)的方法
- ssm框架上傳圖片保存到本地和數(shù)據(jù)庫(kù)示例
- 詳解SSM框架下結(jié)合log4j、slf4j打印日志
- SSM項(xiàng)目中配置LOG4J日志的方法
- SSM整合中的Log4j日志的配置詳情
相關(guān)文章
Struts2中Action中是否需要實(shí)現(xiàn)Execute方法
這篇文章主要介紹了Struts2中Action中是否需要實(shí)現(xiàn)Execute方法的相關(guān)資料,需要的朋友可以參考下2016-03-03IDEA利用自帶Axis工具和wsdl文件反向生成服務(wù)端客戶端代碼圖文詳解
這篇文章主要介紹了IDEA利用自帶Axis工具和wsdl文件反向生成服務(wù)端客戶端代碼詳細(xì)流程,在這里小編使用的是idea2021.1最新開發(fā)工具,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-05-05Java實(shí)現(xiàn)解析JSON大文件JsonReader工具詳解
這篇文章主要介紹了Java實(shí)現(xiàn)解析JSON大文件的工具JsonReader使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01Java AtomicInteger類使用方法實(shí)例講解
這篇文章主要介紹了Java AtomicInteger類使用方法實(shí)例講解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Java使用Soap方式調(diào)用WebService接口代碼示例
Java調(diào)用WebService接口是指通過(guò)Java語(yǔ)言來(lái)訪問并與WebService進(jìn)行交互,WebService是一種基于Web的服務(wù)架構(gòu),它通過(guò)標(biāo)準(zhǔn)的XML和HTTP協(xié)議來(lái)提供服務(wù),這篇文章主要給大家介紹了關(guān)于Java使用Soap方式調(diào)用WebService接口的相關(guān)資料,需要的朋友可以參考下2024-03-03