MyBatis + Spring Boot 实现学生信息查询:详细步骤及代码示例

本文将通过一个简单的示例,演示如何使用 MyBatis 和 Spring Boot 实现学生信息的查询功能。代码涵盖了数据访问层(DAO)、服务层(Service)、控制层(Controller)以及 MyBatis 映射文件等关键部分。

1. 项目结构

└── src
    └── main
        └── java
            └── com
                └── example
                    └── demo1
                        ├── mapper
                        │   └── StudentMapper.java
                        ├── domain
                        │   └── Student.java
                        ├── service
                        │   ├── StudentService.java
                        │   └── StudentServiceImpl.java
                        └── controller
                            └── StudentController.java

2. 实体类 Student.java

package com.example.demo1.domain;

public class Student {
    private Integer id;
    private String name;
    private String no;
    private String gender;
    private Integer classId;

    // getter and setter
}

3. 数据访问层接口 StudentMapper.java

package com.example.demo1.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo1.domain.Student;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface StudentMapper extends BaseMapper<Student> {

}

4. MyBatis 映射文件 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.example.demo1.mapper.StudentMapper'>

    <resultMap id='BaseResultMap' type='com.example.demo1.domain.Student'>
            <id property='id' column='id' jdbcType='INTEGER'/>
            <result property='name' column='name' jdbcType='VARCHAR'/>
            <result property='no' column='no' jdbcType='VARCHAR'/>
            <result property='gender' column='gender' jdbcType='VARCHAR'/>
            <result property='classId' column='class_id' jdbcType='INTEGER'/>
    </resultMap>

    <sql id='Base_Column_List'>
        id,name,no,
        gender,class_id
    </sql>
</mapper>

5. 服务层接口 StudentService.java

package com.example.demo1.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo1.domain.Student;

public interface StudentService extends IService<Student> {

}

6. 服务层实现类 StudentServiceImpl.java

package com.example.demo1.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo1.domain.Student;
import com.example.demo1.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
        implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student getById(Serializable id) {
        return super.getById(id);
    }

}

7. 控制层 StudentController.java

package com.example.demo1.controller;

import com.example.demo1.domain.Student;
import com.example.demo1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/{id}")
    public Student getById(@PathVariable("id") Integer id) {
        return studentService.getById(id);
    }
}

8. 总结

以上代码演示了如何使用 MyBatis 和 Spring Boot 实现简单的学生信息查询功能。

需要注意的是,本例中使用的是 MyBatis-Plus,它简化了 MyBatis 的使用,提供了许多便捷的功能,例如:

  • BaseMapper 接口:提供了基本的 CRUD 操作
  • ServiceImpl 类:提供了基本的服务层实现

在实际开发中,还需要根据具体需求对代码进行调整和完善。

MyBatis + Spring Boot 实现学生信息查询:详细步骤及代码示例

原文地址: https://www.cveoy.top/t/topic/lB8M 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录