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

mac2022-06-30  97

环境:

Spring-boot:2.1.2IDEA 2018.3MAVEN 3.6.0

1.热部署(hot swapping)

加入依赖:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>

一些设置:

 

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命令,可以避免上述问题:

 4.以MySQL为例连接数据库

//首先添加依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> //然后再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<TvSeries> getAll(); } @Service @Transactional public class TvSeriesService { @Autowired private TvSeriesDao tvSeriesDao; public List<TvSeries> 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<TvSeries> 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():被调用了

  

转载于:https://www.cnblogs.com/wanyi/p/10352868.html

最新回复(0)