数据库表之间的关系说明
设计数据库表
#创建数据库
create database WechatOrder;
#切换数据库
use WechatOrder;
#商品表
create table `product_info`(
`product_id` varchar(32) not null,
`product_name` varchar(64) not null comment '商品名称',
`product_price` decimal(8,2) not null comment '单价',
`product_stock` int not null comment '库存',
`product_description` varchar(64) comment '描述',
`product_icon` varchar(512) comment '小图',
`category_type` int not null comment '类目编号',
`create_time` timestamp not null default current_timestamp comment '创建时间',#timestamp是时间戳
`update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',#设置当修改的时候自动记录时间
primary key(`product_id`)
)comment '商品表';
#类目表
create table `product_category`(
`category_id` int not null auto_increment,
`category_name` varchar(64) not null comment '类目名字',
`category_type` int not null comment '类目编号',
`create_time` timestamp not null default current_timestamp comment '创建时间',#timestamp是时间戳
`update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',#设置当修改的时候自动记录时间
primary key(`category_id`),
unique key `uqe_category_type` (`category_type`)#设置唯一约束
)comment '类目表';
#订单主表
create table `order_master`(
`order_id` varchar(32) not null,
`buyer_name` varchar(32) not null comment '买家名字',
`buyer_phone` varchar(32) not null comment '买家电话',
`buyer_address` varchar(128) not null comment '买家地址',
`buyer_openid` varchar(64) not null comment '买家微信openid',
`order_amount` decimal(8,2) not null comment '订单总金额',
`order_status` tinyint(3) not null default '0' comment '订单状态,默认0新下单',
`pay_status` tinyint(3) not null default '0' comment '支付状态,默认0未支付',
`create_time` timestamp not null default current_timestamp comment '创建时间',#timestamp是时间戳
`update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',#设置当修改的时候自动记录时间
primary key(`order_id`),
key `idx_buyer_openid`(`buyer_openid`)
)comment '订单主表';
#订单详情表
create table `order_detail`(
`detail_id` varchar(32) not null,
`order_id` varchar(32) not null,
`product_id` varchar(32) not null,
`product_name` varchar(64) not null comment '商品名称',
`product_price` decimal(8,2) not null comment '商品价格',
`product_quantity` int not null comment '商品数量',
`product_icon` varchar(512) comment '商品小图',
`create_time` timestamp not null default current_timestamp comment '创建时间',#timestamp是时间戳
`update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',#设置当修改的时候自动记录时间
primary key(`detail_id`),
key `idx_order_id` (`order_id`)
)comment '订单详情表';
什么是日志框架?
日志框架的能力
System.out
只能将日志输出到控制台)常见的日志框架
log.in
,简称JULcommon log in
,简称JCL日志门面 | 日志实现 |
---|---|
JCL | Log4j |
SLF4j | Log4j2 |
Jboss-logging | Logback |
JUL |
使用SLF4j实例代码
package com.wechatorder.sell.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class LoggerTest {
private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);//参数是当前类,表示记录日志的类。可以在类上使用@Slf4j注解替代。
@Test
public void test1(){
logger.debug("debug...");//没有输出内容,因为系统默认的日志级别是info,在info之上的可以输出出来
logger.info("info...");//2018-09-27 20:09:57.730 INFO 1519 --- [ main] com.wechatorder.sell.service.LoggerTest : info...
logger.error("error...");//2018-09-27 20:09:57.730 ERROR 1519 --- [ main] com.wechatorder.sell.service.LoggerTest : error...
}
}
org.slf4j.event.Level
中的等级分布
等级 | 级别(越大级别越高) |
---|---|
ERROR | 40 |
WARN | 30 |
INFO | 20 |
DEBUG | 10 |
TRACE | 0 |
在类上使用@Slf4j注解替代LoggerFactory.getLogger(LoggerTest.class)
需引入maven依赖
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
logger可以使用log替换
替换后的代码
package com.wechatorder.sell.service;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LoggerTest {
@Test
public void test1(){
log.debug("debug...");//没有输出内容,因为系统默认的日志级别是info,在info之上的可以输出出来
log.info("info...");//2018-09-27 20:34:14.844 INFO 1661 --- [ main] com.wechatorder.sell.service.LoggerTest : info...
log.error("error...");//2018-09-27 20:34:14.844 ERROR 1661 --- [ main] com.wechatorder.sell.service.LoggerTest : error...
}
}
打印日志中使用变量,实例代码({}
表示占位符)
package com.wechatorder.sell.service;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LoggerTest {
@Test
public void test1(){
String username = "Jack";
String perference = "smart";
log.debug("debug...");//没有输出内容,因为系统默认的日志级别是info,在info之上的可以输出出来
log.info("info...");//2018-09-27 20:09:57.730 INFO 1519 --- [ main] com.wechatorder.sell.service.LoggerTest : info...
log.error("error...");//2018-09-27 20:09:57.730 ERROR 1519 --- [ main] com.wechatorder.sell.service.LoggerTest : error...
log.info("Your name is {}, your perference is {}",username,perference);//2018-09-27 20:38:51.486 INFO 1683 --- [ main] com.wechatorder.sell.service.LoggerTest : Your name is Jack, your perference is smart
}
}
Logback的使用与配置
application.yml
中配置
logback-spring.xml
中配置
Logback的配置需求:
区分info和error日志
每天产生一个日志文件(便于日后定位问题,查找方便)
配置application.yml
的logging:pattern:console
实现按照指定格式输出日志【演示功能,实际实现上述需求请参见下方对logback-spring.xml
的配置】
#%d日期 %msg日志信息 %n换行
logging:
pattern:
console: "%d - %msg%n"
#path:/var/log/tomcat #会输出spring.log日志文件到此路径(文件中记录日志)
file:/var/log/tomcat/sell.log #会输出指定日志文件名sell.log的文件到此路径(文件中记录日志)
level:debug
2018-09-27 20:45:49,760 - info...
2018-09-27 20:45:49,760 - error...
2018-09-27 20:45:49,760 - Your name is Jack, your perference is smart
logging:path:/var/log/tomcat
会输出spring.log日志文件到此路径(文件中记录日志信息)logging:file:/var/log/tomcat/sell.log
会输出指定日志文件名sell.log的文件到此路径(文件中记录日志信息)【通常我们会采用file,因为可以自定义文件名。且file和path二选一即可】logging:level:debug
将默认的日志级别从info变成debug(也可以指定文件的日志级别:logging:level:com.wechatorder.sell.service.LoggerTest:debug
)配置logback-spring.xml
实现按照指定格式输出日志(如resources文件夹下没有则自行创建)
<configuration>
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d - %msg%n
pattern>
layout>
appender>
<appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERRORlevel>
<onMatch>DENYonMatch>
<onMismatch>ACCEPTonMismatch>
filter>
<encoder>
<pattern>
%msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/Users/wangzhe/Desktop/info.%d.logfileNamePattern>
rollingPolicy>
appender>
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERRORlevel>
filter>
<encoder>
<pattern>
%msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/Users/wangzhe/Desktop/error.%d.logfileNamePattern>
rollingPolicy>
appender>
<root level="info">
<appender-ref ref="consoleLog">appender-ref>
<appender-ref ref="fileInfoLog">appender-ref>
<appender-ref ref="fileErrorLog">appender-ref>
root>
configuration>
info.2018-09-27.log
Starting LoggerTest on Jack.local with PID 1936 (started by wangzhe in /Users/wangzhe/Practice/微信点餐系统)
No active profile set, falling back to default profiles: default
Refreshing org.springframework.web.context.support.GenericWebApplicationContext@13d73f29: startup date [Thu Sep 27 21:32:09 CST 2018]; root of context hierarchy
Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@13d73f29: startup date [Thu Sep 27 21:32:09 CST 2018]; root of context hierarchy
Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
Started LoggerTest in 4.076 seconds (JVM running for 5.469)
info...
Your name is Jack, your perference is smart
Closing org.springframework.web.context.support.GenericWebApplicationContext@13d73f29: startup date [Thu Sep 27 21:32:09 CST 2018]; root of context hierarchy
error.2018-09-27.log
error...