Myeclipse搭建SSM项目

一、项目环境

项目环境:
apache-tomcat-8.5.37
apache-maven-3.6.0
MySQL 5.6.30
jdk8

二、创建Maven项目(完整项目结构)
Myeclipse搭建SSM项目

三、配置文件及数据库文件
1、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com</groupId>
	<artifactId>myapp</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>myapp Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<!--定义版本号 -->
	<properties>
		<project.lombok.version>1.18.16</project.lombok.version>
		<project.spring.version>5.2.12.RELEASE</project.spring.version>
		<project.junit.version>4.12</project.junit.version>
		<project.mysql.version>8.0.22</project.mysql.version>
		<project.druid.version>1.2.4</project.druid.version>
		<project.mybatis.version>3.5.6</project.mybatis.version>
		<project.mybatis-spring.version>2.0.6</project.mybatis-spring.version>
		<project.jackson.version>2.12.1</project.jackson.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>${project.lombok.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${project.spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${project.spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${project.spring.version}</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${project.junit.version}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${project.mysql.version}</version>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>${project.druid.version}</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${project.mybatis.version}</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>${project.mybatis-spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>2.14.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.14.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-web</artifactId>
			<version>2.14.1</version>
		</dependency>
		
		  <!--异步日志依赖 -->
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.4.2</version>
        </dependency>

		<!-- <dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.30</version>
		</dependency> -->

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>2.14.1</version>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${project.jackson.version}</version>
		</dependency>
	</dependencies>


	<build>
		<finalName>myapp</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
					<compilerArguments>
						<extdirs>src\main\webapp\WEB-INF\lib</extdirs>
					</compilerArguments>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

2、db.properties 数据库信息

druid.driver=com.mysql.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
druid.username=root
druid.password=root

3、log4j2.xml 日志信息

<?xml version="1.0" encoding="UTF-8" ?>
<!-- status="off" 日志框架本身的输出日志级别,可以修改为debug monitorInterval="5" 自动加载配置文件的间隔时间,不低于 
	5秒;生产环境中修改配置文件,是热更新,无需重启应用 -->
<configuration status="off" monitorInterval="5">
	<!-- 集中配置属性进行管理 使用时通过:${name} -->
	<properties>
		<property name="LOG_HOME">${web:rootDir}/WEB-INF/logs</property>
	</properties>

	<!-- 日志处理 -->
	<Appenders>
		<!-- 控制台输出 appender,SYSTEM_OUT输出黑色,SYSTEM_ERR输出红色 -->
		<Console name="Console" target="SYSTEM_OUT">
			<PatternLayout
				pattern="%d{HH:mm:ss.SSS} [%t] [%-5level] %c{36}:%L --- %m%n" />
		</Console>

		<!-- 日志文件输出 appender -->
		<!-- <File name="file" fileName="${LOG_HOME}/myfile.log"> <PatternLayout 
			pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] %l %c{36} - %m%n" /> </File> -->

		<!-- 使用随机读写流的日志文件输出 appender,性能提高 -->
		<!-- <RandomAccessFile name="accessFile" fileName="${LOG_HOME}/myAcclog.log"> 
			<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] %l %c{36} 
			- %m%n" /> </RandomAccessFile> -->

		<!-- 按照一定规则拆分的日志文件的appender -->  <!-- 拆分后的文件 -->
		<!-- filePattern="${LOG_HOME}/$${date:yyyy-MM-dd}/myrollog-%d{yyyy-MM-dd-HH-mm}-%i.log"> -->
		<RollingFile name="rollingFile"
			fileName="${LOG_HOME}/myrollog.log"
			filePattern="${LOG_HOME}/olds/app-%d{yyyy-MM-dd}.log">
			<!-- 日志级别过滤器 -->
			<ThresholdFilter level="info" onMatch="ACCEPT"
				onMismatch="DENY" />
			<!-- 日志消息格式 -->
			<PatternLayout
				pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] %l %c{36} - %msg%n" />
			<Policies>
				<!-- 在系统启动时,出发拆分规则,生产一个新的日志文件 -->
				<!-- <OnStartupTriggeringPolicy /> -->
				<!-- 按照文件大小拆分,200MB -->
				<SizeBasedTriggeringPolicy size="200MB" />
				<!-- 按照时间节点拆分,规则根据filePattern定义的 -->
				<TimeBasedTriggeringPolicy modulate="true"
					interval="1" />
			</Policies>
			<!-- 在同一个目录下,文件的个限定为 30个,超过进行覆盖 -->
			<DefaultRolloverStrategy max="10" />
		</RollingFile>

		<!-- 异步AsyncAppender -->
		<!-- <Async name="Async"> <AppenderRef ref="rollingFile"/> </Async> -->
	</Appenders>

	<!-- logger 定义 -->
	<Loggers>
		<!-- 混合异步AsyncLogger -->
		<AsyncLogger name="com.asyn.Main" level="trace"
			additivity="false">
			<appender-ref ref="rollingFile" />
		</AsyncLogger>

		<!-- 使用 rootLogger 配置 日志级别 level="trace" -->
		<Root level="info">
			<!-- 指定日志使用的处理器 -->
			<AppenderRef ref="Console" />
			<!-- <AppenderRef ref="file" /> -->
			<!-- <AppenderRef ref="Async" /> -->
			<!-- <AppenderRef ref="accessFile" /> -->
		</Root>
	</Loggers>
</configuration>

4、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

	<!-- 自动扫描 -->
	<context:component-scan base-package="com.app">
		<context:exclude-filter type="regex"
			expression="com.app.controller.*" />
	</context:component-scan>

	<!-- 引入数据库配置文件 -->
	<context:property-placeholder
		location="classpath:db.properties" />

	<!-- 配置数据源 -->
	<bean id="dataSource"
		class="com.alibaba.druid.pool.DruidDataSource">
		<property name="url" value="${druid.url}" />
		<property name="username" value="${druid.username}" />
		<property name="password" value="${druid.password}" />
	</bean>

	<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件, 创建Session工厂 -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 引入数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- Aliases取别名 -->
		<property name="typeAliasesPackage" value="com.app.domain" />
		<!-- 引入mapper -->
		<property name="mapperLocations"
			value="classpath:mapper/*.xml"></property>
	</bean>

	<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.app.mapper" />
		<property name="sqlSessionFactoryBeanName"
			value="sqlSessionFactory"></property>
	</bean>

	<!-- 事务管理 -->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 启用注解事务 -->
	<tx:annotation-driven
		transaction-manager="txManager" />
</beans>

5、springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!--开启controller扫描 指定扫描的基础包 -->
	<context:component-scan
		base-package="com.app.controller" />

	<!--开启注解扫描 -->
	<mvc:annotation-driven />


	<!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/views/目录下,查找XXX.jsp文件 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

6、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">

	<display-name>Archetype Created Web Application</display-name>

	<!--初始化spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!--读取spring文件夹下所有applicationContext-打头的文件 -->
		<param-value>classpath:spring/applicationContext.xml</param-value>
	</context-param>
	<!--监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

	<!-- 解决乱码 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter
		</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- springmvc的前端控制器 -->
	<servlet>
		<servlet-name>myapp</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>myapp</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

7、UserMapper.xml

<?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="com.app.mapper.UserMapper">

    <!--查询全部 id必须与接口的方法名对应-->
    <select id="findUser" resultType="User">
        select * from user where name=1
    </select>
</mapper>

7、数据库数据

CREATE DATABASE;

USE `test`;


DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `username` varchar(50) NOT NULL,
  `password` varchar(16) NOT NULL,
  `tel` varchar(11) NOT NULL,
  `email` varchar(30) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


insert  into `user`(`username`,`password`,`tel`,`email`,`name`) values ('1','1','1','1','1'),('2','2','2','2','2'),('3','3','3','3','3');

四、Java代码
1、com.app.domain.User

package com.app.domain;

import java.io.Serializable;

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

/**
 * @ClassName: User
 * 
 * @Description: 用户类
 * 
 * @author ***
 * 
 * @date 2021年11月16日 下午6:16:25
 * 
 */
@Data // get、set方法
@NoArgsConstructor // 构成构造
@AllArgsConstructor // 全参构造
public class User implements Serializable {
    private String username;
    private String password;
    private String tel;
    private String email;
    private String name;

}

2、com.app.mapper.UserMapper

package com.app.mapper;

import com.app.domain.User;

/**
 * @ClassName: UserMapper
 * 
 * @Description: 用户mapper
 * 
 * @author **
 * 
 * @date 2021年11月16日 下午6:17:12
 * 
 */
public interface UserMapper {
    public User findUser();
}

3、com.app.service.UserService

package com.app.service;

import com.app.domain.User;

/**
 * @ClassName: UserService
 * 
 * @Description: 用户server接口
 * 
 * @author **
 * 
 * @date 2021年11月16日 下午6:18:08
 * 
 */
public interface UserService {
    public User findUser();
}

4、com.app.service.impl.UserServiceImpl

package com.app.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.app.domain.User;
import com.app.mapper.UserMapper;
import com.app.service.UserService;

/**
 * @ClassName: UserServiceImpl
 * 
 * @Description: 用户server实现
 * 
 * @author **
 * 
 * @date 2021年11月16日 下午6:18:31
 * 
 */
@Service
public class UserServiceImpl implements UserService {
    // 注入UserMapper对象
    @Resource
    private UserMapper mapper;

    @Override
    public User findUser() {
        return mapper.findUser();
    }
}

5、com.app.controller.UserController

package com.app.controller;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.app.domain.User;
import com.app.service.UserService;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: UserController
 * 
 * @Description: TODO(这里用一句话描述这个类的作用)
 * 
 * @author 陈玉明
 * 
 * @date 2021年11月16日 下午6:19:59
 * 
 */
@Slf4j(topic = "com.asyn.Main")
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    public static final org.slf4j.Logger log1 = LoggerFactory.getLogger(UserController.class);

    @RequestMapping("/all")
    @ResponseBody
    public User findUser() {
        log.info("进入controller----日志异步写入文件");
        log1.info("进入controller----日志同步写入控制台");
        return userService.findUser();
    }
}


五、运行项目
1、调用接口http://localhost:8080/myapp/user/all
Myeclipse搭建SSM项目
2、控制台同步日志
Myeclipse搭建SSM项目
3、日志文件异步日志
Myeclipse搭建SSM项目

六、项目小知识

1、同一个Tomcate跑多个项目,需要在web.xml中区分不同项目

<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>app1.root</param-value>
</context-param>

2、关于org.springframework.web.util.Log4jConfigListener启用问题

1)、spring5.0下可以使用log4j

使用log4j.properties配置日志(放在resources下,默认加载)

当需要相对日志文件路径,可以配置web.xml,通过${webapp.root}获取项目路径

<!-- 加载log4j文件 -->
	<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>webapp.root</param-value>
	</context-param>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:spring/log4j.properties</param-value>
	</context-param>
	<!--Spring刷新Log4j配置文件的间隔60秒,单位为millisecond -->
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>60000</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener
		</listener-class>
	</listener>

2)、5.0及以上可以使用log4j2

使用log4j2.xml配置文件(放在resources下,默认加载)

当需要相对日志文件路径,注意添加jar,通过${web:rootDir}获取项目路径

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-web</artifactId>
			<version>2.14.1</version>
		</dependency>

3)log4j2+Slf4j 使用教程

1、添加jar

	<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>2.14.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.14.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-web</artifactId>
			<version>2.14.1</version>
		</dependency>
		
		  <!--异步日志依赖 -->
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.4.2</version>
        </dependency>

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>2.14.1</version>
		</dependency>

2、创建log4j2.xml 配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!-- status="off" 日志框架本身的输出日志级别,可以修改为debug monitorInterval="5" 自动加载配置文件的间隔时间,不低于 
	5秒;生产环境中修改配置文件,是热更新,无需重启应用 -->
<configuration status="off" monitorInterval="5">
	<!-- 集中配置属性进行管理 使用时通过:${name} -->
	<properties>
		<!-- <property name="LOG_HOME">${web:rootDir}/WEB-INF/logs</property> -->
		<property name="LOG_HOME">D:\apache-tomcat-8.5.37\webapps\myapp\WEB-INF\logs
		</property>
	</properties>

	   <!-- 日志处理 -->
    <Appenders>
        <!-- 控制台输出 appender,SYSTEM_OUT输出黑色,SYSTEM_ERR输出红色 -->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{HH:mm:ss.SSS} [%t] [%-5level] %c{36}:%L --- %m%n" />
        </Console>

        <!-- 日志文件输出 appender -->
        <!-- <File name="file" fileName="${LOG_HOME}/myfile.log"> <PatternLayout 
            pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] %l %c{36} - %m%n" /> </File> -->

        <!-- 使用随机读写流的日志文件输出 appender,性能提高 -->
        <!-- <RandomAccessFile name="accessFile" fileName="${LOG_HOME}/myAcclog.log"> 
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] %l %c{36} 
            - %m%n" /> </RandomAccessFile> -->

        <!-- 按照一定规则拆分的日志文件的appender -->  <!-- 拆分后的文件 -->
        <!-- filePattern="${LOG_HOME}/$${date:yyyy-MM-dd}/myrollog-%d{yyyy-MM-dd-HH-mm}-%i.log"> -->
        <RollingFile name="rollingFile"
            fileName="${LOG_HOME}/myrollog.log"
            filePattern="${LOG_HOME}/olds/app-%d{yyyy-MM-dd}.log">
            <!-- 日志级别过滤器 -->
            <ThresholdFilter level="info" onMatch="ACCEPT"
                onMismatch="DENY" />
            <!-- 日志消息格式 -->
            <PatternLayout
                pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] %l %c{36} - %msg%n" />
            <Policies>
                <!-- 在系统启动时,出发拆分规则,生产一个新的日志文件 -->
                <!-- <OnStartupTriggeringPolicy /> -->
                <!-- 按照文件大小拆分,200MB -->
                <SizeBasedTriggeringPolicy size="200MB" />
                <!-- 按照时间节点拆分,规则根据filePattern定义的 -->
                <TimeBasedTriggeringPolicy modulate="true"
                    interval="1" />
            </Policies>
            <!-- 在同一个目录下,文件的个限定为 30个,超过进行覆盖 -->
            <DefaultRolloverStrategy max="10" />
        </RollingFile>

        <!-- 异步AsyncAppender -->
        <!-- <Async name="Async"> <AppenderRef ref="rollingFile"/> </Async> -->
    </Appenders>

    <!-- logger 定义 -->
    <Loggers>
        <!-- 混合异步AsyncLogger -->
        <AsyncLogger name="com.asyn" level="trace"
            additivity="false">
            <appender-ref ref="rollingFile" />
            <appender-ref ref="Console" />
        </AsyncLogger>

        <!-- 使用 rootLogger 配置 日志级别 level="trace" -->
        <!-- 同步 -->
        <Root level="info">
            <!-- 指定日志使用的处理器 -->
            <AppenderRef ref="Console" />
            <AppenderRef ref="rollingFile" />
            <!-- <AppenderRef ref="file" /> -->
            <!-- <AppenderRef ref="Async" /> -->
            <!-- <AppenderRef ref="accessFile" /> -->
        </Root>
    </Loggers>
</configuration>

3、测试类

package com.test;

import org.junit.Test;
import org.slf4j.LoggerFactory;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: TestLog4j2
 * 
 * @Description: TODO(这里用一句话描述这个类的作用)
 * 
 * @author 
 * 
 * @date 2021年11月19日 下午2:59:04
 * 
 */
@Slf4j(topic = "com.asyn") // 注解方式--异步--文件、控制台
// @Slf4j //注解方式--同步--文件、控制台
public class TestLog4j2 {
    // 同步日志
    public static final org.slf4j.Logger log2 = LoggerFactory.getLogger(TestLog4j2.class);
    // 异步日志
    public static final org.slf4j.Logger log3 = LoggerFactory.getLogger("com.asyn");

    @Test
    public void testInfo() {
        // 1、注解方式

        // 同步(打开第二个注解)
        // log.info("注解--同步");
        // 异步(打开第一个注解)
        log.info("注解--异步");

        // 2、实例方式

        // 同步(打开第二个注解)
        log2.info("实例--同步");
        // 异步(打开第一个注解)
        log3.info("实例--异步");

    }

}

4、结果

Myeclipse搭建SSM项目

上一篇:python数据可视化


下一篇:基于阿里云平台的人力资源流动大数据分析(二)