sping boot mybatis-plus添加打印mapping.xml执行的sql
时间: 2024-03-11 17:50:29 浏览: 64
在 Spring Boot 中使用 MyBatis-Plus,可以通过在配置文件中配置 MyBatis 的日志来打印 SQL 语句。具体步骤如下:
1. 在 application.properties 或 application.yml 配置文件中,添加以下配置:
```
# 配置 MyBatis-Plus 的日志级别为 debug
logging.level.com.baomidou.mybatisplus.mapper=debug
```
其中,`com.baomidou.mybatisplus.mapper` 是 MyBatis-Plus 的 Mapper 接口所在的包名。这样配置之后,MyBatis-Plus 将会输出 SQL 语句和执行时间等信息到控制台。
2. 如果你想在日志中显示完整的 SQL 语句,可以在 MyBatis 的配置文件中添加以下配置:
```xml
<configuration>
<settings>
<!-- 显示完整的 SQL 语句 -->
<setting name="logImpl" value="LOG4J2" />
</settings>
<!-- 其他配置 -->
</configuration>
```
这样配置之后,MyBatis-Plus 将会在日志中显示完整的 SQL 语句。
注意,在开发环境中可以开启 SQL 日志来方便调试,但是在生产环境中一定要关闭 SQL 日志,以保护数据的安全性。
相关问题
sping boot mybatis配置文件在哪
在 Spring Boot 中使用 MyBatis,可以在 application.properties 或 application.yml 配置文件中配置 MyBatis 相关的属性。
如果你使用的是 application.properties 文件,则可以在文件中添加以下属性:
```
# MyBatis 配置
mybatis.config-location=classpath:mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.domain
```
如果你使用的是 application.yml 文件,则可以在文件中添加以下属性:
```yaml
mybatis:
config-location: classpath:mybatis-config.xml
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.domain
```
其中,`mybatis.config-location` 属性用于指定 MyBatis 的配置文件,`mybatis.mapper-locations` 属性用于指定 Mapper 文件的路径,`mybatis.type-aliases-package` 属性用于指定实体类的路径。
如果你的 MyBatis 配置文件和 Mapper 文件都放在了默认的位置(即 classpath:mybatis-config.xml 和 classpath:mapper/*.xml),则可以省略这些属性。
注意,在 Spring Boot 中使用 MyBatis 时,一定要保证 MyBatis 的配置文件和 Mapper 文件都放在类路径下,否则会出现找不到文件的错误。
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'username' in 'class com.example.sping_boot_mybatis.pojs.User'
这个异常的原因是在`com.example.sping_boot_mybatis.pojs.User`类中没有名为`username`的属性的getter方法。MyBatis在映射结果到对象时,会使用反射来设置属性的值,但是如果没有对应的getter方法,就无法设置该属性的值,从而导致此异常的抛出。
要解决这个问题,你可以检查`User`类中是否存在名为`username`的属性,并确保该属性有对应的getter方法。如果不存在,你可以添加一个名为`getUsername()`的方法来获取`username`属性的值。
示例代码:
```java
public class User {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
```
确保`User`类中有正确的属性和对应的getter方法后,再次运行代码应该就不会再出现该异常了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)