
-
- 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;
-
- /**
- *
Project: spring-boot-mybatis - User
- *
Powered by scl On 2023-10-07 10:12:33
- *
描述:
- *
- * @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;
-
- @Override
- public 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() {
- }
- }

配置文件:
- #驱动类名称
- 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=
-
-
-
- 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;
-
- /**
- *
Project: spring-boot-mybatis - UserMapper
- *
Powered by scl On 2023-10-07 10:17:12
- *
描述:
- *
- * @author 孙臣龙 [1846080280@qq.com]
- * @version 1.0
- * @since 17
- */
- @Mapper //在运行时会自动生成实现类对像,并交给ioc容器管理
- public interface UserMapper {
- @Select("select * from user")
- public List
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 {
-
- @Autowired
- private UserMapper userMapper;
- @Test
- public void testListUser(){
- List
userList=userMapper.list(); - userList.stream().forEach(user -> {
- System.out.println(user);
- });
- }
-
- }