mybatis获取目标及其相关目标的两种方法ITeye - 乐橙lc8

mybatis获取目标及其相关目标的两种方法ITeye

2019年02月18日11时15分10秒 | 作者: 曜坤 | 标签: 目标,方法,代码 | 浏览: 2328

场景如下:有一个Book类,一个Category类,Book类有一个对Category的引证,要求获取Book目标的一起取出Category目标

Book.java类代码如下

public class Book implements Serializable {
 private static final long serialVersionUID = 8634505584095346474L;
 private String id;
 private String name;
 private Date publishDate;
 private String author;
 private Float price;
 private Category category;
 private Long quantity;
 // setter and getter method is omited!
}

 Category.java类代码如下

 

public class Category implements Serializable {
 private static final long serialVersionUID = 8964146416205979692L;
 private String id;
 private String name;
 private String spell;
 private String comments;
 //setter and getter method is omited!
}

 对应获取Book目标的两种方法,经过装备BookMapper.xml中完成

 

方法一(经过装备映射完成):

 resultMap id="bookMap" type="Book" 
 id property="id" column="id"/ 
 result property="name" column="name"/ 
 result property="publishDate" column="publishDate"/ 
 result property="author" column="author"/ 
 result property="price" column="price"/ 
 result property="quantity" column="quantity"/ 
 association property="category" javaType="Category" 
 id property="id" column="category_id" javaType="string"/ 
 id property="name" column="category_name"/ 
 id property="spell" column="category_spell"/ 
 id property="comments" column="category_comments"/ 
 /association 
 /resultMap 
 select id="getBook" parameterType="string" resultMap="bookMap" 
select b.id,
 b.name,
 b.publishDate,
 b.author,
 b.price,
 b.category,
 b.quantity,
 c.id as category_id,
 c.name as category_name,
 c.spell as category_spell,
 c.comments as category_comments 
from t_book b left outer join t_bookcategory c on b.category=c.id where b.id=#{id}
 /select 

 方法二(经过OGNL完成):

 

 select id="getBook" parameterType="string" resultType="Book" 
select b.id,
 b.name,
 b.publishDate,
 b.author,
 b.price,
 b.quantity,
 c.id as "category.id",
 c.name as "category.name",
 c.spell as "category.spell",
 c.comments as "category.comments" 
from t_book b left outer join t_bookcategory c on b.category=c.id where b.id=#{id}
 /select 

 

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表乐橙lc8立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章