mysql自增mybatis返回主键_Mybatis + mysql 返回自增主键
網上介紹的方法有很多 此處只寫這一種
步驟1:
parameterType="com.asiainfo.category.website.bean.WebsiteClassify"
useGeneratedKeys="true" keyProperty="websiteClassifyId">
insert into website_classify
(website_classify_id,website_id, website_classify_name, website_classify_level, parent_website_classify_id)
values (#{websiteClassifyId},#{websiteId,jdbcType=INTEGER}, #{websiteClassifyName,jdbcType=VARCHAR},
#{websiteClassifyLevel,jdbcType=INTEGER}, #{parentWebsiteClassifyId,jdbcType=INTEGER}
)
步驟2:(重點)
/**
*
* @Title: insertAndGetParentWebsiteClassifyId
* @Description: TODO(插入操作)
* @param ?@return ?參數
* @return ?int 返回類型
* @throws
*/
public int insertAndGetParentWebsiteClassifyId(Integer websiteId, String cat, Integer parentWebsiteClassifyId,
Integer classifyLvel) {
WebsiteClassify websiteClassify = new WebsiteClassify();
websiteClassify.setWebsiteClassifyName(cat);
websiteClassify.setParentWebsiteClassifyId(parentWebsiteClassifyId);
websiteClassify.setWebsiteClassifyLevel(classifyLvel);
websiteClassify.setWebsiteId(websiteId);
websiteClassifyDao.insert(websiteClassify);? //這里插入成功,返回值永遠都是1
return websiteClassify.getWebsiteClassifyId(); //這樣來取值
}
附Dao
@Repository
public class WebsiteClassifyDao {
@Autowired
private WebsiteClassifyMapper websiteClassifyMapper;
public WebsiteClassify findByCondition(WebsiteClassify websiteClassify) {
return websiteClassifyMapper.findByCondition(websiteClassify);
}
public int insert(WebsiteClassify websiteClassify) {
return websiteClassifyMapper.insert(websiteClassify);
}
}
原文:http://my.oschina.net/u/2329222/blog/494050
總結
以上是生活随笔為你收集整理的mysql自增mybatis返回主键_Mybatis + mysql 返回自增主键的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哒哒智运怎么删除车辆
- 下一篇: 在win8上安装mysql_如何在Win