springboot配置
application.properties
#servlet
server.port=8080
server.tomcat.max-connections=1000
server.servlet.context-path=/
#配置数据源
#配置druid数据源,添加依赖,此行可以省略
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql:///jtsys?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=root
#可以省略
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybati相关配置
#配置sql执行超时时间
mybatis.configuration.default-statement-timeout=30
#开启驼峰命名规则
mybatis.configuration.map-underscore-to-camel-case= true
#配置日志,让控制台打印sql语句
logging.level.com.cy=debug
#开启静态页面拼接 视图解析器
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
application.yml
server:
port: 8080 #端口
servlet:
context-path: / #请求内容
spring:
datasource: #数据库连接
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql:///jtsys?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
username: root
password: root
thymeleaf: #视图解析器
suffix: .html
prefix: classpath:/templates/
mybatis-plus: #mybatis-plus
mapper-locations: mappers/*.xml
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
type-aliases-package: com.cy.entity
springboot依赖
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cy</groupId>
<artifactId>{{projectName}}</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>{{projectName}}</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!--jar包锁定-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/eu.bitwalker/UserAgentUtils -->
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.20</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.stuxuhai/jpinyin -->
<!-- 导入Jpinyin;汉字转拼音的包 -->
<dependency>
<groupId>com.github.stuxuhai</groupId>
<artifactId>jpinyin</artifactId>
<version>1.1.8</version>
</dependency>
<!-- 发送邮件插件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-gson -->
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>2.3.0</version>
</dependency>
<!-- 导入fastjson -->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.36</version>
</dependency>
<!--Mybatis-plus的依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
</dependency>
<!-- 文件操作 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 文件操作 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- 视图解析器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.7.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 如果不设置fork,那么不会restart,devtools热部署不会起作用-->
<fork>true</fork>
</configuration>
<!--热加载 -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.7.RELEASE</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
</project>
springboot启动类
package cn.gson.oasys;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OasysApplication {
public static void main(String[] args) {
SpringApplication.run(OasysApplication.class, args);
}
}
springboot集成log4j
<!-- 需要排除web自带日志 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加log4j依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
log4j配置文件 log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别从低到高优先级为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF。-->
<!--status:设置 log4j2 自身内部的信息输出,可以不设置-->
<!--monitorInterval:监视配置文件变化间隔时间,单位秒,Log4j2 能够自动检测配置文件是否修改,同时更新配置-->
<configuration monitorInterval="30">
<!--定义属性,在下方方便维护引用-->
<properties>
<!--当天文件保存目录-->
<property name="logDir">./logs</property>
<!--历史文件保存目录-->
<property name="historyLogDir">./logs/history</property>
<!--如果当天日志文件大于100MB,则存档-->
<property name="splitSize">100MB</property>
<!--文件保存个数,这里的30说明可保留30个,即30天-->
<property name="fileCount">30</property>
<!--输出格式-->
<property name="logPattern">%d{yyyy-MM-dd HH:mm:ss.SSS} => [%thread] => %-5level %logger{50}:(%line) - %msg%n</property>
</properties>
<!--先定义所有的 appender 附加器-->
<appenders>
<!--控制台输出配置-->
<console name="Console" target="SYSTEM_OUT">
<filters>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</filters>
<!--输出日志的格式
%d表示日期时间,
%thread表示线程名,
%-5level:级别从左显示5个字符宽度
%logger{50} 表示logger名字最长50个字符,否则按照句点分割。
%line:表示行号
%msg:日志消息
%n是换行符-->
<PatternLayout pattern="${logPattern}" />
</console>
<!--文件存储文件设置-->
<!--fileName:文件存储路径及名称,可以是绝对路径或者相对路径; 存储的永远是最新的日志信息-->
<!--filePattern:当 fileName 指定的文件大小超过限制,就会根据此文件名规则新建存档目录与文件,同时将 fileName 文件中的
内容剪切到存档文件中,如下配置,会新建存档路径 logs/history/error-2023-03-26-1.log -->
<!--以下只保存debug信息-->
<RollingFile name="fileDebug" fileName="${logDir}/debug.log"
filePattern="${historyLogDir}/debug-%d{yyyy-MM-dd}-%i.log">
<filters>
<ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL" />
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY" />
</filters>
<PatternLayout pattern="${logPattern}" />
<Policies>
<TimeBasedTriggeringPolicy />
<!--日志文件大小超过多少时进行存档-->
<SizeBasedTriggeringPolicy size="${splitSize}" />
</Policies>
<DefaultRolloverStrategy max="${fileCount}" />
</RollingFile>
<!--以下只保存info信息-->
<RollingFile name="fileInfo" fileName="${logDir}/info.log"
filePattern="${historyLogDir}/info-%d{yyyy-MM-dd}-%i.log">
<filters>
<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL" />
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY" />
</filters>
<PatternLayout pattern="${logPattern}" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="${splitSize}" />
</Policies>
<DefaultRolloverStrategy max="${fileCount}" />
</RollingFile>
<!--以下只保存warn信息-->
<RollingFile name="fileWarn" fileName="${logDir}/warn.log"
filePattern="${historyLogDir}/warn-%d{yyyy-MM-dd}-%i.log">
<filters>
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL" />
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY" />
</filters>
<PatternLayout pattern="${logPattern}" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="${splitSize}" />
</Policies>
<DefaultRolloverStrategy max="${fileCount}" />
</RollingFile>
<!--以下只保存error信息-->
<RollingFile name="fileError" fileName="${logDir}/error.log"
filePattern="${historyLogDir}/error-%d{yyyy-MM-dd}-%i.log">
<filters>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY" />
</filters>
<PatternLayout pattern="${logPattern}" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="${splitSize}" />
</Policies>
<DefaultRolloverStrategy max="${fileCount}" />
</RollingFile>
<!--以下保存api请求信息-->
<RollingFile name="apiInfo" fileName="${logDir}/api.log"
filePattern="${historyLogDir}/api-%d{yyyy-MM-dd}-%i.log">
<filters>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY" />
</filters>
<PatternLayout pattern="${logPattern}" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="${splitSize}" />
</Policies>
<DefaultRolloverStrategy max="${fileCount}" />
</RollingFile>
<!-- 整合elk -->
<Socket name="logstash" host="127.0.0.1" port="4560" protocol="TCP">
<PatternLayout pattern="${logPattern}" />
</Socket>
</appenders>
<!--
1、root与logger是父子关系,没有特别定义logger的都默认为root。
2、任何一个类只会和一个logger对应,要么是定义的logger,要么是root,判断的关键在于找到这个logger,然后判断这个logger的appender和level。
3、appender-ref 用于引用上面定义好的 appender 日志追加器,只有引用了,上面的 appender 才能生效. -->
<loggers>
<!--过滤掉 spring 和 mybatis 的一些无用的 DEBUG 信息;也可以单独指定自己的某个包-->
<!--以下包中ERROR级别以上日志在root中将被记录-->
<logger name="org.springframework" level="ERROR"></logger>
<logger name="org.mybatis" level="ERROR"></logger>
<logger name="org.hibernate" level="ERROR"></logger>
<logger name="org.apache" level="ERROR"></logger>
<logger name="com.baomidou" level="ERROR"></logger>
<logger name="com.zaxxer" level="ERROR"></logger>
<logger name="springfox.documentation" level="ERROR"></logger>
<!--com.yyl.system包中的DEBUG以上信息在以下的appender中输出,additivity所有日志还要向控制台和allLog中输出-->
<logger name="com.yyl.system" level="DEBUG" additivity="true">
<appender-ref ref="fileDebug" />
<appender-ref ref="fileInfo" />
<appender-ref ref="fileWarn" />
<appender-ref ref="fileError" />
<appender-ref ref="apiInfo" />
<appender-ref ref="logstash" />
</logger>
<root level="INFO">
<appender-ref ref="Console" />
</root>
</loggers>
</configuration>
热启动: