SpringBoot整合mybatis案例

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
把需要的文件夹目录创建好
在这里插入图片描述
将需要用到的文件创建好
在这里插入图片描述

将数据库的链接改成你自己的链接
在这里插入图片描述

接下来是项目代码

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;

之后运行项目试试
在这里插入图片描述
在浏览器输入添加链接描述在这里插入图片描述
完美运行!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值