springboot集成mybatis步骤总结

Spring Boot 集成 MyBatis 主要包括以下几个步骤:

  1. 添加依赖:在你的 pom.xml 文件中添加 Spring Boot 和 MyBatis 的起步依赖(starter dependency)。
    <!-- Spring Boot起步依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- MyBatis起步依赖 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>你的版本号</version>
    </dependency>
    <!-- 数据库驱动依赖,以MySQL为例 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>你的版本号</version>
    </dependency>
    
  2. 配置数据源:在 application.propertiesapplication.yml 文件中配置数据库连接信息。
    # application.properties
    spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC
    spring.datasource.username=your_username
    spring.datasource.password=your_password
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
  3. 配置MyBatis:在配置文件中配置 MyBatis 相关设置,例如类型别名、映射器等。
    # application.properties
    mybatis.type-aliases-package=你的实体类所在包
    mybatis.mapper-locations=classpath:mapper/*.xml
    
  4. 创建实体类:根据你的数据库表创建对应的实体类(Entity)。
  5. 创建映射文件:创建 MyBatis 的映射文件(XML Mapper),定义 SQL 语句和映射规则。
    <!-- 在 resources/mapper/YourEntityMapper.xml -->
    <mapper namespace="你的Mapper接口路径">
        <select id="selectById" resultType="你的实体类路径">
            SELECT * FROM your_table WHERE id = #{id}
        </select>
    </mapper>
    
  6. 创建Mapper接口:定义一个接口用于操作数据库,并使用 @Mapper 注解或在启动类上使用 @MapperScan 指定扫描的Mapper接口路径。
    // @Mapper 或者在启动类上使用 @MapperScan
    public interface YourEntityMapper {
        YourEntity selectById(int id);
    }
    
  7. 启动类配置:确保你的 Spring Boot 启动类上添加了 @SpringBootApplication 注解,并在适当的地方使用 @MapperScan 注解。
    @SpringBootApplication
    @MapperScan("你的Mapper接口所在包")
    public class YourApplication {
        public static void main(String[] args) {
            SpringApplication.run(YourApplication.class, args);
        }
    }
    
  8. 编写服务层和控制器:创建服务层(Service)调用 Mapper 接口中的方法,并在控制器(Controller)中调用服务层逻辑。
  9. 测试:编写测试用例,确保集成无误。
    按照以上步骤操作后,你的 Spring Boot 应用应该能够成功集成 MyBatis,并且可以通过 MyBatis 的 Mapper 接口操作数据库。记得根据实际情况调整依赖版本和配置细节。
  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
springboot集成mybatis框架的步骤如下: 1. 首先,在项目的pom.xml文件中添加mybatis的依赖。你可以使用以下的依赖配置来添加mybatis-spring-boot-starter依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> ``` 2. 然后,在springboot的启动类上添加@MapperScan注解,指定mapper接口的包名,让springboot能够扫描到这些接口: ```java @MapperScan(basePackages = "mapper类所放的包名") @SpringBootApplication public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 3. 接下来,需要进行mybatis的配置。在application.properties或application.yml文件中添加mybatis的相关配置: ```yaml # mybatis配置 mybatis: check-config-location: true # 检查mybatis的配置文件位置 config-location: "classpath:mybatis/mybatis-config.xml" # mybatis的配置文件路径 mapper-locations: "classpath:mybatis/mapper/*Mapper.xml" # mapper接口的xml文件路径 type-aliases-package: "com.example.awesomespring.dao.entity.*" # mapper接口对应的实体类路径 ``` 通过以上步骤,你就成功地集成mybatis框架到springboot项目中。这样,你就可以使用mybatis来进行数据库的持久化操作了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [手把手教你springboot集成mybatis](https://blog.csdn.net/Trouvailless/article/details/126315399)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值