spring boot笔记2——spring boot整合mybatis

本文详细介绍了如何在SpringBoot项目中整合MyBatis,包括添加依赖、配置启动类、设置MyBatis属性及使用Mapper接口。通过具体步骤,帮助开发者快速实现SpringBoot与MyBatis的整合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、spring boot整合mybatis首先需要添加依赖,pom需要的部分如下:

<!-- spring boot的配置 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

<dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

二、启动类设置:

@SpringBootApplication     启动项目设置
@ComponentScan(basePackages={"com.unicom.springboot.controller","com.unicom.springboot.service"})     controller、service扫描相关package
@MapperScan(value="com.unicom.springboot.mapper")             扫描mapper的接口
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
}

三、在application.properties中设置mybatis属性(数据库部分略):

mybatis.typeAliasesPackage=com.unicom.springboot.model        //相当于设置mybatis的config.xml属性,扫哪个包
mybatis.mapperLocations=classpath\:mapper/*.xml         //寻找mapper下的mapper.xml

四、对UserMapper进行处理,加上@Mapper

@Mapper
public interface UserMapper {

 

如此配置就可以实现spring boot与mybatis的整合。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值