[Mybatis] SpringBoot配置Mybatis

本文介绍了如何在Spring Boot应用中集成MyBatis,通过添加mybatis-spring-boot-starter依赖,配置数据源,并创建UserMapper接口实现数据的增删操作。启动类通过@MapperScan注解扫描Mapper接口,简化了项目的初始化工作。
摘要由CSDN通过智能技术生成
代码

引入依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>

配置数据源

spring:
    datasource:
      url: jdbc:mysql://127.0.0.1:3306/auto?useUnicode=true&useSSL=false&characterEncoding=utf8
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver

创建Mapper

public interface UserMapper {
    @Select("SELECT * FROM Person WHERE id = #{id}")
    Person selectUser(int id);
 
    @Delete("delete from person where id = #{id}")
    void deleteByPrimaryKey(int id);
}

启动类

// 扫描该包下的Mapper
@MapperScan("com.fendo.auto.mapper") 
@SpringBootApplication
public class AutoApplication {
    public static void main(String[] args) {
        SpringApplication.run(AutoApplication.class, args);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值