mybatis报错整理

mybatis报错整理

1. 报错一
org.apache.ibatis.binding.BindingException: Type interface spring.wmm.mapper.StudentMapper is not known to the MapperRegistry.
从报错信息的解读我们可以看见报错信息中可以看出几个关键词1.interface(接口)2.StudentMapper 3.not known(不知道) MapperRegistry(映射注册),从几个字面意思我们可以大概知道是跟我们编写的接口有关,说明我们的接口和映射文件之间没有进行绑定。
上面提到的StudentMapper是自己编写的接口,非固定词,如我在项目中的接口名
在这里插入图片描述
解决问题:
打开该mybatis的全局配置文件,在里面添加如下内容:
在这里插入图片描述

  1. 报错二
    org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.(大概翻译:服务器时区值“äÐúêêê×¼Êêêêêêêêêêêê。如果要利用时区支持,必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)以使用更具体的时区值)### The error may exist in spring/wmm/mapper/StudentMapper.xml### The error may involve spring.wmm.mapper.StudentMapper.stusList### The error occurred while executing a query### Cause: java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
    遇到这种异常只需要在我们项目mybatis全局配置文件中的
    jdbc:mysql://localhost:3306/servlet后面添加上?serverTimezone=UTC
    "jdbc:mysql://localhost:3306/servlet?serverTimezone=UTC”
    在这里插入图片描述

  2. 报错三:
    Error building SqlSession.### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 1 字节的 UTF-8 序列的字节 1 无效。
    出现这种错误是因为我们在mybatisconfig.xml配置文件中添加中文(也包括注释),只需将mybatis全局配置文件中更改运行字符编码把UTF-8改为GDK;
    在这里插入图片描述

  3. 报错四:
    Error building SqlSession.
    The error may exist in spring/wmm/mapper/StudentMapper.xml
    #Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource spring/wmm/mapper/StudentMapper.xml

    出现这种异常说明我们编写的.xml文件没有被maven加载进target文件中。
    补充target文件是干什么的:target文件夹是用来存放项目构建后的文件和目录、jar包、war包、编译的class文件,所有都是maven构建是生成的。
    出现之类问题我们只需在pom.xml文件中加人如下代码:
    !–src/main/resources下.properties和.xml文件无法被导出或者生效对的问题,–>

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>
  1. 报错五
    ##Error building SqlSession.
    Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 16; 文档根元素 “configuration” 必须匹配 DOCTYPE 根 “null”。

    出现这种异常是因为mybatis全局配置中的头部配置缺少引发的
    在这里插入图片描述
<think>好的,我需要帮助用户找到如何在项目中添加MySQL和MyBatis的Maven或Gradle依赖配置示例。首先,我应该回忆用户提供的引用内容,看看是否有相关的依赖配置信息。 用户引用了四个来源,其中引用[1]提到使用Spring Boot、MyBatis和Maven,引用[2]和[3]涉及MyBatis Generator的配置,引用[4]提到了Spring Boot版本和依赖管理。这些信息可能包含所需的依赖配置。 首先,Maven依赖部分。对于Spring Boot项目,通常需要spring-boot-starter-parent作为父项目,然后添加spring-boot-starter-web、mybatis-spring-boot-starter,以及MySQL驱动。引用[4]中提到了springBootVersion,这可能是指定版本的地方,所以应该在properties中定义版本号。引用[1]提到使用MyBatis进行CRUD操作,所以需要mybatis-spring-boot-starter依赖。MySQL驱动则是mysql-connector-java,需要指定版本,比如8.x。 接下来是Gradle部分。引用[3]展示了Gradle中如何添加MyBatis Generator插件,而引用[4]提到dependencies的配置方式。Gradle的依赖应该包括spring-boot-starter-web、mybatis-spring-boot-starter,以及MySQL驱动。同样需要指定版本,可能在build.gradle的ext里设置版本变量,如mysqlVersion和mybatisVersion。 另外,用户可能需要配置数据源,比如在application.yml中设置数据库连接信息。引用[4]提到了application.yml,所以应该补充这部分配置示例。同时,MyBatis的Mapper扫描路径和配置XML文件的位置也是常见的设置,需要包括进去。 还要考虑用户是否使用MyBatis Generator,如引用[2]和[3]提到的,可能需要添加相关插件配置,但用户的问题主要是依赖配置,所以这部分可能作为额外补充。 需要确保所有依赖的版本兼容,比如Spring Boot 2.5.4对应的MyBatis Starter和MySQL驱动版本是否匹配。同时,检查是否有遗漏的依赖项,比如Spring Boot的starter-data-jpa可能不需要,但用户用的是MyBatis,所以应该不需要JPA相关的依赖。 最后,整理出Maven和Gradle的依赖配置示例,并给出数据源和MyBatis的基本配置,确保用户能够顺利集成MySQL和MyBatis。</think>### Maven 依赖配置示例 在 `pom.xml` 中添加以下依赖: ```xml <!-- Spring Boot 基础依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.4</version> </parent> <dependencies> <!-- Web 支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis 集成 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- MySQL 驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> </dependencies> ``` ### Gradle 依赖配置示例 在 `build.gradle` 中添加以下配置: ```groovy plugins { id 'org.springframework.boot' version '2.5.4' } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0' implementation 'mysql:mysql-connector-java:8.0.26' } // 可选:MyBatis Generator 插件配置 buildscript { repositories { maven { url "https://plugins.gradle.org/m2/" } } dependencies { classpath "gradle.plugin.com.arenagod.gradle:mybatis-generator-plugin:1.4" } } apply plugin: "com.arenagod.gradle.MybatisGenerator" ``` ### 关键配置说明 1. **数据库连接配置** 在 `application.yml` 中配置数据源和 MyBatis: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml # 指定 XML 映射文件路径 configuration: map-underscore-to-camel-case: true # 自动驼峰命名转换 ``` 2. **MyBatis Mapper 扫描** 在 Spring Boot 主类添加注解: ```java @MapperScan("com.example.mapper") // 指定 Mapper 接口包路径 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` ### 验证依赖是否生效 1. 运行 `mvn clean install` (Maven) 或 `gradle build` (Gradle) 检查依赖下载 2. 查看控制台是否报错(如驱动类加载失败) 3. 编写简单 SQL 查询验证数据库连接[^1][^4]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值