一 通过逆向工程生成po与mapper
MyBatis官方提供了强大的逆向工程代码,即可以通过数据库生成po与mapper,这里不去做过多的讨论,争取写完这一系列后更新一篇关于mybatis逆向工程的博文。
生成后目录如下(注:ItemsMapperCustom,ItemsCustom,ItemsQueryVo为自定义po与mapper,下一部分将讲解):
二 手工定义po与mapper
随着项目的进展和改进,往往需要开发人员自己定义po与mapper,实现综合查询等功能。
1 po下的ItemsCustom与ItemsQueryVo
ItemsCustom是为了满足商品信息扩展而实现的类。我们知道,通过逆向工程生成的po来源的属性其实都是表中的字段,当我们想对商品增加属性时,如果还是通过逆向工程来再次生成po,那么就需要修改数据库,再执行逆向工程,这个工作量是很大的。
package com.lpc.po;
/**
* 商品信息的扩展类
*/
public class ItemsCustom extends Items{
}
对于ItemsQueryVo,它是为了商品的包装类。
package com.lpc.po;
/**
* 商品的包装类
*/
public class ItemsQueryVo {
//商品信息
private Items items;
//为了系统的可扩展性,对原始生成的po进行扩展
private ItemsCustom itemsCustom;
public ItemsCustom getItemsCustom() {
return itemsCustom;
}
public void setItemsCustom(ItemsCustom itemsCustom) {
this.itemsCustom = itemsCustom;
}
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
}
2 ItemsMapperCustom与ItemsMapperCustom.xml
ItemsMapperCustom
这里只实现查询商品的简单功能。