1. maven依赖
<!-- mybatis --> <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.1</version></dependency><!-- mysql驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.25</version></dependency>
2. 编写Mapper接口和Mapper.xml
package com.example.mybatis.mapper;
import com.example.mybatis.pojo.Demo;
import java.util.List;
public interface DemoMapper {List<Demo> selectList();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.mybatis.mapper.DemoMapper" ><select id="selectList" resultType="com.example.mybatis.pojo.Demo">select * from tb_demo</select>
</mapper>
3. 启动类添加注解
//配置Mapper接口包
@MapperScan("com.example.mybatis.mapper")
4. 配置文件配置
# 配置数据库url
spring.datasource.url=jdbc:mysql://localhost:3306/learn?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
# 配置数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 配置数据账号
spring.datasource.username=root
# 配置数据库密码
spring.datasource.password=zproot_zjroot449057
# 配置实体类包
mybatis.typeAliasesPackage=com.example.mybatis.pojo
# 配置mapper.xml位置
mybatis.mapperLocations= classpath:mapper/*.xml