Spring Boot + MyBatis Plus 查询学生信息示例
Spring Boot + MyBatis Plus 查询学生信息示例
本示例演示了如何使用 Spring Boot、MyBatis Plus 和注解方式实现查询学生信息的接口。
项目结构
└── src
└── main
└── java
└── com
└── example
└── demo1
└── service
└── StudentServiceImpl.java
代码示例
1. 数据模型 (Student.java)
package com.example.demo1.domain;
import lombok.Data;
import java.io.Serializable;
@Data
public class Student implements Serializable {
private Long id;
private String name;
private Integer age;
}
2. 数据访问层 (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> {
}
3. 服务层 (StudentService.java)
package com.example.demo1.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo1.domain.Student;
import java.io.Serializable;
public interface StudentService extends IService<Student> {
}
4. 服务层实现 (StudentServiceImpl.java)
package com.example.demo1.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo1.domain.Student;
import com.example.demo1.mapper.StudentMapper;
import com.example.demo1.service.StudentService;
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 studentMapper.selectById(id);
}
}
5. 控制层 (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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/student/{id}")
@ResponseBody
public Student getStudentById(@PathVariable("id") Long id) {
return studentService.getById(id);
}
}
错误解决
问题:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.demo1.mapper.StudentMapper.selectById
原因
这个错误通常是由于 MyBatis Plus 没有找到对应的数据访问层方法 selectById 而导致的。可能的原因包括:
StudentMapper.java中没有定义selectById方法。StudentMapper.java中定义了selectById方法,但是方法名或参数与 MyBatis Plus 默认的selectById方法不一致。StudentMapper.java中定义了selectById方法,但是@Mapper注解没有生效,导致 MyBatis Plus 无法识别。
解决方法
-
确保
StudentMapper.java中定义了selectById方法,方法名和参数与 MyBatis Plus 默认的selectById方法保持一致,如下所示: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> { // 该方法由 MyBatis Plus 默认提供,无需额外定义 // Student selectById(Serializable id); } -
如果
@Mapper注解没有生效,请确认以下几点:-
在
application.properties或application.yml文件中配置了 MyBatis Plus 的相关配置,例如:mybatis-plus: mapper-locations: classpath:mapper/*.xml global-config: db-config: id-type: auto -
在启动类中添加了
@MapperScan注解,并指定扫描路径:@SpringBootApplication @MapperScan(basePackages = "com.example.demo1.mapper") public class Demo1Application { public static void main(String[] args) { SpringApplication.run(Demo1Application.class, args); } }
-
-
如果以上步骤都无法解决问题,可以尝试重新构建项目或者清除缓存等操作。
代码运行
- 运行项目。
- 在浏览器中访问地址
http://localhost:8080/student/12,即可查询到 id 为 12 的学生信息。
总结
本示例演示了如何使用 Spring Boot、MyBatis Plus 和注解方式实现查询学生信息的接口。通过代码示例和错误解决方法,帮助读者更好地理解和应用相关技术。
原文地址: https://www.cveoy.top/t/topic/lB8o 著作权归作者所有。请勿转载和采集!