springboot2.x使⽤mybatis实现系统⽇志记录(将完整参数的SQL语句。
。。
⼀、前⾔
最近要求做系统的全局⽇志记录功能,要求把执⾏的完整SQL语句保存到数据库中,在实际开发中最终放弃了使⽤AOP的⽅法,改由使⽤mybatis的实现,这⾥简单记录⼀下实现过程。
系统数据库主要环境:
数据库:MySQL
数据源:DruidDataSource
⼆、创建数据库⽤表
这⾥只是简单演⽰⼀下实现过程,只⽤两张表模拟⼀下:
⽤户表(user)
CREATE TABLE`user`(
`id`bigint(20)NOT NULL,
`age`int(11)NULL DEFAULT NULL,
`name`varchar(255)CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY(`id`)USING BTREE
)ENGINE=InnoDB CHARACTER SET= utf8mb4 COLLATE= utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
⽇志表(sys_log)
CREATE TABLE`sys_log`(
`id`bigint(20)NOT NULL COMMENT'主键',
`uri`varchar(255)CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT'调⽤的接⼝',
`daoMethodName`varchar(1000)CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT'DAO层执⾏的⽅法名称',
`ip`varchar(100)CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT'ip地址',
`wholeSql`mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT'完整SQL语句',
`desc`varchar(255)CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT'描述',
`createDate`datetime(0)NULL DEFAULT NULL COMMENT'创建时间',
PRIMARY KEY(`id`)USING BTREE
)ENGINE=InnoDB CHARACTER SET= utf8mb4 COLLATE= utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
注意:不要使⽤“SQL”关键字作为字段名称,否则后续系统执⾏SQL语句会报错。
三、创建演⽰项⽬
1. 创建⼀个spring boot项⽬,项⽬结构如下:
2. 导⼊maven依赖:
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!--apache⼯具类-->
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--lombok-->
springboot aop
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--spring boot test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
3. 修改l配置⽂件:
server:
port: 8888
spring:
datasource:
url: jdbc:mysql://localhost:3306/bg-learnsp?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false # 连接数据库的url
username: root # 连接数据库的⽤户名
password: root # 连接数据库的密码
driver-class-name: sql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:/mapper/*.xml
4. 创建⽤户和⽇志的实体类:
User类:
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.Serializable;
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private long id;
private Integer age;
private String name;
}
SysLog类:
package ity.log;
import lombok.Data;
import java.util.Date;
@Data
public class SysLog {
private long id;
private String uri;
private String daoMethodName;
private String ip;
private String wholeSql;
private String desc;
private Date createDate;
}
5. 创建⽤户和⽇志的mapper接⼝和xml⽂件:
UserMapper:
import ity.user.User;
import java.util.List;
public interface UserMapper {
List<User>selectAllUsers();
int insert(User user);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.learn.mapper.user.UserMapper">
<select id="selectAllUsers"  resultType="ity.user.User">
select id,age,name from user
</select>
<insert id="insert" parameterType="ity.user.User">
INSERT INTO USER(ID,NAME, AGE)VALUES(#{id},#{name}, #{age})
</insert>
</mapper>
LogMapper:
package com.learn.mapper.log;
import ity.log.SysLog;
public interface LogMapper {
int insertLog(SysLog sysLog);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.learn.mapper.log.LogMapper">
<insert id="insertLog" parameterType="ity.log.SysLog">
INSERT INTO `sys_log`(`id`, `uri`,`daoMethodName`, `ip`, `wholeSql`,`desc`, `createDate`) VALUES (#{id}, #{uri}, #{daoMethodName}, #{ip}, #{whole Sql}, #{desc}, #{createDate})
</insert>
</mapper>
6. 修改启动类,添加相关配置:
@SpringBootApplication
@MapperScan("com.learn.mapper.user")//mapper包扫描
@EnableTransactionManagement
public class SpringbootLogrecordApplication {
public static void main(String[] args){
SpringApplication.run(SpringbootLogrecordApplication.class, args);
}
}
四、⾃定义⽇志注解和增加⼯具类
为了增加⽇记记录的灵活性,增加两个⾃定义⽇志注解:
RecordLog注解:
package com.learn.annotation.log;
import java.lang.annotation.*;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
/**
* 使⽤此注解标识DAO层需要⽇记记录的⽅法
*
*/
public @interface RecordLog {
/**
* ⽇志记录描述
*
* @return
*/
String desc()default"";
}
UnRecordLog注解:
package com.learn.annotation.log;
import java.lang.annotation.*;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
/**
* 使⽤此注解标识DAO层不需要⽇记记录的⽅法
*
*/
public @interface UnRecordLog {
}
为了⽅便开发,增加⼏个⼯具类:
GlobalWebVarUtil⼯具类主要⽤于获取HttpServletRequest对象:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。