把需要的文件夹目录创建好
将需要用到的文件创建好
将数据库的链接改成你自己的链接
接下来是项目代码
StudentController.java
@Controller
public class StudentController {
@Autowired
StudentService studentService;
@RequestMapping("/Student")
@ResponseBody
public List<Student> hello() {
return studentService.queryAllStudent();
}
}
StudentMapper.java
@Mapper
public interface StudentMapper {
List<Student> queryAllStudent();
}
Student.java
public class Student {
private int id;
private String name;
private int age;
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
StudentService.java
public interface StudentService {
List<Student> queryAllStudent();
}
StudentServiceImpl
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
StudentMapper studentMapper;
@Override
public List<Student> queryAllStudent() {
return studentMapper.queryAllStudent();
}
}
resources/mappers/StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lb.demo.dao.StudentMapper">
<select id="queryAllStudent" resultType="com.lb.demo.domain.Student">
select * from student
</select>
</mapper>
sql文件(一个student表)
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`age` int(0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 20 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '张三', 12);
INSERT INTO `student` VALUES (2, 'Zara', 20);
INSERT INTO `student` VALUES (3, 'Nuha', 2);
INSERT INTO `student` VALUES (4, 'Ayan', 15);
INSERT INTO `student` VALUES (5, 'Zara', 11);
INSERT INTO `student` VALUES (6, 'Nuha', 2);
INSERT INTO `student` VALUES (7, 'Ayan', 15);
INSERT INTO `student` VALUES (8, 'Zara', 11);
INSERT INTO `student` VALUES (9, 'Nuha', 2);
INSERT INTO `student` VALUES (10, 'Ayan', 15);
INSERT INTO `student` VALUES (11, 'Zara', 11);
INSERT INTO `student` VALUES (12, 'Nuha', 2);
INSERT INTO `student` VALUES (13, 'Ayan', 15);
INSERT INTO `student` VALUES (14, 'Zara', 11);
INSERT INTO `student` VALUES (15, 'Nuha', 2);
INSERT INTO `student` VALUES (16, 'Ayan', 15);
INSERT INTO `student` VALUES (17, 'Zara', 11);
INSERT INTO `student` VALUES (18, 'Nuha', 20);
SET FOREIGN_KEY_CHECKS = 1;
之后运行项目试试
在浏览器输入添加链接描述
完美运行!!!