spring日志mysql_[spring-boot] 热部署/时间格式化/模拟请求/连接MySQL/日志记录

环境:

Spring-boot:2.1.2

IDEA 2018.3

MAVEN 3.6.0

1.热部署(hot swapping)

加入依赖:

org.springframework.boot

spring-boot-devtools

true

一些设置:

7d84b8d58d07c4b0494ba0d5411ba858.png

5de000b785e37d8e2d68be3f460a2af6.png

2.对Date类型进行时间格式化,变成东八区时间:

@JsonFormat(timezone="GMT+8", pattern="yyyy-MM-dd")

private Date originRelease;

3.模拟http请求

建议用postman,因为curl的命令在windows下,在post请求,--data后面,本来的单引号要改成两个双引号,双引号要改成三个双引号,不然会报JSON Parse Error

curl -v localhost:8080/tvseries -H "Content-Type:application/json" -X POST --data ""{"""id""":1

000,"""originRelease""":"""2019-08-08"""}""

建议用postman软件代替在命令行中输入curl命令,可以避免上述问题:

5dad5bfb21a7fa10b490b36eeaf74340.png

4.以MySQL为例连接数据库

//首先添加依赖:

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

mysql

mysql-connector-java

8.0.13

//然后再application.yml中添加数据库相关配置:

spring:

datasource:

driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8

&rewriteBatchedStatements=true&chara

username: root

password: 1234

@Repository

public interface TvSeriesDao

{

@Select("select * from tvseries")

List getAll();

}

@Service

@Transactional

public class TvSeriesService

{

@Autowired private TvSeriesDao tvSeriesDao;

public List getAllTvSeries() {

return tvSeriesDao.getAll();

}

}

//@MapperScan扫描dao所在类

@SpringBootApplication

@MapperScan("com.abc.bootdemo.dao")

public class Demo4Application

{

public static void main(String[] args)

{

SpringApplication.run(Demo4Application.class, args);

}

}

5.日志记录

application.yml:

logging:

file: target/app.log

level:

ROOT: warn

com.abc: trace

在方法中使用log:

@RestController

@RequestMapping("/tvseries")

public class TvSeriesController

{

private static final Log log = LogFactory.getLog(TvSeriesController.class);

@Autowired

TvSeriesService tvSeriesService;

@GetMapping

public List getAll() {

if(log.isTraceEnabled()) {

log.trace("getAll():被调用了");

}

return tvSeriesService.getAllTvSeries();

}

}

console会输出:

2019-02-06 00:03:16.067 TRACE 668868 --- [nio-8080-exec-6] c.a.b.controller.TvSeriesController : getAll():被调用了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值