分享

myBatis系列之四:关联数据的查询

 aaie_ 2016-05-21
myBatis系列之一:搭建开发环境
myBatis系列之二:以接口方式交互数据
myBatis系列之三:增删改查
myBatis系列之五:与Spring3集成
myBatis系列之六:与SpringMVC集成
myBatis系列之七:事务管理


myBatis系列之三:增删改查是基于单表的查询,如果联表查询,返回的是复合对象,需要用association关键字来处理。
如User发表Article,每个用户可以发表多个Article,他们之间是一对多的关系。

1. 创建Article表,并插入测试数据:
Sql代码  收藏代码
  1. -- Drop the table if exists  
  2. DROP TABLE IF EXISTS `Article`;  
  3.   
  4. -- Create a table named 'Article'  
  5. CREATE TABLE `Article` (  
  6.     `id` int NOT NULL AUTO_INCREMENT,  
  7.     `user_id` int NOT NULL,  
  8.     `title` varchar(100) NOT NULL,  
  9.     `content` text NOT NULL,  
  10.     PRIMARY KEY (`id`)  
  11. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;  
  12.   
  13. -- Add several test records  
  14. INSERT INTO `article`  
  15. VALUES  
  16. ('1''1''title1''content1'),  
  17. ('2''1''title2''content2'),  
  18. ('3''1''title3''content3'),  
  19. ('4''1''title4''content4');  


2. com.john.hbatis.model.Article类:
Java代码  收藏代码
  1. public class Article {  
  2.     private int id;  
  3.     private User user;  
  4.     private String title;  
  5.     private String content;  
  6.     // Getters and setters are omitted  
  7. }  


3. 在IUserMapper中添加:
Java代码  收藏代码
  1. List<Article> getArticlesByUserId(int id);  


4. 在User.xml中添加:
Xml代码  收藏代码
  1. <resultMap type="com.john.hbatis.model.Article" id="articleList">  
  2.     <id column="a_id" property="id" />  
  3.     <result column="title" property="title" />  
  4.     <result column="content" property="content" />  
  5.       
  6.     <association property="user" javaType="User"><!-- user属性映射到User类 -->  
  7.         <id column="id" property="id" />  
  8.         <result column="name" property="name" />  
  9.         <result column="address" property="address" />  
  10.     </association>  
  11. </resultMap>  
  12.   
  13. <select id="getArticlesByUserId" parameterType="int" resultMap="articleList">  
  14.     select u.id, u.name, u.age, u.address, a.id a_id, a.title, a.content  
  15.     from article a  
  16.     inner join user u  
  17.     on a.user_id=u.id and u.id=#{id}  
  18. </select>  


5. 测试方法:
Java代码  收藏代码
  1. @Test  
  2. public void getArticlesByUserIdTest() {  
  3.     SqlSession session = sqlSessionFactory.openSession();  
  4.     try {  
  5.         IUserMapper mapper = session.getMapper(IUserMapper.class);  
  6.         List<Article> articles = mapper.getArticlesByUserId(1);  
  7.         for (Article article : articles) {  
  8.             log.info("{} - {}, author: {}", article.getTitle(), article.getContent(), article.getUser().getName());  
  9.         }  
  10.     } finally {  
  11.         session.close();  
  12.     }  
  13. }  


附:
除了在association标签内定义字段和属性的映射外,还可以重用User的resultMap:
Xml代码  收藏代码
  1. <association property="user" javaType="User" resultMap="userList" />  


参考:
http://www./article/java/306.htm

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约