1.准备工作(创建springboot工程、数据库表user、实体类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');
创建实体类:
/** Copyright (c) 2020, 2023, All rights reserved.**/
package com.example.springbootmybatis.pojo;/*** <p>Project: spring-boot-mybatis - User</p>* <p>Powered by scl On 2023-10-07 10:12:33</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
public class User {private Integer id;private String name;private short age;private short gender;private String phone;@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", gender=" + gender +", 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 getGender() {return gender;}public void setGender(short gender) {this.gender = gender;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public User(Integer id, String name, short age, short gender, String phone) {this.id = id;this.name = name;this.age = age;this.gender = gender;this.phone = phone;}public User() {}
}
2.引入Mybatis的相关依赖,配置Mybatis(数据库连接信息)
配置文件:
#驱动类名称
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=
3.编写SQL语句(注解/XML)
编写mapper接口:
package com.example.springbootmybatis.mapper;import com.example.springbootmybatis.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;/*** <p>Project: spring-boot-mybatis - UserMapper</p>* <p>Powered by scl On 2023-10-07 10:17:12</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
@Mapper //在运行时会自动生成实现类对像,并交给ioc容器管理
public interface UserMapper {@Select("select * from user")public List<User> list();
}
编写测试类:
package com.example.springbootmybatis;import com.example.springbootmybatis.mapper.UserMapper;
import com.example.springbootmybatis.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 SpringBootMybatisApplicationTests {@Autowiredprivate UserMapper userMapper;@Testpublic void testListUser(){List<User> userList=userMapper.list();userList.stream().forEach(user -> {System.out.println(user);});}}