UserMapper.java
@Repository
public interface UserMapper extends BaseMapper<User> {
//多对一
@Results(value = {
@Result(property = "id", column = "id"),
@Result(property = "name", column = "name"),
@Result(property = "age", column = "age"),
@Result(property = "email", column = "email"),
@Result(property = "dept", column = "deptid", one = @One(select = "com.atguigu.mybatisplus.mapper.DeptMapper.selectById")),
})
@Select("select id, name, age, email, deptid from t_user")
List<UserVO> getUserDept();
@Select("select id, name, age, email from t_user where deptid=#{deptid}")
List<User> getUserByDeptId(@Param("deptid") int deptid);
}
DeptMapper.java
public interface DeptMapper extends BaseMapper<Dept> {
//一对多
@Results(value = {
@Result(property = "id", column = "id"),
@Result(property = "name", column = "name"),
@Result(property = "users", column = "id", many = @Many(select = "com.atguigu.mybatisplus.mapper.UserMapper.getUserByDeptId")),
})
@Select("select id, name from t_dept")
List<DeptVO> getDeptUser();
}
DeptVO
@Data
public class DeptVO {
private Integer id;
private String name;
private List<User> users;
}
UserVO
@Data
public class UserVO {
private int id;
private String name;
private int age;
private String email;
private Dept dept;
}
调用
@SpringBootTest
class MybatisplusApplicationTests {
@Resource
private IUserService userService;
@Resource
private IDeptService deptService;
@Test
void contextLoads() {
List<UserVO> userDept = userService.getUserDept();
userDept.forEach(System.out::println);
List<DeptVO> deptUser = deptService.getDeptUser();
deptUser.forEach(System.out::println);
}
}
标签:name,column,private,Plus,Result,MyBatis,注解,property,id
From: https://blog.51cto.com/u_14452299/6010730