mybatis-plus 连表查询
一、mybatis
xml格式可以参考: https://www.cnblogs.com/wt7018/p/13333855.html
xml方法 失败,求大神指导
在springboot中使用mybaits注解的方式查询
1、根据查询的字段创建实现类
package com.wt.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User2 { private String name; private int age; private String courseName; }
2、在mapper文件夹下,创建方法
package com.wt.mapper; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.wt.pojo.User; import com.wt.pojo.User2; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface UserMapper extends BaseMapper<User> { @Select("SELECT user.name, user.age, course.course_name FROM user right join course on user.id = course.user_id WHERE id = #{id}") List<User2> getUserList(@Param("id") int id); }
3、写get接口
@GetMapping("/selectBy") public String selectMu(@RequestParam("id") int id){ List<User2> userList = userMapper.getUserList(id); return JSON.toJSONString(userList); }