[MyBatis和MyBatis-Plus] 嵌套查询(一对一、一对多)

表结构:

CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_ame` varchar(50) DEFAULT NULL, `user_age` int(11) DEFAULT NULL, `user_address` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) );

CREATE TABLE `article` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `title` varchar(100) DEFAULT NULL, `content` text, `blog_id` int(11) NOT NULL, PRIMARY KEY (`id`) );

CREATE TABLE `blog` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) );

mybatis-cfg.xml:

 

Article实体类:

public class Article { private int id; private User user; //Article类中有一个User类。 private String title; private String content;

…… //getter、setter

}

Article.xml:

Blog实体类:

public class Blog { private int id; private String title; private List

articles; //Blog类中有一个List

…… //getter、setter

}

Blog.xml:

 

注意:如果是一对多的分页,不能在最后limit,需要在主表先分页。

主表分页的时候,有可能从表没有数据,分页数量可能还是不对,可添加条件来处理。

一对多分页处理示例: