mybatis-plus的多表查询——vo层——@select注解

定义vo层

@Data
public class stvo {
    private Integer sid;
    private String sname;
    private String tname;
    private Integer age;
    private LocalDate gradetime;
}

在service定义方法

public interface StudentService extends IService<Student> {
public List<stvo> find();
}

实现定义的方法

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

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Override
    public List<stvo> find() {
        List<Student> students = studentMapper.selectList(null);
        List<stvo> stvoList = new ArrayList<>();
        for (Student student : students) {
            stvo stvo = new stvo();
            BeanUtils.copyProperties(student,stvo);
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("tid",student.getTeacher());
            stvo.setTname(teacherMapper.selectOne(wrapper).getTname());
            stvoList.add(stvo);
        }
        return stvoList;
    }
}

controller层返回json数据

@RequestMapping("/list")
    @ResponseBody
    public List<stvo> list(){
        return studentService.find();
    }

@select注解
mapper方法

public interface StudentMapper extends BaseMapper<Student> {

    @Select("SELECT s.sid,s.sname,t.tname,s.age,s.gradetime FROM student s,teacher t WHERE s.teacher=t.tid")
    public List<stvo> duo();
}

controller层返回json数据

@RequestMapping("/duo")
    @ResponseBody
    public List<stvo> duo(){
        return studentMapper.duo();
    }
上一篇:VO和DTO有什么区别?


下一篇:go nacos服务发现