针对springboot自定义sql语句(1)
一、mapper层
在mapper层定义一个方法,加上注解@select,@insert, @update, @delete
如下图所示:
@Select("SELECT `class_id` FROM `stuclass` WHERE `class_name` = #{className}")
String getClassId(@Param("className") String className);
数据若返回单单一个string类型,注意得到的数据要是一条,建议在sql语句后面加上一条limit 1,传的参数用@param注解(注意是annotation包下的)
二、service层
同样定义一个方法用来使用mapper层的方法
public interface StudentService extends IService<Student> {
// void pageQuery(Page<Student> studentPage, StudentQuery studentQuery);
String getClassId(String className);
// String getCardId(String classId);
}
三、实现接口的 impl
@Override
public String getClassId(String className) {
return this.baseMapper.getClassId(className);
}
使用this.baseMapper相当于StudentMapper中的方法。
四、controller层
@Autowired
private StudentService studentService;
public String addStu(@RequestBody Student student) {
return studentService.getClassId(student.getStuClass());
}
得到数据库中学生班级名对应的id