Mybatis + mysql 返回自增主键
2015-08-18 00:00
489 查看
摘要: Mybatis + mysql insert 返回自增主键
写出来作为今天的教训
网上介绍的方法有很多 此处只写这一种
步骤1:
<!--这里设置useGeneratedKeys、keyProperty这两个属性 keyProperty的值对应javaBean中的主键属性-->
<insert id="insert"
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}
)
</insert>
步骤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);
}
}
写出来作为今天的教训
网上介绍的方法有很多 此处只写这一种
步骤1:
<!--这里设置useGeneratedKeys、keyProperty这两个属性 keyProperty的值对应javaBean中的主键属性-->
<insert id="insert"
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}
)
</insert>
步骤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);
}
}
相关文章推荐
- ubuntu 12.04 安装 Cloudera Manager5及CDH5(Mysql)【添加yum源方式安装集群】
- mysql 事务提交过程
- 如何将navicat for mysql 中查询出来的内容导出到xls文件中
- MySQL批量SQL插入性能优化
- 【MySQL】MySQL for Mac 环境变量的配置
- MySQL存储引擎
- MySQL双主(Master-Master)同步
- mysql下载地址
- MySQL5.7第8章翻译系列——8.1 优化概述
- mysql日志问题定位实用命令
- ubuntu下mysql安装(server、client、dev),开启、停止和重启,及常见错误
- 由浅入深探究 MySQL索引结构原理、性能分析与优化
- MySQL权限管理总结
- mysql中,索引,主键,唯一索引,联合索引的区别
- MySQL日志总结
- MySQL 调优/优化的 100 个建议
- 8 个不得不说的 MySQL 陷阱
- MySQL索引类型以及创建
- mysql修改用户密码
- mysql进程SHOW PROCESSLIST