表 1 :products 产品表
createtable JC_PRODUCTS
(
PRODUCTS_ID NUMBERnotnull,
SITE_ID NUMBER,
TITLE VARCHAR2(30),
DESCRIPTIONVARCHAR2(300),
IMGPATH VARCHAR2(300)
)
表 2 :productsExt 产品扩展表
createtableJC_PRODUCTS_EXT
(
PRODUCTS_ID NUMBERnotnull,
EMAIL VARCHAR2(100),
PHONE VARCHAR2(100),
QQ VARCHAR2(50)
)
以下为多表(增、删、改)的实现流程,注意DAO和Manager实现类的注解。
XML文件声明需到项目中查看,在此就不做记录了。
第一步:建立实体对象。
Products表实体对象:
public
class BaseCmsProducts implements Serializable {
public
static String PROP_EXT =
"ext";
...
// one to one 多增加 productsExt表对象,并生成get,set方法。
privatecom.jeecms.cms.entity.assist.CmsProductsExt
ext;
...
}
public
class CmsProducts extends BaseCmsProducts {... }
Products表实体对象映射:
<hibernate-mapping
package="com.jeecms.cms.entity.assist">
<class
name="CmsProducts"
table="jc_Products">
<meta
attribute="sync-DAO">false</meta>
<cache
usage="read-write"/>
<id
name="id"type="integer"
column="products_id">
<generator
class="sequence"><param
name="sequence">S_JC_PRODUCTS</param></generator>
</id>
...
<one-to-one
name="ext"
class="CmsProductsExt"
cascade="delete"/>
</hibernate-mapping>
ProductsExt表实体对象:
public
class BaseCmsProductsExt implements Serializable {
public
static String PROP_PRODUCTS =
"products";
...
// one to one 多增加 products表对象,并生成get,set方法。
privatecom.jeecms.cms.entity.assist.CmsProducts
products;
}
public
class CmsProductsExt extends BaseCmsProductsExt {... }
ProductsExt表实体对象映射:
<hibernate-mapping
package="com.jeecms.cms.entity.assist">
<class
name="CmsProductsExt"
table="jc_Products_Ext">
<meta
attribute="sync-DAO">false</meta>
<cache
usage="read-write"/>
<id
name="id"type="integer"
column="products_id">
<generator
class="foreign"><param
name="property">products</param></generator>
</id>
...
<one-to-one
name="products"class="CmsProducts"
constrained="true"/>
</class>
</hibernate-mapping>
第二步:写DAO。
CmsProductsDao 只写自己的增删改查:
public
interface CmsProductsDao {
public Pagination getPage(Integer siteId,
int pageNo, int pageSize);
public List<CmsProducts> getList(Integer siteId);
public CmsProducts findById(Integer id);
public CmsProducts save(CmsProducts bean);
public CmsProducts updateByUpdater(Updater<CmsProducts>updater);
public CmsProducts deleteById(Integer id);
}
CmsProductsExtDao只写自己的增删改查
public
interface CmsProductsExtDao {
public CmsProductsExt findById(Integer id);
public CmsProductsExt save(CmsProductsExt bean);
public CmsProductsExtupdateByUpdater(Updater<CmsProductsExt> updater);
public CmsProductsExt deleteById(Integer id);
}
第三步:写Manager。
CmsProductsMng 注意 save 和 updateByUpdater需要传入双对象:
public
interface CmsProductsMng {
public Pagination getPage(Integer siteId,
int pageNo, int pageSize);
public List<CmsProducts> getList(Integer siteId);
public CmsProducts findById(Integer id);
public CmsProducts save(CmsProducts bean,CmsProductsExt ext);
public CmsProducts updateByUpdater(Updater<CmsProducts>updater,CmsProductsExt ext);
public CmsProducts deleteById(Integer id);
public CmsProducts[] deleteByIds(Integer[] ids);
}
CmsProductsExtMng 注意 hibernate映射时已控制了关联删除,添加时要传入双对象:
public
interface CmsProductsExtMng {
public CmsProductsExt save(CmsProductsExt ext, CmsProductsproducts);
public CmsProductsExt update(CmsProductsExt ext);
}
第四步:只要前台页面把值传给主表的Action(CmsProductsAct) 即可:
前台页面:
<@p.form id="jvForm"action="o_save.do" labelWidth="12">
<@p.text colspan="2"label="cmsProducts.title" name="title" /><@p.tr/>
<@p.text colspan="2"label="cmsProducts.description" name="description" /><@p.tr/>
<@p.text colspan="2"label="cmsProducts.qq" name="qq" /><@p.tr/>
<@p.text colspan="2"label="cmsProducts.email" name="email" /><@p.tr/>
<@p.text colspan="2"label="cmsProducts.phone" name="phone" /><@p.tr/>
CmsProductsAct:
@Controller
public
class CmsProductsAct {
@RequestMapping("/Products/o_save.do")
public Stringsave(CmsProducts bean,CmsProductsExt ext,
HttpServletRequestrequest,ModelMap model) {
...
bean = manager.save(bean,ext);
log.info("save CmsProducts id={}.", bean.getId());
cmsLogMng.operating(request,
"CmsProducts.log.save",
"id="
+ bean.getId() + ";title=" + bean.getTitle()+";siteid="
+bean.getSite().getId()+";imgpath="+bean.getImgpath());
return
"redirect:v_list.do";}
@RequestMapping("/Products/o_update.do")
public String update(Integer id,CmsProducts bean,CmsProductsExt ext,
HttpServletRequest request,ModelMap model) {
...
bean = manager.updateByUpdater(updater,ext);
log.info("updateCmsProducts id={}.", bean.getId());
cmsLogMng.operating(request,
"CmsProducts.log.update",
"id="
+ bean.getId() + ";title=" + bean.getTitle()+";siteid="
+bean.getSite().getId()+";imgpath="+bean.getImgpath());
return
"redirect:v_list.do";
}
}