1、准备工作
创建springboot工程、数据库表user、实体类User
创建springboot工程如下:
引入MyBatis相关依赖,勾选MyBatis相关选项
准备数据库表user
create table user(
id int unsigned primary key auto_increment comment 'ID',
name varchar(100) comment '姓名',
age tinyint unsigned comment '年龄',
gender tinyint unsigned comment '性别, 1:男, 2:女',
phone varchar(11) comment '手机号'
) comment '用户表';
insert into user(id, name, age, gender, phone) VALUES (null,'白眉鹰王',55,'1','18800000000');
insert into user(id, name, age, gender, phone) VALUES (null,'金毛狮王',45,'1','18800000001');
insert into user(id, name, age, gender, phone) VALUES (null,'青翼蝠王',38,'1','18800000002');
insert into user(id, name, age, gender, phone) VALUES (null,'紫衫龙王',42,'2','18800000003');
insert into user(id, name, age, gender, phone) VALUES (null,'光明左使',37,'1','18800000004');
insert into user(id, name, age, gender, phone) VALUES (null,'光明右使',48,'1','18800000005');
创建实体类User:
package com.itheima.pojo;
public class User {
private Integer id;
private String name;
private short age;
private Short gender;
private String address;
//有参构造
public User(Integer id, String name, short age, Short gender, String address) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.address = address;
}
//无参构造
public User() {
}
//get和set方法
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 getGender() {
return gender;
}
public void setGender(Short gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + ''' +
", age=" + age +
", gender=" + gender +
", address='" + address + ''' +
'}';
}
}
2、配置MyBatis
在application.properties文件里面
spring.application.name=springboot-mybatis-quickstart
#配置数据库的连接信息 --四要素
#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=123456
3、编写SQL语句(注解/XML)
定义一个持久层的接口UserMapper 创建一个mapper的文件夹,把该文件放到下面去
package com.itheima.springboot.mapper;
import com.itheima.springboot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper//运行时,该接口会自动生成实现类对象(代理对象),并且将该对象交给IOC容器管理
public interface UserMapper {
//查询全部的用户信息
@Select("select * from user")
public List<User> list();
}
测试:在test目录下 在该测试文件下可以调用UserMapper来执行相关的SQL语句:查询全部的用户信息
@SpringBootTest//springboot整合单元测试的注解
class SpringbootMybatisDemo1ApplicationTests {
@Autowired//完成依赖注入
private UserMapper userMapper;
@Test
public void testListUser(){
List<User> userList = userMapper.list();//直接调用userMapper来查询所有的用户数据
userList.stream().forEach(user -> {
System.out.println(user);
});
}
}
运行该测试,结果如下