mybatis中查询有复杂关联对象的结果集

MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接
表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。

1.resultType

 

在MyBatis进行查询映射的时候,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,
值则是其对应的值。当提供的返回类型属性是resultType的时候,MyBatis会将Map里面的键值对取出赋给
resultType所指定的对象对应的属性。所以其实MyBatis的每一个查询映射的返回类型都是ResultMap,
只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType
所指定对象的属性,而当我们提供的返回类型是resultMap的时候,因为Map不能很好表示领域模型,
我们就需要自己再进一步的把它转化为对应的对象,这常常在复杂查询中很有作用。

 

这里要强调的是,Mybatis是对返回的结果的每一行做映射的。所以,下面的语句返回的是Integer,而不是List

 

 

Xml代码   收藏代码
  1. <select id="count" parameterType="AreaDto" resultType="java.lang.Integer">  
  2.         SELECT id FROM USER  
  3. </select>  

 

返回一个int

 

Xml代码   收藏代码
  1. <select id="count" parameterType="AreaDto" resultType="java.lang.Integer">  
  2.         SELECT count(*) FROM USER  
  3. </select>  

 

返回map

Xml代码   收藏代码
  1. <select id=”selectUsers” parameterType=”int” resultType=”hashmap”>  
  2.     select id, username, hashedPassword  
  3.     from some_table  
  4.     where id = #{id}  
  5. </select>  

 

这样一个语句简单作用于所有列被自动映射到HashMap的键上,这由resultType属性指定。这在很多情况下是有用的,但是HashMap不能很好描述一个领域模型。那样你的应用程序将会使用JavaBeans或POJOs(Plain Old Java Objects,普通Java对象)来作为领域模型

返回javaBEAN 对象

 

Xml代码   收藏代码
  1. <select id="count" parameterType="AreaDto" resultType="User">  
  2.         SELECT * FROM USER  
  3. </select>   

 

要记住类型别名是你的伙伴。使用它们你可以不用输入类的全路径。

 

Xml代码   收藏代码
  1. <typeAlias type=”com.someapp.model.User” alias=”User”/>  

 这些情况下,MyBatis会在幕后自动创建一个ResultMap,基于属性名来映射列到JavaBean的属性上

 

2.resultMap

 

MyBatis会自动创建一个ResultMap对象,然后基于查找出来的属性名进行键值对封装,然后再看到返回类型是Blog对象,再从ResultMap中取出与Blog对象对应的键值对进行赋值。

当返回类型直接是一个ResultMap的时候也是非常有用的,这主要用在进行复杂联合查询上,因为进行简单查询是没有什么必要的。

 

简单resultMap配置 

 

Xml代码   收藏代码
  1. <resultMap type="com.liulanghan.Blog" id="BlogResult">    
  2.     <id column="id" property="id"/>    
  3.     <result column="title" property="title"/>    
  4.     <result column="content" property="content"/>    
  5.     <result column="owner" property="owner"/>    
  6. </resultMap>   
  7.    
  8. <select id="selectBlog" parameterType="int" resultMap="BlogResult">    
  9.       select * from t_blog where id = #{id}    
  10. </select>  

 

结果集的列比resultMap多会报错么?
不会,只映射resultMap中有的列。

 

结果集的列比resultMap少会报错么?
不会,只映射结果集中有的列。

 

高级结果映射

 

Xml代码   收藏代码
  1. <resultMap id="detailedBlogResultMap" type="Blog">  
  2.     <constructor>  
  3.         <idArg column="blog_id" javaType="int"/>  
  4.     </constructor>  
  5.     <result property="title" column="blog_title"/>  
  6.     <association property="author" column="blog_author_id" javaType=" Author">  
  7.         <id property="id" column="author_id"/>  
  8.         <result property="username" column="author_username"/>  
  9.         <result property="password" column="author_password"/>  
  10.         <result property="email" column="author_email"/>  
  11.         <result property="bio" column="author_bio"/>  
  12.         <result property="favouriteSection" column="author_favourite_section"/>  
  13.     </association>  
  14.     <collection property="posts" ofType="Post">  
  15.         <id property="id" column="post_id"/>  
  16.         <result property="subject" column="post_subject"/>  
  17.         <association property="author" column="post_author_id" javaType="Author"/>  
  18.         <collection property="comments" column="post_id" ofType=" Comment">  
  19.             <id property="id" column="comment_id"/>  
  20.         </collection>  
  21.         <collection property="tags" column="post_id" ofType=" Tag" >  
  22.             <id property="id" column="tag_id"/>  
  23.         </collection>  
  24.         <discriminator javaType="int" column="draft">  
  25.             <case value="1" resultType="DraftPost"/>  
  26.         </discriminator>  
  27.     </collection>  
  28. </resultMap>  

 

 

resultMap
      constructor – 类在实例化时,用来注入结果到构造方法中
           idArg – ID参数;标记结果作为ID可以帮助提高整体效能
           arg – 注入到构造方法的一个普通结果
      id – 一个ID结果;标记结果作为ID可以帮助提高整体效能
      result – 注入到字段或JavaBean属性的普通结果
     association – 一个复杂的类型关联;许多结果将包成这种类型
           嵌入结果映射 – 结果映射自身的关联,或者参考一个
     collection – 复杂类型的集
           嵌入结果映射 – 结果映射自身的集,或者参考一个
    discriminator – 使用结果值来决定使用哪个结果映射
         case – 基于某些值的结果映射
               嵌入结果映射 – 这种情形结果也映射它本身,因此可以包含很多相同的元素,或者它可以参照一个外部的结果映射。

 

 

id 和result
   
id和result都映射一个单独列的值到简单数据类型

 

这两者之间的唯一不同是id表示的结果将是当比较对象实例时用到的标识属性。这帮助来改进整体表现,特别是缓存和嵌入结果映射(也就是联合映射)。

 

它们共有的属性如下:

 

property

 

映射到列结果的字段或属性。如果匹配的是存在的,和给定名称相同的JavaBeans的属性,那么就会使用。否则MyBatis将会寻找给定名称的字段。这两种情形你可以使用通常点式的复杂属性导航。比如,你可以这样映射一些东西:“username”,或者映射到一些复杂的东西:“address.street.number”。

 

column

 

从数据库中得到的列名,或者是列名的重命名标签。这也是通常和会传递给resultSet.getString(columnName)方法参数中相同的字符串。

 

javaType

 

一个Java类的完全限定名,或一个类型别名(参加上面内建类型别名的列表)。如果你映射到一个JavaBean,MyBatis通常可以断定类型。然而,如果你映射到的是HashMap,那么你应该明确地指定javaType来保证所需的行为。

 

jdbcType

 

在这个表格之后的所支持的JDBC类型列表中的类型。JDBC类型是仅仅需要对插入,更新和删除操作可能为空的列进行处理。这是JDBC的需要,而不是MyBatis的。如果你直接使用JDBC编程,你需要指定这个类型-但仅仅对可能为空的值。

 

typeHandler

 

我们在前面讨论过默认的类型处理器。使用这个属性,你可以覆盖默认的类型处理器。这个属性值是类的完全限定名或者是一个类型处理器的实现,或者是类型别名。


constructor

 

。构造方法注入允许你在初始化时为类设置属性的值,而不用暴露出公有方法。MyBatis也支持私有属性和私有JavaBeans属性来达到这个目的,但是一些人更青睐构造方法注入。

为了向这个构造方法中注入结果,MyBatis需要通过它的参数的类型来标识构造方法。Java没有自查(反射)参数名的方法。所以当创建一个构造方法元素时,保证参数是按顺序排列的,而且数据类型也是确定的。

 

association

 

association关联元素处理“有一个”类型的关系,即一对一关联。它有两种关联方式

 

嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型。

 

嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集。

 

嵌套查询

 

Xml代码   收藏代码
  1. <resultMap  id="userResultMap" type="User">  
  2.     <id property="id" column="ID" jdbcType="NUMERIC" javaType="java.lang.Long"/>  
  3.     <result property="loginName" column="LOGIN_NAME" jdbcType="VARCHAR" javaType="java.lang.String"/>  
  4.     <result property="password" column="password" jdbcType="VARCHAR" javaType="java.lang.String"/>  
  5.     <result property="roleId" column="role_id" jdbcType="NUMERIC" javaType="java.lang.Long"/>  
  6.     <association property="role" column="role_id" javaType="Role" select="selectRole"/>  
  7. </resultMap>  
  8.   
  9. <select id="selectUser" parameterType="java.lang.Long" resultMap="userResultMap" >     
  10.     select * from User where id =#{id}     
  11. </select>   
  12.   
  13. <select id="selectRole" parameterType="java.lang.Long" resultType="Role" >     
  14.     select * from Role where id =#{id}     
  15. </select>   

 

这里有两个查询,一个查询加载User,一个查询加载Role.
这里select为另外一个映射语句的ID,可以加载这个属性映射需要的复杂类型。获取的在列属性中指定的列的值将被传递给目标select语句作为参数。

 

注意:
而select 为selectRole的SQL输入参数可以随便给名称,只要是输入参数与压入进去的值类型相同就行了,可以写成:

 

Xml代码   收藏代码
  1. select * from Role where id = #{sfffs}   

 

不管输入参数名称是什么,mybatis最终会执行:
效果为:

 

Sql代码   收藏代码
  1. select * from role where id =resultSet.getLong("Role_id");  

 

注意:要保证第二个查询查出来的结果只有一条记录。

 

要处理复合主键,你可以指定多个列名通过column="{prop1=col1,prop2=col2}"这种语法来传递给嵌套查询语句。这会引起prop1和prop2以参数对象形式来设置给目标嵌套查询语句。

Xml代码   收藏代码
  1. <resultMap  id="userResultMap" type="User">  
  2.     <id property="id" column="ID" jdbcType="NUMERIC" javaType="java.lang.Long"/>  
  3.     <result property="loginName" column="LOGIN_NAME" jdbcType="VARCHAR" javaType="java.lang.String"/>  
  4.     <result property="password" column="password" jdbcType="VARCHAR" javaType="java.lang.String"/>  
  5.     <result property="roleId" column="role_id" jdbcType="NUMERIC" javaType="java.lang.Long"/>  
  6.     <association property="role" column="{id=role_id,name=role_name}" javaType="Role" select="selectRole"/>  
  7. </resultMap>  
  8.   
  9. <select id="selectRole" parameterType="HashMap"  resultType="Role" >     
  10.     select * from Role where id =#{id} and name= #{name}     
  11. </select>  

  

这种方式很简单,但是对于大型数据集合和列表将不会表现很好。问题就是我们熟知的“N+1查询问题”。概括地讲,N+1查询问题可以是这样引起的:


   你执行了一个单独的SQL语句来获取结果列表(就是“+1”)。
   对返回的每条记录,你执行了一个查询语句来为每个加载细节(就是“N”)。


这个问题会导致成百上千的SQL语句被执行。这通常不是期望的。


比如一个查询用户列表的SQL,假如有2000个用户,那么就是一个查询用户的SQL和2000个查询角色的SQL,一共有2001个SQL被运行。


MyBatis能延迟加载这样的查询就是一个好处,因此你可以分散这些语句同时运行的消耗。然而,如果你加载一个列表,之后迅速迭代来访问嵌套的数据,你会调用所有的延迟加载,这样的行为可能是很糟糕的。

 

所以还有另外一种方法。


关联的嵌套结果

 

嵌套结果

 

Xml代码   收藏代码
  1. <resultMap  id="userResultMap" type="User">  
  2.     <id property="id" column="ID" jdbcType="NUMERIC" javaType="java.lang.Long"/>  
  3.     <result property="loginName" column="LOGIN_NAME" jdbcType="VARCHAR" javaType="java.lang.String"/>  
  4.     <result property="password" column="password" jdbcType="VARCHAR" javaType="java.lang.String"/>  
  5.     <result property="roleId" column="role_id" jdbcType="NUMERIC" javaType="java.lang.Long"/>  
  6.     <association property="role" column="role_id" javaType="Role" resultMap="roleResultMap"/>  
  7.         <id property="id" column="role_id"/>  
  8.         <result property="name" column="role_name"/>  
  9.     </association>  
  10. </resultMap>  
  11.   
  12. <resultMap id="roleResultMap" type="Role">  
  13.     <id property="id" column="role_id"/>  
  14.     <result property="name" column="role_name"/>  
  15. </resultMap>  

 

 

也可以这样配置

 

Xml代码   收藏代码
  1. <resultMap  id="userResultMap" type="User">  
  2.     <id property="id" column="ID" jdbcType="NUMERIC" javaType="java.lang.Long"/>  
  3.     <result property="loginName" column="LOGIN_NAME" jdbcType="VARCHAR" javaType="java.lang.String"/>  
  4.     <result property="password" column="password" jdbcType="VARCHAR" javaType="java.lang.String"/>  
  5.     <result property="roleId" column="role_id" jdbcType="NUMERIC" javaType="java.lang.Long"/>  
  6.     <association property="role" column="role_id" javaType="Role" resultMap="roleResultMap"/>  
  7. </resultMap>  
  8.   
  9. <resultMap id="roleResultMap" type="Role">  
  10.     <id property="id" column="role_id"/>  
  11.     <result property="name" column="role_name"/>  
  12. </resultMap>  

 

 resultMap这是结果映射的ID,可以映射关联的嵌套结果到一个合适的对象图中。这是一种替代方法来调用另外一个查询语句。这允许你联合多个表来合成到一个单独的结果集。这样的结果集可能包含重复,数据的重复组需要被分解,合理映射到一个嵌套的对象图。为了使它变得容易,MyBatis让你“链接”结果映射,来处理嵌套结果。一个例子会很容易来仿照,这个表格后面也有一个示例。

 

注意这个联合查询,以及采取保护来确保所有结果被唯一而且清晰的名字来重命名。

 

columnPrefix 属性

 

Xml代码   收藏代码
  1. <association property="role" column="role_id" javaType="Role" resultMap="roleResultMap" columnPrefix="role_"/>  
  2.         <id property="id" column="id"/>  
  3.         <result property="name" column="name"/>  
  4. </association>  

 

非常重要:在嵌套据诶过映射中id元素扮演了非常重要的角色。应应该通常指定一个或多个属性,它们可以用来唯一标识结果。实际上就是如果你离开她了,但是有一个严重的性能问题时MyBatis仍然可以工作。选择的属性越少越好,它们可以唯一地标识结果。主键就是一个显而易见的选择(尽管是联合主键)。

 

上面你已经看到了如何处理“有一个”类型关联。但是“有很多个”是怎样的?下面这个部分就是来讨论这个主题的。

 

collection

 

collection关联元素处理一对多关联。

 

Xml代码   收藏代码
  1. <resultMap  id="roleResultMap" type="Role">  
  2.     <id property="id" column="ID" jdbcType="NUMERIC" javaType="java.lang.Long"/>  
  3.     <result property="name" column="NAME" jdbcType="VARCHAR" javaType="java.lang.String"/>  
  4.     <result property="userId" column="user_id" jdbcType="NUMERIC" javaType="java.lang.Long"/>  
  5.     <collection property="user" column="user_id" javaType="ArrayList" ofType="Post" select="selectUser"/>  
  6.   
  7. </resultMap>  
  8.   
  9. <select id="selectUser" parameterType="java.lang.Long"   resultType="User" >     
  10.     select * from uer where id =#{id}   
  11. </select>   

 

同样,可以这样配置

 

Xml代码   收藏代码
  1. <resultMap  id="roleResultMap" type="Role">  
  2.     <id property="id" column="ID" jdbcType="NUMERIC" javaType="java.lang.Long"/>  
  3.     <result property="name" column="NAME" jdbcType="VARCHAR" javaType="java.lang.String"/>  
  4.     <result property="userId" column="user_id" jdbcType="NUMERIC" javaType="java.lang.Long"/>  
  5.     <collection property="user" column="user_id" javaType="ArrayList" ofType="Post">  
  6.         <id property="id" column="user_id"/>  
  7.         <result property="name" column="user_name"/>  
  8.     </collection>  
  9. </resultMap>  
  10. <resultMap id="userResultMap" type="User">  
  11.     <id property="id" column="user_id"/>  
  12.     <result property="name" column="user_name"/>  
  13. </resultMap>

  14.  另外一处的例子
  15. 基础部分可以查看我的另一篇博客http://haohaoxuexi.iteye.com/blog/1333271

    MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。在MyBatis进行查询映射的时候,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值。当提供的返回类型属性是resultType的时候,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。所以其实MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性,而当我们提供的返回类型是resultMap的时候,因为Map不能很好表示领域模型,我们就需要自己再进一步的把它转化为对应的对象,这常常在复杂查询中很有作用。

     

    有这样一个Blog.java文件

     

    Java代码   收藏代码
    1. import java.util.List;  
    2.   
    3. public class Blog {  
    4.   
    5.     private int id;  
    6.   
    7.     private String title;  
    8.   
    9.     private String content;  
    10.   
    11.     private String owner;  
    12.       
    13.     private List<Comment> comments;  
    14.   
    15.    // getter setter ...

    16. }  

     

    其所对应的数据库表中存储有id,title,Content,Owner属性,那么当我们进行下面这样一个查询映射的时候

     

    Xml代码   收藏代码
    1. <typeAlias alias="Blog" type="com.tiantian.mybatis.model.Blog"/><!--来自MyBatis的配置文件mybatis_config.xml-->  
    2. <select id="selectBlog" parameterType="int" resultType="Blog">  
    3.         select * from t_blog where id = #{id}  
    4. </select><!--来自SQL映射文件BlogMapper.xml-->  

     

     MyBatis会自动创建一个ResultMap对象,然后基于查找出来的属性名进行键值对封装,然后再看到返回类型是Blog对象,再从ResultMap中取出与Blog对象对应的键值对进行赋值。

    当返回类型直接是一个ResultMap的时候也是非常有用的,这主要用在进行复杂联合查询上,因为进行简单查询是没有什么必要的。我们先看看一个返回类型为ResultMap的简单查询,再看看复杂查询的用法。

    简单查询的写法

     

    Xml代码   收藏代码
    1. <resultMap type="Blog" id="BlogResult">  
    2.         <id column="id" property="id"/>  
    3.         <result column="title" property="title"/>  
    4.         <result column="content" property="content"/>  
    5.         <result column="owner" property="owner"/>  
    6.     </resultMap>  
    7.     <select id="selectBlog" parameterType="int" resultMap="BlogResult">  
    8.         select * from t_blog where id = #{id}  
    9.     </select>  

     

     select映射中resultMap的值是一个外部resultMap的id,表示返回结果映射到哪一个resultMap上,外部resultMap的type属性表示该resultMap的结果是一个什么样的类型,这里是Blog类型,那么MyBatis就会把它当作一个Blog对象取出。resultMap节点的子节点id是用于标识该对象的id的,而result子节点则是用于标识一些简单属性的,其中的Column属性表示从数据库中查询的属性,Property则表示查询出来的属性对应的值赋给实体对象的哪个属性。简单查询的resultMap的写法就是这样的。接下来看一个复杂一点的查询。

    有一个Comment类,其中有一个Blog的引用,表示是对哪个Blog的Comment,那么我们在查询Comment的时候把其对应的Blog也要查出来赋给其blog属性。

     

    Java代码   收藏代码
    1. import java.util.Date;  
    2.   
    3. public class Comment {  
    4.   
    5.     private int id;  
    6.       
    7.     private String content;  
    8.       
    9.     private Date commentDate = new Date();  
    10.       
    11.     private Blog blog;  
    12.   
    13.     // getter setter ...
    14. }  
     

     

    其写法是这样的

     

    Xml代码   收藏代码
    1. <!--来自CommentMapper.xml文件    -->  
    2.     <resultMap type="Comment" id="CommentResult">  
    3.         <association property="blog" select="selectBlog" column="blog" javaType="Blog"/>  
    4.     </resultMap>  
    5.       
    6.     <select id="selectComment" parameterType="int" resultMap="CommentResult">  
    7.         select * from t_Comment where id = #{id}  
    8.     </select>  
    9.       
    10.     <select id="selectBlog" parameterType="int" resultType="Blog">  
    11.         select * from t_Blog where id = #{id}  
    12.     </select>  

    其访问情况是这样的,先是请求id为selectComment的select映射,然后得到一个id为CommentResult的ResultMap对象,我们可以看到在对应的resultMap的返回类型是一个Comment对象,其中只有一个association节点,而没有像前面说的简单查询所对应的id,result子节点,但是其仍会把对应的id等属性赋给Comment对象,这就是前面所说的MyBatis拥有自动封装功能,只要你提供了返回类型,MyBatis会根据自己的判断来利用查询结果封装对应的对象,所以前面的简单查询中,如果你不在resultMap中明确的指出id对应哪个字段,title对应哪个字段,MyBatis也会根据自身的判断来帮你封装,MyBatis的自身判断是把查询的field或其对应的别名与返回对象的属性进行比较,如果相匹配且类型也相匹配,MyBatis则会对其进行赋值。在上面对应的resultMap中关联了一个blog属性,其对应的JAVA类型为Blog,在上述的写法中,关联对象是通过子查询来进行关联的,当然也可以直接通过关联查询来进行关联。上面的association子节点中,Property属性表示是resultMap返回类型的哪个关联属性,对于上面的例子就是Comment管理的blog属性;select表示进行哪个select映射来映射对应的关联属性,即会去请求id为select所对应的值的select映射 来查询出其所关联的属性对象;Column表示当前关联对象在id为CommentResult的resultMap中所对应的键值对,该键值对将作为对关联对象子查询的参数,即将把在selectComment中查询出来的blog属性的值作为参数传给进行关联对象blog的子查询selectBlog的参数;javaType表示当前关联对象在JAVA中是什么类型。

     

    上述介绍的是一对一或一对多的情况下,对一的一方的关联的查询。在实际应用中还有一个用的比较多的应用是通过一的一方查出对应的多的一方,在拿出多的一方的时候也同样要把一的一方关联上,即在上述例子中,在拿出Blog对象的时候,就把其对应的Comment全部拿出来,在拿出Comment的时候也还是需要把其对应的Blog拿出来,这是在JAVA中通过一次请求就拿出来的。写法如下:

     

    Xml代码   收藏代码
    1. <!-- 来自BlogMapper.xml文件 -->  
    2.     <resultMap type="Blog" id="BlogResult">  
    3.         <id column="id" property="id"/>  
    4.         <collection property="comments" select="selectCommentsByBlog" column="id" ofType="Comment"></collection>  
    5.     </resultMap>  
    6.   
    7.     <resultMap type="Comment" id="CommentResult">  
    8.         <association property="blog" javaType="Blog" column="blog" select="selectBlog"/>  
    9.     </resultMap>  
    10.   
    11.     <select id="selectBlog" parameterType="int" resultMap="BlogResult">  
    12.         select * from t_blog where id = #{id}  
    13.     </select>  
    14.   
    15. <!--  通过Blog来查找Comment   -->  
    16.     <select id="selectCommentsByBlog" parameterType="int" resultMap="CommentResult">  
    17.         select * from t_Comment where blog = #{blogId}  
    18.     </select>  

    上述请求的入口是id为selectBlog的select映射,返回结果为id为BlogResult的resultMap,id为BlogResult的类型为Blog,其中指定了id的属性和字段,指定id将对MyBatis内部的构造作用非常大。其中关联了一个comments对象,因为一个Blog可以有很多Comment,该comments为一个集合,所以用集合collection进行映射,其中的select还是表示进行哪个子查询来查询对应的comments,column表示把上述查出来的哪个字段值当作参数传给子查询,ofType也是表示返回类型,这里的返回类型是集合内部的类型,之所以用ofType而不是用type是MyBatis内部为了和关联association进行区别。 

     

     

     

    测试代码:

     

    Java代码   收藏代码
    1. @Test  
    2. public void selectCommentsByBlogTest() {  
    3.     SqlSession session = Util.getSqlSessionFactory().openSession();  
    4.     CommentMapper commentMapper = session.getMapper(CommentMapper.class);  
    5.     List<Comment> comments = commentMapper.selectCommentsByBlog(6);  
    6.     for (Comment comment : comments)  
    7.         System.out.println(comment);  
    8.     session.close();  
    9. }  
    10.   
    11. /** 
    12.  * 查询单条记录 
    13.  */  
    14. @Test  
    15. public void testSelectOne() {  
    16.     SqlSession session = Util.getSqlSessionFactory().openSession();  
    17.     BlogMapper blogMapper = session.getMapper(BlogMapper.class);  
    18.     Blog blog = blogMapper.selectBlog(6);  
    19.     List<Comment> comments = blog.getComments();  
    20.     if (comments != null) {  
    21.         System.out.println("--------------Comments Size------------" + comments.size());  
    22.         for (Comment comment : comments)  
    23.             System.out.println(comment);  
    24.     }  
    25.     session.close();  


  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值