springboot+mysql+mybatis如何实现控制台打印sql

在Spring Boot中使用MyBatis与MySQL,并希望在控制台打印SQL语句,你可以通过配置MyBatis的日志级别来实现。以下是具体步骤:

  1. 添加依赖:

确保你的项目中包含了MyBatis和MySQL的相关依赖。在pom.xml文件中添加如下依赖:

xml 复制代码
<!-- MyBatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version> <!-- 请根据实际需要选择版本 -->
</dependency>

<!-- MySQL Connector -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.27</version> <!-- 请根据实际需要选择版本 -->
</dependency>
  1. 配置application.properties或application.yml:

application.propertiesapplication.yml中添加数据库连接配置:

properties 复制代码
# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  1. 配置MyBatis:

application.propertiesapplication.yml中添加以下配置,开启MyBatis的日志输出:

properties 复制代码
# MyBatis配置
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

这里使用org.apache.ibatis.logging.stdout.StdOutImpl作为日志实现,它会将日志输出到控制台。

  1. 编写MyBatis映射文件和DAO:

编写MyBatis映射文件(Mapper XML)和相应的DAO接口,实现你的SQL操作。

  1. 运行项目:

启动你的Spring Boot应用程序,MyBatis会在控制台打印SQL语句。

注意:在生产环境中,避免将日志级别设置为DEBUG,因为它可能会导致输出大量敏感信息。在生产环境中,你可以将日志级别设置为INFO。

相关推荐
while(1){yan}10 分钟前
MYSQL索引的底层数据结构
数据结构·数据库·mysql
by__csdn12 分钟前
Spring Boot 全面解析
java·数据库·spring boot·后端·spring
合作小小程序员小小店20 分钟前
web网页开发,在线%人力资源管理%系统,基于Idea,html,css,jQuery,java,jsp,ssh,mysql。
java·前端·css·数据库·mysql·html·intellij-idea
vx_bisheyuange2 小时前
基于SpringBoot的宠物商城网站的设计与实现
spring boot·后端·宠物
一个处女座的程序猿O(∩_∩)O2 小时前
Spring Boot、Redis、RabbitMQ 在项目中的核心作用详解
spring boot·redis·java-rabbitmq
元Y亨H4 小时前
Spring Boot 路由踩坑:当“通配符”吞掉了你的“固定路径”
spring boot
The best are water4 小时前
MySQL FEDERATED引擎跨服务器数据同步完整方案
android·服务器·mysql
沐浴露z5 小时前
一张思维导图理清 【MySQL】
数据库·mysql
q***65695 小时前
使用 java -jar 命令启动 Spring Boot 应用时,指定特定的配置文件的几种实现方式
java·spring boot·jar
q***84575 小时前
Java进阶,时间与日期,包装类,正则表达式
java·mysql·正则表达式