Mybatis Plus YYDS!
Thanks to my colleague Dr. L for pointing me in this content!
Ancestral chatter
When we use MybatisPlus for development, log output is very important for debugging and performance monitoring. MybatisPlus uses Log4j for log output by default, but if our project does not use Log4j, we can configure it log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
to output the log to the console.
Code that can be copied after modification
First, add the following configuration in application.yml
or :application.properties
mybatis-plus:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath*:mapper/**/*.xml
type-aliases-package: com.example.entity
global-config:
db-config:
id-type: auto
banner: false
sql-parser-cache: false
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
复制代码
Among them, log-impl
the implementation class of the log output is specified org.apache.ibatis.logging.stdout.StdOutImpl
, that is, the console output. When the log is output, we can see the following output information:
2023-04-12 23:03:27.720 INFO 1126 --- [nio-8019-exec-1] c.b.p.controller.HeroController : ==> Preparing: SELECT * FROM hero WHERE id = ?
2023-04-12 23:03:27.721 INFO 1126 --- [nio-8019-exec-1] c.b.p.controller.HeroController : ==> Parameters: 1(Integer)
2023-04-12 23:03:27.724 INFO 1126 --- [nio-8019-exec-1] c.b.p.controller.HeroController : <== Total: 1
复制代码
As you can see, MybatisPlus outputs both SQL statements and parameters to the console.
The following is a complete Controller sample code for reference:
@RestController
@RequestMapping("/hero")
public class HeroController {
@Autowired
private HeroService heroService;
@GetMapping("/{id}")
public Hero getById(@PathVariable Integer id) {
return heroService.getById(id);
}
}
复制代码
Among them, the method of MybatisPlus is called in HeroService getById
:
@Service
public class HeroServiceImpl extends ServiceImpl<HeroMapper, Hero> implements HeroService {
@Override
public Hero getById(Serializable id) {
log.info("getById - id = {}", id);
return super.getById(id);
}
}
复制代码
In getById
the method, we added custom log output information for future debugging. At the same time, we configured log-impl
the console output so that we can easily view the SQL log on the console.
references
- MybatisPlus官方文档:https://baomidou.com
复制代码