-
使用Spring Initializr创建一个新的Spring Boot项目
-
选择以下依赖:
Spring Web(用于构建Web应用)
MyBatis Framework(用于整合MyBatis)
MySQL Driver(用于连接MySQL数据库)
-
配置数据库链接
-
创建实体类:创建一个实体类,对应数据库中的表
//User类
package com.wfs.springbootcode.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 gender, String phone) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
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 getGender() {
return gender;
}
public void setGender(Short gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
", phone='" + phone + '\'' +
'}';
}
}
- 创建Mapper接口
package com.wfs.springbootcode.mapper;
import com.wfs.springbootcode.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user")
List<User> findAll();
}
- 创建Service层:创建Service接口和实现类,用于处理业务逻辑。
UserService.java
package com.wfs.springbootcode.service;
import com.wfs.springbootcode.pojo.User;
import org.springframework.stereotype.Service;
import java.util.List;
public interface UserService {
List<User> getAllUsers();
}
UserServiceImpl.java
package com.wfs.springbootcode.service.impl;
import com.wfs.springbootcode.mapper.UserMapper;
import com.wfs.springbootcode.pojo.User;
import com.wfs.springbootcode.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
/**
* 获取所有用户信息。
* @return 返回所有用户的信息列表
*/
@Override
public List<User> getAllUsers() {
// 从userMapper中获取所有用户信息
List<User> users = userMapper.findAll();
// 返回用户信息列表
return users;
}
}
- Controller层
package com.wfs.springbootcode.controller;
import com.wfs.springbootcode.pojo.User;
import com.wfs.springbootcode.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserServiceImpl userService;
/**
* 处理用户列表请求的控制器方法。
* 该方法从用户服务中获取所有用户,并将每个用户的信息打印到控制台,最后返回用户列表。
*
* @return 返回所有用户的列表
*/
@RequestMapping("/user/list")
public List<User> getUserList() {
// 从用户服务中获取所有用户
List<User> users = userService.getAllUsers();
// 遍历用户列表,将每个用户的信息打印到控制台
for (User user : users) {
System.out.println(user);
}
// 返回用户列表
return users;
}
}
- 创建数据库表并插入数据
create database if not exists mybatis;
use mybatis;
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');
- 项目目录以及最终效果图