菜鸟的springboot工程

143 阅读2分钟

题外话

  • 生成UUID方法

(1)java8

UUID.rondom().toString();

(2) hutool

IdUtil.fastSimpleUUID();

(3) mica(基于spring,java8为服务工具集)

StringUtil.getUUID();

正题

  1. 新建一个springboot工程(此处省略)

  2. 引入依赖

    • hutool工具包
    	    <dependency>
    	        <groupId>cn.hutool</groupId>
    	        <artifactId>hutool-all</artifactId>
    	        <version>4.5.7</version>
    	    </dependency>
    
    
    • json
    	    <!--alibaba-->
    		<dependency>
    			<groupId>com.alibaba</groupId>
    			<artifactId>fastjson</artifactId>
    			<version>1.2.56</version>
    		</dependency>
    
    • 数据库连接池(这里使用阿里巴巴的德鲁伊)
    	    <dependency>
    			<groupId>com.alibaba</groupId>
    			<artifactId>druid-spring-boot-starter</artifactId>
    			<version>1.1.9</version>
    		</dependency>
    
  • jdbc

    	<dependency>
    	    <groupId>mysql</groupId>
    	    <artifactId>mysql-connector-java</artifactId>
    	</dependency>
    
  1. 配置日志文件

    • lockback.spring.xml
    	<?xml version="1.0" encoding="UTF-8"?>
    	<configuration debug="false">
    		<!--定义日志文件的存储地址 -->
    		<property name="LOG_HOME" value="d:/logs" />
    
    		<!-- 控制台输出 -->
    		<appender name="STDOUT"
    		class="ch.qos.logback.core.ConsoleAppender">
    		<encoder
    			class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    			<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
    			<pattern>
    				%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n
    			</pattern>
    		</encoder>
    	</appender>
    	<!-- 按照每天生成日志文件 -->
    	<appender name="FILE"
    		class="ch.qos.logback.core.rolling.RollingFileAppender">
    		<rollingPolicy
    			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    			<!--日志文件输出的文件名 -->
    			<FileNamePattern>${LOG_HOME}/copote.%d{yyyy-MM-dd}.log
    			</FileNamePattern>
    			<!--日志文件保留天数 -->
    			<MaxHistory>30</MaxHistory>
    		</rollingPolicy>
    	<encoder
    			class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    			<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
    			<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n</pattern>
    		</encoder>
    		<!--日志文件最大的大小 -->
    		<triggeringPolicy
    			class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
    			<MaxFileSize>10MB</MaxFileSize>
    		</triggeringPolicy>
    	</appender>
    	<!-- 生成html格式日志开始 -->
    <appender name="HTML" class="ch.qos.logback.core.FileAppender">
    	<!-- 过滤器,只记录WARN级别的日志 -->
    	<!-- <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>info</level> 
    		<onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> -->
    
    	<encoder
    		class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
    		<layout class="ch.qos.logback.classic.html.HTMLLayout">
    			<pattern>%p%d%msg%M%F{32}%L</pattern>
    		</layout>
    	</encoder>
    	<file>${LOG_HOME}/error-log.html</file>
    </appender>
    <!-- 生成html格式日志结束 -->
    
    <!-- 每天生成一个html格式的日志开始 -->
    <appender name="FILE_HTML"
    	class="ch.qos.logback.core.rolling.RollingFileAppender">
    		<rollingPolicy
    			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    			<!--日志文件输出的文件名 -->
    			<FileNamePattern>${LOG_HOME}/copote.%d{yyyy-MM-dd}.html
    			</FileNamePattern>
    			<!--日志文件保留天数 -->
    			<MaxHistory>30</MaxHistory>
    		</rollingPolicy>
    		<encoder
    			class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
    			<layout class="ch.qos.logback.classic.html.HTMLLayout">
    			<pattern>%p%d%msg%M%F{32}%L</pattern>
    		</layout>
    		</encoder>
    		<!--日志文件最大的大小 -->
    		<triggeringPolicy
    			class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
    			<MaxFileSize>10MB</MaxFileSize>
    		</triggeringPolicy>
    		</appender>
    		<!-- 每天生成一个html格式的日志结束 -->
    		<!--myibatis log configure -->
    		<logger name="com.apache.ibatis" level="TRACE" />
    		<logger name="java.sql.Connection" level="DEBUG" />
    		<logger name="java.sql.Statement" level="DEBUG" />
    		<logger name="java.sql.PreparedStatement" level="DEBUG" />
    
    		<!-- 日志输出级别 -->
    		<root level="INFO">
    			<appender-ref ref="STDOUT" />
    			<appender-ref ref="FILE" />
    			<appender-ref ref="HTML" />
    			<appender-ref ref="FILE_HTML" />
    		</root>
    	</configuration>
    
  2. 新建application.yml文件

    注:编写yml文件时,冒号后面一定要加空格

    • 报错1 The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property)

    解决方法:

     在jdbc url后加上useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    
    • 报错2 com.mysql.jdbc.Driver 标红

      解决方法:

      原因:未引入jdbc依赖

        <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
        </dependency>
    
  3. 工程就可以启动啦

  4. 小菜鸟一枚,请多多指教~