1、Mybatis 关联查询(嵌套查询)表的存储 sql 文件:表的存储 sql 文件:select user.id user_id,user.userName user_userName,user.userAddress user_userAddress,article.id article_id,article.title article_title,article.content article_content from user,article where user.id=article.userid and user.id=#idselect * from user where id =
2、 #idBlog.xml 配置:select user.id user_id,user.userName user_userName,user.userAddress user_userAddress,article.id article_id,article.title article_title,article.content article_content, blog.id blog_id, blog.title blog_titlefrom user,article,blog where user.id=article.userid and blog.id=article.blogid
3、 and blog.id=#idIArticleOperation 定义:package com.mybatis.test;import java.util.List;public interface IArticleOperation public List getUserArticles(int userID);IBlogOperation 定义:package com.mybatis.test;public interface IBlogOperation Blog getBlogByID(int id);Test 类:package com.mybatis.test;import ja
4、va.io.Reader;import java.util.List;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class Test private static SqlSessionFactory sqlSessionFactory;priva
5、te static Reader reader;static try /通过配置文件初始化 sqlSessionFactoryreader = Resources.getResourceAsReader(“Configuration.xml“);sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); catch (Exception e) e.printStackTrace();public static SqlSessionFactory getSession() return sqlSessionFactory;p
6、ublic void getUserByID(int userID) SqlSession session = sqlSessionFactory.openSession();try IUserOperation userOperation = session.getMapper(IUserOperation.class);User user = userOperation.selectUserByID(userID);if (user != null) System.out.println(user.getId() + “:“ + user.getUserName()+ “:“ + user
7、.getUserAddress(); finally session.close();public void getUserList(String userName) SqlSession session = sqlSessionFactory.openSession();try IUserOperation userOperation = session.getMapper(IUserOperation.class);List users = userOperation.selectUsersByName(userName);for (User user : users) System.ou
8、t.println(user.getId() + “:“ + user.getUserName()+ “:“ + user.getUserAddress(); finally session.close();/* 增加后要 commit*/public void addUser() User user = new User();user.setUserAddress(“place“);user.setUserName(“test_add“);user.setUserAge(30);SqlSession session = sqlSessionFactory.openSession();try
9、IUserOperation userOperation = session.getMapper(IUserOperation.class);userOperation.addUser(user);mit();System.out.println(“新增用户 ID:“ + user.getId(); finally session.close();/* 修改后要 commit*/public void updateUser() SqlSession session = sqlSessionFactory.openSession();try IUserOperation userOperatio
10、n = session.getMapper(IUserOperation.class);User user = userOperation.selectUserByID(1);if (user != null) user.setUserAddress(“A new place“);userOperation.updateUser(user);mit(); finally session.close();/* 删除后要 commit.* * param id*/public void deleteUser(int id) SqlSession session = sqlSessionFactor
11、y.openSession();try IUserOperation userOperation = session.getMapper(IUserOperation.class);userOperation.deleteUser(id);mit(); finally session.close();public void getUserArticles(int userid) SqlSession session = sqlSessionFactory.openSession();try IArticleOperation articleOperation = session.getMapp
12、er(IArticleOperation.class);List articles = articleOperation.getUserArticles(userid);for (Article article : articles) System.out.println(article.getTitle() + “:“+ article.getContent() + “用户名:“+ article.getUser().getUserName() + “用户地址:“+ article.getUser().getUserAddress(); finally session.close();pub
13、lic void getBlogArticles(int blogid) SqlSession session = sqlSessionFactory.openSession();try IBlogOperation blogOperation = session.getMapper(IBlogOperation.class);Blog blog = blogOperation.getBlogByID(blogid);System.out.println(blog.getTitle() + “:“);List articles = blog.getArticles();for (Article
14、 article : articles) System.out.println(article.getTitle() + “:“+ article.getContent() + “用户名:“+ article.getUser().getUserName() + “用户地址:“+ article.getUser().getUserAddress();/*System.out.println(article.getTitle() + “:“+ article.getContent();*/ finally session.close();public static void main(String
15、 args) try Test test = new Test();/ test.getUserByID(1);/ test.getUserList(“test1“);/ test.addUser();/ test.updateUser();/ test.deleteUser(6);/test.getUserArticles(1);test.getBlogArticles(1); catch (Exception e) System.out.println(e.getMessage();Mybatis 的“N+1 查询问题”SELECT * FROM BLOG WHERE ID = #idSE
16、LECT * FROM AUTHOR WHERE ID = #id有两个查询语句:一个来加载博客,另外一个来加载作者,而且博客的结果映射描述了“selectAuthor”语句应该被用来加载它的 author 属性。其他所有的属性将会被自动加载,假设它们的列和属性名相匹配。这种方式很简单,但是对于大型数据集合和列表将不会表现很好。问题就是我们熟知的“N+1 查询问题”。概括地讲,N+1 查询问题可以是这样引起的:你执行了一个单独的 SQL 语句来获取结果列表(就是“+1”)。对返回的每条记录,你执行了一个查询语句来为每个加载细节(就是“N”)。这个问题会导致成百上千的 SQL 语句被执行。这通常不是期望的。可以采用关联的嵌套结果来解决这个问题:resultMap 这是结果映射的 ID,可以映射关联的嵌套结果到一个合适的对象图中。这是一种替代方法来调用另外一个查询语句。