MybatisPlus实现对象嵌套关联查询一对多List集合查询
目录
对象嵌套关联查询一对多List集合查询mybatis嵌套关联查询如下一对多查询(经典案例)条件数据库代码实现对象嵌套关联查询一对多List集合查询
mybatis嵌套关联查询如下
由于我的是一对集合查询,所以我有两个类。
@Data
@TableName("tb_user")
public class User {
@TableId(type= IdType.INPUT)
private String id;
@TableField("user_name")
private String username;
private String password;
private String name;
private String email;
private int age;
private ArrayList list;
}
权限类
@Data
@TableName
public class Authority {
@TableId(type = IdType.INPUT)
@TableField("aid")
private int id;
@TableId("aname")
private String name;
}测试类
@Test
public void ManyToMany(){
User user = userMapper.selectAuthorityById(1);
ArrayList list = user.getList();
System.out.println(user);
for (Authority authority : list) {
System.out.println("所对应权限为"+authority.getName());
}
} springboot项目的依赖
org.springframework.boot spring-boot-starter mysql mysql-connector-java 5.1.26 org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test com.baomidou mybatis-plus-boot-starter 3.4.0
这下面就是我xml文件里面怎么写的嵌套查询语句
数据库的配置我就不放了,直接编写就可以了,看会下面这个xml配置就可以了
一对多查询(经典案例)
条件
查询班级表 返回所有学生信息 (一对多问题)
数据库
班级class_info
学生student
代码实现
实体类ClassInfo.java
@Data
public class ClassInfo {
private Long id;
private String name;
private String nameTest;
private List studentList;
} ClassInfoMapper.xml
关联StudentMapper.xml中的子查询
ClassInfoMapper.java
public interface ClassInfoMapper extends BaseMapper{ IPage listAllWithStudent(IPage page); }
ClassInfoService.java
public interface ClassInfoService extends IService{ IPage listAllWithStudent(IPage page); }
ClassInfoServiceImpl.java
@Service public class ClassInfoServiceImpl extends ServiceImplimplements ClassInfoService { @Autowired private StudentService studentService; @Override public IPage listAllWithStudent(IPage page) { return this.baseMapper.listAllWithStudent(page); } }
ClassInfoController.java
@Controller
@RequestMapping("classInfo")
public class ClassInfoController {
@Autowired
private ClassInfoService classInfoService;
@RequestMapping("listAllWithStudent")
@ResponseBody
public IPage listAllWithStudent(Integer pageNo,Integer pageSize){
Page page = new Page<>(pageNo,pageSize);
return classInfoService.listAllWithStudent(page);
}
} 以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
X 关闭
X 关闭

