Springboot整合Mybatis

引入依赖

首先我们需要在pom文件中引入mybatis自动配置的依赖

注意:mybatis自动配置的依赖一定要写版本号

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.11</version>
    </dependency>
</dependencies>

配置参数

在配置文件中,我们需要告诉mybatis所有的xml文件在哪里实体类在哪里,以及是否开启驼峰

spring:
  # datasource 数据源配置内容
  datasource:
    url: jdbc:mysql://localhost:3306/meetsystem?useSSL=false&useUnicode=true&characterEncoding=UTF-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: hejianfei1998

# mybatis 配置内容
mybatis:
#  config-location: classpath:mybatis-config.xml # 配置 MyBatis 配置文件路径
  mapper-locations: classpath:mapper/*.xml # 配置 Mapper XML 地址
  type-aliases-package: com.example.mybatis.entity # 配置数据库实体包路径
  configuration:
    map-underscore-to-camel-case: true  # 开启驼峰

编写Mapper和xml文件

完成以上配置后,我们就可以编写mapper接口类了,但是需要注意的是,接口类需要用@Mapper注解标明或者在启动类上使用@MapperScan(basepackage=" ")注解表明mapper所在包,这样mapper才会被mybatis扫描到。
编写mapper和xml文件,可以看这篇文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值