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

定义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();
    }

jdbc

mybatis-plus的多表查询——vo层——@select注解——jdbc_第1张图片
mybatis-plus的多表查询——vo层——@select注解——jdbc_第2张图片

你可能感兴趣的:(mybatis-plus,json,mybatis,spring,boot,mysql)