This is the 14th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge

The target

Develop a log management module in the system. This module is mainly used to record which method a user calls and what operation he or she does. In this way, when a user has illegal operation or the data in the database is found to be maliciously tampered with, it can immediately locate the user who does it and quickly locate the problem.

methods

Using AOP in Springboot to record user operations, adopting AOP method can make the code recording user operations independent of other business logic code, not hinder the normal operation of the business, but also conducive to the maintenance in the later period.

AOP(Aspect Oriented Programming) is a technique to add functions dynamically and uniformly to programs without modifying the source code by means of precompilation and dynamic proxy at runtime.

To learn more about AOP, go to the website:

Specific implementation Steps

1. Add AOP dependencies to POM.xml

  <! -- AOP dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
Copy the code

2. Database table design

I use Oracle database, to use mysql, just change the data type.

At the same time, I use myBatis to connect to the database, if you want to carry out the following operations, to add myBatis dependency on pom. XML. Mybatis connects to Oracle database

3. The Pojo layer

Create a New Java file called SysLog in the POJO layer.

package com.tjm.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class SysLog {
    int log_id;
    String username;
    String class_name;
    String method_name;
    String args;
}
Copy the code

4. The service layer

Create the service interface file and the interface implementation class as follows:

SysLogService

package com.tjm.service;

import com.tjm.pojo.SysLog;
import org.springframework.stereotype.Service;

@Service
public interface SysLogService {

    int insertLog(SysLog log);
}
Copy the code

SysLogServiceImpl

package com.tjm.service;

import com.tjm.mapper.LogMapper;
import com.tjm.pojo.SysLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("sysLogService")
public class SysLogServiceImpl implements SysLogService{

    @Autowired
    private LogMapper logMapper;

    @Override
    public int insertLog(SysLog log) {
        returnlogMapper.insertLog(log); }}Copy the code