MybatisPlus实现对象嵌套关联查询一对多List集合查询(mybatis嵌套结果查询)

网友投稿 1032 2022-07-29


目录对象嵌套关联查询一对多List集合查询mybatis嵌套关联查询如下一对多查询(经典案例)条件数据库代码实现

对象嵌套关联查询一对多List集合查询

mybatis嵌套关联查询如下

由于我的是一对集合查询,所以我有两个类。

@Data

@TableName("tb_user")

public class User {

@TableId(type= IdType.INPUT)

private String id;

@Tabhttp://leField("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文件里面怎么写的嵌套查询语句

ofType="com.itheima.mybatisplus.domain.Authority">

ofType="com.itheima.mybatisplus.domain.Authority">

SELECT * FROM

authority a,tb_user t,user_authority ua

WHERE a.aid=ua.authority_id

AND t.id=ua.user_id

AND t.id=#{id}

数据库的配置我就不放了,直接编写就可以了,看会下面这个xml配置就可以了

一对多查询(经典案例)

条件

查询班级表 返回所有学生信息  (一对多问题)

数据库

班级class_info

学生student

代码实现

<!- -

实体类ClassInfo.java

@Data

public class ClassInfo {

private Long id;

private String name;

private String nameTest;

private List studentList;

}

ClassInfoMapper.xml

property="studentList"

select="com.example.demo.mapper.StudentMapper.listByClassInfoId">

property="studentList"

select="com.example.demo.mapper.StudentMapper.listByClassInfoId">

select * from class_info

关联StudentMapper.xml中的子查询

SELECT

*

FROM

student s

where class_info_id = #{id1} or name = #{name}

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 ServiceImpl implements 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);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Lombok和MapStruct整合详情
下一篇:springboot整合freemarker的踩坑及解决
相关文章

 发表评论

暂时没有评论,来抢沙发吧~