spring boot 整合Mybatis

mybatis-spring-boot-starter

  1. idea创建工程-spring InisInitialzr-web:web-sql:postgresql(或者mysql)
    注:在sql项中,只选择数据库类型,如postgresql,其他的都不选(包括jpa、jdbc、MyBatis),因为MyBatis不是spring生态体系中的一份子,starter启动器是第三方提供的,所以要单独引用
  2. 引入MyBatis start,在Maven网站查询引用,地址:https://mvnrepository.com/artifact/org.mybatis/mybatis

在这里插入图片描述

  1. 在pom.xml中添加dependency
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency> 
  1. maven中重新引用reimport(不做这步也行,idea会自动下载引入,虽然前面并没有勾选jdbc,MyBatis会自动关联引用)

  2. 在外部库中可看到idea下载的MyBatis jar包
    在这里插入图片描述

  3. 可以在依赖图中检查一下是否已经引入成功
    在这里插入图片描述

配置application.yml

  1. 配置数据源信息
    示例:

     spring:
       http:
         encoding:
           charset: utf-8
       thymeleaf:
         cache: true
       mvc:
         date-format: yyyy-mm-dd
       datasource:
         type: com.alibaba.druid.pool.DruidDataSource
         driver-class-name: org.postgresql.Driver
         url: jdbc:postgresql://localhost:5432/postgres
         username: postgres
         password: postgres
     
       #format json
       jackson:
         serialization:
           indent-output: true
     mybatis:
     #mybatis的核心配置文件
       config-location: classpath:/mybatis/mybatis-config.xml
       mapper-locations: classpath:/mybatis/mapper/*.xml
    

    #show sql
    logging:
    level:
    com:
    mapper: debug

注:在编写yml文件时,一定要注意格式“属性值:空格+值”!

创建Mybatis-config.xml

  1. 根据mybatis.config-location路径,创建Mybatis-config.xml,
    内容如下:

    <?xml version="1.0" encoding="UTF-8"?>

在这里插入图片描述

创建mybatis.mapper-locations目录

根据mybatis.mapper-locations属性中的目录,创建mapper文件夹
在这里插入图片描述

开发Mybatis应用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值