以下例子:
-------------Book表(相应的model为Book)----------------------------
bookId,
title,
authorId(作者表)
------------------------------------------------------------------
-------------Author表(相应的model为Author)----------------------------
authorId,
authorName
------------------------------------------------------------------
那么我建立Book表的model层的时候,该怎样建立?
第一种:public class Book{
private int bookId;
private String title;
private authorId;
..相应的get和set
}第二种:public class Book{
private int bookId;
private String title;
private Author author(区别在这);
..相应的get和set
}该用哪种方式定义Book呢
说明一下,我的数据库框架是ibatis,
如果用第一种的话,直接用resultClass就可以,很简单: <select id="selectBookById" parameterClass="java.lang.Integer" resultClass="Book">
select * from bs_book where bookId=#bookId#
</select>如果我使用第二种的话,要是查询的话,我得使用resultMap:<resultMap class="Book" id="BookMap">
<result property="bookId" column="bookId" />
<result property="title" column="title" />
<result property="author" column="authorId" select="selectAuthorById" />
<select id="selectBookById" parameterClass="java.lang.Integer" resultMap="BookMap">
select * from bs_book where bookId=#bookId#
</select>
<select id="selectAuthorById" parameterClass="java.lang.Integer" resultClass="Author">
select * from bs_author where authorId=#authorId#
</select>
</resultMap>注意下resultMap,它要求model的每个属性都得写一个 <result property="属性名(必须有get和set)" column="列名" />这是个问题,我简化了模型,实际上我的Book表有将近20个属性,那么写那么多resultMap,是不是很痛苦?
我应该选哪种model,我倾向于第二种,可是谁能给我解决resultMap必须写每个property的问题??
谢谢了
说明一下:不写某个列的property,则该列是查不出来的,我试过的。
-------------Book表(相应的model为Book)----------------------------
bookId,
title,
authorId(作者表)
------------------------------------------------------------------
-------------Author表(相应的model为Author)----------------------------
authorId,
authorName
------------------------------------------------------------------
那么我建立Book表的model层的时候,该怎样建立?
第一种:public class Book{
private int bookId;
private String title;
private authorId;
..相应的get和set
}第二种:public class Book{
private int bookId;
private String title;
private Author author(区别在这);
..相应的get和set
}该用哪种方式定义Book呢
说明一下,我的数据库框架是ibatis,
如果用第一种的话,直接用resultClass就可以,很简单: <select id="selectBookById" parameterClass="java.lang.Integer" resultClass="Book">
select * from bs_book where bookId=#bookId#
</select>如果我使用第二种的话,要是查询的话,我得使用resultMap:<resultMap class="Book" id="BookMap">
<result property="bookId" column="bookId" />
<result property="title" column="title" />
<result property="author" column="authorId" select="selectAuthorById" />
<select id="selectBookById" parameterClass="java.lang.Integer" resultMap="BookMap">
select * from bs_book where bookId=#bookId#
</select>
<select id="selectAuthorById" parameterClass="java.lang.Integer" resultClass="Author">
select * from bs_author where authorId=#authorId#
</select>
</resultMap>注意下resultMap,它要求model的每个属性都得写一个 <result property="属性名(必须有get和set)" column="列名" />这是个问题,我简化了模型,实际上我的Book表有将近20个属性,那么写那么多resultMap,是不是很痛苦?
我应该选哪种model,我倾向于第二种,可是谁能给我解决resultMap必须写每个property的问题??
谢谢了
说明一下:不写某个列的property,则该列是查不出来的,我试过的。
第二种方式:如果你只想查找一个表的某些字段,你只需要返回一个map对象即可
这里是不是可以建 一个关联自动关联到authorId就不用写那些东西了啊
properties多就多写己行吧。要不就写个生成工具,也不麻烦。我觉得还是看许需求来决定实现方法。
如果“只要需要书籍的信息,就需要作者的信息”的话,
就用第2种,这样也一劳永逸。免去了每次取得数据都还要再取一次作者的“麻烦”。如果“作者信息只是在需要的时候才取得”这样的需求,
我的话还是选择第1种方法,
这样起码节省了处理多余数据在网络上的“流量”,
还能降低些些程序的复杂度。good luck
这个其实是非必须的,如果数据库字段与bean命名一致的话
推荐torque
效率也不差