mybatis-spring-boot-starter
- idea创建工程-spring InisInitialzr-web:web-sql:postgresql(或者mysql)
注:在sql项中,只选择数据库类型,如postgresql,其他的都不选(包括jpa、jdbc、MyBatis),因为MyBatis不是spring生态体系中的一份子,starter启动器是第三方提供的,所以要单独引用 - 引入MyBatis start,在Maven网站查询引用,地址:https://mvnrepository.com/artifact/org.mybatis/mybatis
- 在pom.xml中添加dependency
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
-
maven中重新引用reimport(不做这步也行,idea会自动下载引入,虽然前面并没有勾选jdbc,MyBatis会自动关联引用)
-
在外部库中可看到idea下载的MyBatis jar包
-
可以在依赖图中检查一下是否已经引入成功
配置application.yml
-
配置数据源信息
示例: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
-
根据mybatis.config-location路径,创建Mybatis-config.xml,
<?xml version="1.0" encoding="UTF-8"?>
内容如下:
创建mybatis.mapper-locations目录
根据mybatis.mapper-locations属性中的目录,创建mapper文件夹