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

2021/4/10 10:30:59

本文主要是介绍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();
    }


这篇关于mybatis-plus的多表查询——vo层——@select注解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程