1、主键策略
在Oracle中,sequence提供多表多字段可共用一个不重复值。MySQL中存在自增列,基本可以满足PK的要求。但自增列存在限制:
a.只能用于表中的一个字段,一张不能同时存在两个以上的自增列;
b.自增列必须被定义为key(PK或FK);
c.自增列不能被多个表共用;
d.当insert语句不包括自增字段或将其值设置为NULL时,该值会自动填上
oracle本身支持sequence:
create sequence PHOTO_LABEL_TYPE_SEQ
minvalue 1
maxvalue 9999999999999999999999999999
start with 801
increment by 1;
定义好sequence后,你就可以用currVal,nextVal取得值。
CurrVal:返回 sequence的当前值
NextVal:增加sequence的值,然后返回 增加后sequence值
<insert id="insert" parameterClass="PhotoLabelType">
<selectKey keyProperty="photoLabelTypeId" resultClass="java.lang.Long">
SELECT PHOTO_LABEL_TYPE_SEQ.NEXTVAL FROM DUAL
</selectKey>
<![CDATA[
insert into PHOTO_LABEL_TYPE
(PHOTO_LABEL_TYPE_ID, PHOTO_LABEL_TYPE_NAME)
values
(#photoLabelTypeId#, #photoLabelTypeName#)
]]>
</insert>
public void insert(PhotoLabelType photoLabelType) {
super.insert("PHOTO_LABEL_TYPE.insert", photoLabelType);
}
此时可以获取到id:
photoLabelTypeDAO.insert(photoLabelType);
PhotoLabel photoLabel = new PhotoLabel();
photoLabel.setPhotoLabelTypeId(photoLabelType.getPhotoLabelTypeId());
而mysql不支持
<insert id="insert" parameterType="" keyProperty="id" useGeneratedKeys="true">
insert int...
</insert>