mybatis中useGeneratedKeys和keyProperty的使用

领域模型主键属性是shopId,使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型shopId属性中,
配置参考如下:
<insert id="insert" parameterType="com.XXX.domain.ShopBaseInfo" useGeneratedKeys="true" keyProperty="shopId">
INSERT INTO
<include refid="TABLE"/>
(
shop_name,
shop_keeper_id,
shop_desc,
shop_template_id,
shop_logo_url,
shop_backgroud_pic,
shop_type,
sync_status,
xx_shop_id,
platform_id,
created,
modified,
yn
)VALUES(
#{shopName},
#{shopKeeperId},
#{shopDesc},
#{shopTemplateId},
#{shopLogoUrl},
#{shopBackgroudPic},
#{shopType},
#{syncStatus},
#{xxShopId},
#{platformId},
#{created},
#{modified},
#{yn}
)
</insert>

java代码Demo测试类:
//测试类
public class TestInsert{
@Resource
private ShopBaseInfoMapper shopBaseInfoMapper;
public Long insert(@NotNull ShopBaseInfo entity) {
      ShopBaseInfo shop = new ShopBaseInfo();
shop.setPlatformId(platformId);
shop.setShopType(1);//todo 根据用户信息--------设定店铺类型
if(StringUtils.isBlank(user.getJcbNickName())){
shop.setShopName(user.getJcbMobile()+"的店");
}else{
shop.setShopName(user.getJcbNickName()+"的店");
}
shop.setShopLogoUrl(user.getJcbHeader());
shop.setShopKeeperId(user.getId());
shop.setShopDesc("掌柜比较懒,。。。。。。。。。。。");
shop.setShopTemplateId(10001);
shop.setCreated(new Date());
shop.setModified(new Date());
shop.setYn(1);
shopBaseInfoMapper.setUTF8MB4();
shopBaseInfoMapper.insert(shop);
log.info("platformId is {},userid is {},shopId is {}",platformId,userId,shop.getShopId());
//todo 调用insert方法后,会怎么给shop对象的shopId属性赋值,所以方法直接返回shopId就可以。
return shop.getShopId();

  }
}
//Mapper类
public interface ShopBaseInfoMapper{
/**
* 新增
* @param entity
* @return
*/
Integer insert(ShopBaseInfo entity);

}
 
posted @ 2019-05-22 18:48  xuzhujack  阅读(1945)  评论(0编辑  收藏  举报
;