pom.xml中引入MyBatis核心依赖

本文档展示了一个Maven项目的配置,主要涉及MyBatis 3.4.6版本与MySQL 5.1.47驱动的依赖集成。项目组名为`com.qf`,项目名为`hello-mybatis`,版本为`1.0-SNAPSHOT`。通过Maven的`dependencies`标签引入了MyBatis和MySQL驱动两个依赖,用于实现数据库操作。
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation=
         "http://maven.apache.org/POM/4.0.0
          http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>

    <!--项目配置-->
    <groupId>com.qf</groupId>
    <artifactId>hello-mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--依赖-->
    <dependencies>
        <!--MyBatis核心依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>

        <!--MySql驱动依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
      </dependencies>
</project>

<think>我们正在讨论如何在Maven项目的pom.xml文件中添加MyBatis和MySQL驱动的依赖。 根据引用[2],我们可以直接看到添加MyBatis和MySQL驱动的依赖代码块。 但是,我们也要注意引用[1]中提到的多模块项目管理结构,即根目录的pom.xml管理依赖版本,子模块引入具体依赖。 因此,我们需要考虑两种情况:单模块项目和多模块项目。 对于单模块项目,我们直接在项目的pom.xml文件中添加依赖。 对于多模块项目,通常的做法是在根pom.xml中通过依赖管理(dependencyManagement)定义依赖版本,然后在子模块中引入依赖(不需要指定版本)。 由于用户没有明确说明项目结构,我们可以提供两种方式: 1. 单模块项目:直接在dependencies中添加 2. 多模块项目:在父pom中管理版本,在子模块中引入 另外,我们需要注意Spring Boot项目通常使用Spring Boot提供的依赖管理,所以我们可以利用Spring Boot Starter来简化配置。 引用[2]中给出的依赖是直接指定了MyBatis和MySQL驱动的版本,但在Spring Boot项目中,我们更推荐使用Spring Boot Starter相关的依赖,并让Spring Boot管理版本。 因此,我们给出两种方案: 方案一:非Spring Boot项目(纯Maven项目)添加依赖 方案二:Spring Boot项目添加依赖 由于用户提到的是Spring Boot项目(根据引用[2]中的描述),我们优先给出Spring Boot项目中的添加方法。 在Spring Boot项目中,我们通常使用Spring Boot提供的MyBatis Starter和MySQL驱动依赖,并且不需要指定版本(因为Spring Boot的父pom已经管理了版本)。 步骤: 1.pom.xml的dependencies部分添加MyBatis Spring Boot Starter和MySQL驱动的依赖。 2. 如果项目是多模块的,且父pom已经使用了Spring Boot的dependencyManagement,那么子模块可以直接添加依赖而不指定版本。 具体依赖如下: 对于MyBatis,我们使用:mybatis-spring-boot-starter 对于MySQL,我们使用:mysql-connector-java 但是注意,在Spring Boot项目中,我们通常这样添加: ```xml <dependencies> <!-- MyBatis Starter --> <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> <!-- 同样,如果父项目有版本管理,这里省略版本号 --> <!-- 在Spring Boot项目中,通常父项目已经管理了版本,所以可以省略 --> </dependency> </dependencies> ``` 然而,如果项目是使用Spring Boot Initializr创建的,那么父pom中已经包含了版本管理,因此我们可以省略版本号。 但是,我们也要考虑到用户可能没有使用Spring Boot的父pom,而是自己管理版本。所以,我们给出更通用的建议: 建议1:如果是Spring Boot项目,并且父项目是spring-boot-starter-parent,那么可以省略版本号。 建议2:如果不是,那么需要显式指定版本号,或者在自己的dependencyManagement中管理版本。 另外,根据引用[2]中的示例,他们直接指定了版本号(mybatis:3.5.0,mysql-connector-java:8.0.15)。但请注意,在Spring Boot项目中,使用mybatis-spring-boot-starter会间接引入mybatismybatis-spring,并且版本是经过测试兼容的,所以推荐使用starter。 因此,我们总结如下: 在Spring Boot项目中,添加MyBatis和MySQL依赖的步骤如下: 1. 打开项目的pom.xml文件。 2. 在<dependencies>标签内添加以下依赖(如果已有<dependencies>,则直接在其中添加): ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> <!-- 如果父项目是Spring Boot,可以省略版本 --> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <!-- 通常设置为runtime,表示在运行时需要 --> <!-- 如果父项目是Spring Boot,可以省略版本 --> </dependency> ``` 3. 如果项目是多模块的,并且父模块已经通过dependencyManagement管理了这些依赖的版本,那么子模块中不需要指定版本。 注意:版本选择 - 对于Spring Boot 2.x,通常使用mybatis-spring-boot-starter 2.x版本。 - 对于Spring Boot 1.x,使用mybatis-spring-boot-starter 1.x版本。 如果用户没有使用Spring Boot,那么可以按照引用[2]中的方式添加普通依赖,但要注意版本兼容性。 非Spring Boot项目的依赖添加: ```xml <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> </dependencies> ``` 最后,我们还要提醒用户,在添加了MySQL驱动后,需要在配置文件中(如application.properties)配置数据库连接信息。 例如: ``` spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 对于MyBatis的配置,可以在application.properties中配置MyBatis的相关属性,如: ``` mybatis.mapper-locations=classpath:mapper/*.xml ``` 总结回答: 我们将分情况说明如何在pom.xml中添加MyBatis和MySQL驱动依赖,并给出配置示例。 </think> ### 如何在 pom.xml 中添加 MyBatis 和 MySQL 驱动依赖 #### 一、基础依赖配置(单模块项目) 在项目的 `pom.xml` 文件中添加以下依赖(**Spring Boot 推荐方式**): ```xml <dependencies> <!-- MyBatis Spring Boot Starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> <!-- 与Spring Boot 3.x兼容 --> </dependency> <!-- MySQL 驱动 --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> <!-- 建议设置为runtime --> </dependency> </dependencies> ``` **说明**: 1. `mybatis-spring-boot-starter` 自动包含 MyBatis 核心库和 Spring 整合模块,无需单独配置 MyBatis 版本 2. `mysql-connector-j` 是 MySQL 官方 JDBC 驱动(8.0+) 3. 若使用 Spring Boot 父工程,MySQL 版本可省略(由 Spring Boot 统一管理)[^1][^2] --- #### 二、多模块项目配置 在**父模块**的 `pom.xml` 中统一管理版本: ```xml <!-- 父模块 pom.xml --> <dependencyManagement> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.0.33</version> </dependency> </dependencies> </dependencyManagement> ``` 在**子模块**的 `pom.xml` 中简化为: ```xml <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> </dependencies> ``` 通过父模块集中管理版本,确保依赖一致性。 --- #### 三、非 Spring Boot 项目配置 若未使用 Spring Boot,需手动指定所有依赖: ```xml <dependencies> <!-- MyBatis 核心 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.13</version> </dependency> <!-- MySQL 驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <!-- 若需 Spring 整合 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>3.0.3</version> </dependency> </dependencies> ``` --- #### 四、验证与配置 1. **生效验证**: 添加依赖后执行 Maven 命令: ```bash mvn clean install ``` 2. **必要配置**: 在 `application.properties` 中添加数据库连接: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/your_db?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=your_password ``` > **注意**:MySQL 8.0+ 必须指定时区(如 `serverTimezone=UTC`),否则可能报时区错误[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值