Spring Boot makes personal blog-build framework

Article Directory

1. New project

  • Use IDEA tools to create project files
Insert picture description here
  • Project name and package name
Insert picture description here
  • Introduce the spring boot module
Insert picture description here
Insert picture description here

Two, application.yml configuration

1. Rename application.properties

Insert picture description here

2. Create a database

Insert picture description here

3. Write configuration files

Insert picture description here
spring:
  thymeleaf:
    mode: HTML

  #数据库连接
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 12465

#    自动验证数据库
  jpa:
    hibernate:
      ddl-auto: update
    #      配置在控制台输出SQL语句
    show-sql: true

#    日志的级别
logging:
  level:
    root: info
    com.lrm: debug
  file: log/blog.log



5. Create a new logbac-spring.xml file

Insert picture description here
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!--1、继承Spring boot logback设置(可以在appliaction.yml或者application.properties设置logging.*属性) 2、重写了默认配置,设置日志文件大小在100MB时,按日期切分日志,切分后目录: blog.2017-08-01.0 80MBblog.2017-08-01.1 10MBblog.2017-08-02.0 56MBblog.2017-08-03.0 53MB...... -->
    <!--包含Spring boot对logback日志的默认配置-->
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <property value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}" name="LOG_FILE"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
    <!--重写了Spring Boot框架 org/springframework/boot/logging/logback/file-appender.xml 配置-->
    <appender name="TIME_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <encoder>
        <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
    <file>${LOG_FILE}</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i</fileNamePattern>
        <!--保留历史日志一个月的时间-->
        <maxHistory>30</maxHistory>
        <!--Spring Boot默认情况下,日志文件10M时,会切分日志文件,这样设置日志文件会在100M时切分日志 -->
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
    </appender>

    <root level="INFO">
    <appender-ref ref="CONSOLE"/>
    <appender-ref ref="TIME_FILE"/>

</root>

</configuration>

6. Copy application.ymlapplication-dev.yml

Insert picture description here
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 12465

  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

# 日志的级别
logging:
  level:
    root: info
    net.zjs.lrm: debug
  file:
    name: log/blog-dev.log

7. Copy application.yml to application-pro.yml and modify the code

Insert picture description here
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 12465

  jpa:
    hibernate:
      ddl-auto: none
    show-sql: true

# 日志的级别
logging:
  level:
    root: warn
    net.zjs.lrm: info
  file:
    name: log/blog.log

server:
  port: 8081

8. Modify the application.yml code

Insert picture description here

9. Run BlogApplication

Insert picture description here