创建项目点击Spring Initializr然后点击next

点击SQL 选择里面的Mybatis Framework和Mysql Driver

按如下图片创建项目

user表中的数据

#下面这些内容是为了让MyBatis映射 #指定Mybatis的Mapper文件 mybatis.mapper-locations=classpath:mappers/*xml #指定Mybatis的实体目录 mybatis.type-aliases-package=com.ming.mybatis.entity #驱动名称 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #数据库的url spring.datasource.url=jdbc:mysql://localhost:3306/db02 #数据库的用户名 spring.datasource.username=root #数据库的密码 spring.datasource.password=123456
创建user类
package com.ming.pojo;
public class User {private Integer id;private String name;private short age;private short gender;private String phone;
public User() {}
public User(Integer id, String name, short age, short genden, String phone) {this.id = id;this.name = name;this.age = age;this.gender = genden;this.phone = phone;}
public Integer getId() {return id;}
public void setId(Integer id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public short getAge() {return age;}
public void setAge(short age) {this.age = age;}
public short getGenden() {return gender;}
public void setGenden(short genden) {this.gender = genden;}
public String getPhone() {return phone;}
public void setPhone(String phone) {this.phone = phone;}
@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", genden=" + gender +", phone='" + phone + '\'' +'}';}
}
创建UsreMapper接口
package com.ming.mapper;
import com.ming.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper//运行时,会自动生成该类接口的实现类对象,并且将该接口交给IOC容器
public interface UsreMapper {@Select("select * from user")public List<User> list();
}
在测试类写如下代码
package com.ming;
import com.ming.mapper.UsreMapper;
import com.ming.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class Springbootmybatis01ApplicationTests {
@Autowiredprivate UsreMapper usreMapper;
@Testpublic void testListUser(){List<User> list = usreMapper.list();for (User user : list) {System.out.println(user);}}
}

数据被打印到了控制台上