SSM框架如何详细实现日志信息入库操作?

2026-05-24 08:341阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1421个文字,预计阅读时间需要6分钟。

SSM框架如何详细实现日志信息入库操作?

在service层和mapper层中,我们需要编写一个插入方法和查询方法。首先,我们定义一个日志类Log,并为其定义属性和数据库中的表结构。以下是简化的代码内容:

java// Log类定义package entity;

public class Log { private Integer id; private Integer logType; // 其他属性根据需要添加

// 省略getter和setter方法}

// Mapper接口package mapper;

import entity.Log;import org.apache.ibatis.annotations.*;

public interface LogMapper { @Insert(INSERT INTO logs (log_type) VALUES (#{logType})) void insertLog(Log log);

@Select(SELECT * FROM logs WHERE id=#{id}) Log selectLogById(Integer id);}

// Service接口package service;

import entity.Log;import mapper.LogMapper;

public interface LogService { void addLog(Log log); Log getLogById(Integer id);}

// Service实现package service.impl;

import entity.Log;import mapper.LogMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;

@Servicepublic class LogServiceImpl implements LogService { @Autowired private LogMapper logMapper;

@Override public void addLog(Log log) { logMapper.insertLog(log); }

@Override public Log getLogById(Integer id) { return logMapper.selectLogById(id); }}

SSM框架如何详细实现日志信息入库操作?

1)在service层和mapper层中写一个插入方法和查询方法;

我们先写一个日志类;定义属性;并且要在数据库中建好表;

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页面;

public interface LogService { int insert(Log log); List<Log> findAll(); }

2、logServiceImpl.java页面;

@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页面:

public interface LogMapper { int insert(Log log); List<Log> findAll(); }

4、logMapper.xml页面;

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "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、由于我们打印日志是通过切面,所以我们写一个切面类;

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.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页面</a> </body> </html>

7、最后,我们写一个控制层的方法;

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; /** * 控制层调service,service调dao层 */ @Controller @RequestMapping("/user") public class UserController { //定义日志对象 //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级别的日志 //logger.debug("前台传递的查询条件:"+map); //logger.info("info级别日志:"+map); System.out.println("前台传递的查询条件:"+map); //List<Student> students = studentService.findAll(); // List<Student> students = studentService.findByMap(map); // modelMap.put("students",students); PageInfo<Student> page = studentService.findByPage(map); //记录error级别日志 //logger.error("查询到分页数据:"+page); System.out.println("查询到分页数据:"+page); modelMap.put("page",page); modelMap.put("params",map);//将查询条件回传到页面,用于回显查询条件 return "list.jsp"; } @LogData(logType = LogType.DELETE,description = "学生信息删除") @RequestMapping("/delete") public String delete(Integer id){ studentService.delete(id); return "redirect:list"; } @LogData(logType = LogType.UPDATE,description = "学生信息修改") @RequestMapping("/update2") public String update2(Integer id,ModelMap modelMap){ Student student = studentService.selectById(id); modelMap.put("student",student); return "update.jsp"; } //根据是否存在id值,来判断是执行新增还是修改操作 @RequestMapping("/update") public String update(Student student){ studentService.update(student); return "redirect:list"; } @LogData(logType = LogType.INSERT,description = "学生信息新增") @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"; } }

测试结果,我们出来的页面效果是:

即说明打印日志成功了;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计1421个文字,预计阅读时间需要6分钟。

SSM框架如何详细实现日志信息入库操作?

在service层和mapper层中,我们需要编写一个插入方法和查询方法。首先,我们定义一个日志类Log,并为其定义属性和数据库中的表结构。以下是简化的代码内容:

java// Log类定义package entity;

public class Log { private Integer id; private Integer logType; // 其他属性根据需要添加

// 省略getter和setter方法}

// Mapper接口package mapper;

import entity.Log;import org.apache.ibatis.annotations.*;

public interface LogMapper { @Insert(INSERT INTO logs (log_type) VALUES (#{logType})) void insertLog(Log log);

@Select(SELECT * FROM logs WHERE id=#{id}) Log selectLogById(Integer id);}

// Service接口package service;

import entity.Log;import mapper.LogMapper;

public interface LogService { void addLog(Log log); Log getLogById(Integer id);}

// Service实现package service.impl;

import entity.Log;import mapper.LogMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;

@Servicepublic class LogServiceImpl implements LogService { @Autowired private LogMapper logMapper;

@Override public void addLog(Log log) { logMapper.insertLog(log); }

@Override public Log getLogById(Integer id) { return logMapper.selectLogById(id); }}

SSM框架如何详细实现日志信息入库操作?

1)在service层和mapper层中写一个插入方法和查询方法;

我们先写一个日志类;定义属性;并且要在数据库中建好表;

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页面;

public interface LogService { int insert(Log log); List<Log> findAll(); }

2、logServiceImpl.java页面;

@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页面:

public interface LogMapper { int insert(Log log); List<Log> findAll(); }

4、logMapper.xml页面;

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "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、由于我们打印日志是通过切面,所以我们写一个切面类;

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.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页面</a> </body> </html>

7、最后,我们写一个控制层的方法;

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; /** * 控制层调service,service调dao层 */ @Controller @RequestMapping("/user") public class UserController { //定义日志对象 //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级别的日志 //logger.debug("前台传递的查询条件:"+map); //logger.info("info级别日志:"+map); System.out.println("前台传递的查询条件:"+map); //List<Student> students = studentService.findAll(); // List<Student> students = studentService.findByMap(map); // modelMap.put("students",students); PageInfo<Student> page = studentService.findByPage(map); //记录error级别日志 //logger.error("查询到分页数据:"+page); System.out.println("查询到分页数据:"+page); modelMap.put("page",page); modelMap.put("params",map);//将查询条件回传到页面,用于回显查询条件 return "list.jsp"; } @LogData(logType = LogType.DELETE,description = "学生信息删除") @RequestMapping("/delete") public String delete(Integer id){ studentService.delete(id); return "redirect:list"; } @LogData(logType = LogType.UPDATE,description = "学生信息修改") @RequestMapping("/update2") public String update2(Integer id,ModelMap modelMap){ Student student = studentService.selectById(id); modelMap.put("student",student); return "update.jsp"; } //根据是否存在id值,来判断是执行新增还是修改操作 @RequestMapping("/update") public String update(Student student){ studentService.update(student); return "redirect:list"; } @LogData(logType = LogType.INSERT,description = "学生信息新增") @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"; } }

测试结果,我们出来的页面效果是:

即说明打印日志成功了;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。