insert主键返回 selectKey使用
有时候新增一条数据,知道新增成功即可,但是有时候,需要这条新增数据的主键,以便逻辑使用,再将其查询出来明显不符合要求,效率也变低了。
这时候,通过一些设置,mybatis可以将insert的数据的主键返回,直接拿到新增数据的主键,以便后续使用。
这里主要说的是selectKey标签
设计表的时候有两种主键,一种自增主键,一般为int类型,一种为非自增的主键,例如用uuid等。
首先说自增类型的主键。
1 映射xml中添加如下代码,注释写的很清楚了,不多做赘述。
- <!--新增信息,并拿到新增信息的表主键信息。
- 新增数据,得到主键的外层写法没什么特别,跟普通的insert一样。只不过里面加了selectKey-->
- <insert id="insertAndgetkey" parameterType="com.soft.mybatis.model.User">
- <!--selectKey 会将 SELECT LAST_INSERT_ID()的结果放入到传入的model的主键里面,
- keyProperty 对应的model中的主键的属性名,这里是 user 中的id,因为它跟数据库的主键对应
- order AFTER 表示 SELECT LAST_INSERT_ID() 在insert执行之后执行,多用与自增主键,
- BEFORE 表示 SELECT LAST_INSERT_ID() 在insert执行之前执行,这样的话就拿不到主键了,
- 这种适合那种主键不是自增的类型
- resultType 主键类型 -->
- <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
- SELECT LAST_INSERT_ID()
- </selectKey>
- insert into t_user (username,password,create_date) values(#{username},#{password},#{createDate})
- </insert>
2 接口 UserDao
- /**
- * 新增用户信息,并得到新增数据的主键
- * 主键自增
- * @return
- */
- int insertAndGeyKey(User user);
3 实现类 UserDaoImpl
- public int insertAndGeyKey(User user) {
- SqlSession sqlSession = null;
- try {
- sqlSession = SqlsessionUtil.getSqlSession();
- int key = sqlSession.insert("test.insertAndgetkey",user);
- // commit
- sqlSession.commit();
- return key;
- } catch (Exception e) {
- sqlSession.rollback();
- e.printStackTrace();
- } finally {
- SqlsessionUtil.closeSession(sqlSession);
- }
- return 0;
- }
接下来就是测试了,
UserDaoTest
- /**
- * 注意,user.xml中已经说过,selectKey会将得到的主键放入model的主键属性中,
- * 所以这里获取主键的方法一定是通过model.get主键才能获取新增的主键
- * @throws Exception
- */
- @Test
- public void insertAndGeyKey() throws Exception {
- User user = new User();
- user.setUsername("新增得到主键5");
- user.setPassword("123456");
- user.setCreateDate(new Date());
- int result = dao.insertAndGeyKey(user);
- System.out.println("insertAndGeyKey :" + result);
- // 获取新增数据主键
- System.out.println("新增数据的主键 :" + user.getId());
- }
数据库表新增数据主键为 34
junit测试结果 得到主键 34 测试成功。
2 自增主键的获取方法,说完了,下面来讲讲非自增主键的获取方法。大致一样,些许不同。
由于只有一张表,这里又新建了一张表,对应的xml,别忘了将新建的xml添加到sqlMapConfig.xml中。
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <!-- namespace命名空间,有种java package的感觉,sql隔离,这个名字必须唯一
- 并且不能省略不写也不能为空,不过名字倒是可以随意,只要不跟另外一个文件中的名字一样即可-->
- <mapper namespace="customer">
- <!-- 跟普通的insert没有什么不同的地方 -->
- <insert id="insert" parameterType="com.soft.mybatis.model.Customer">
- <!-- 跟自增主键方式相比,这里的不同之处只有两点
- 1 insert语句需要写id字段了,并且 values里面也不能省略
- 2 selectKey 的order属性需要写成BEFORE 因为这样才能将生成的uuid主键放入到model中,
- 这样后面的insert的values里面的id才不会获取为空
- 跟自增主键相比就这点区别,当然了这里的获取主键id的方式为 select uuid()
- 当然也可以另写别生成函数。-->
- <selectKey keyProperty="id" order="BEFORE" resultType="String">
- select uuid()
- </selectKey>
- insert into t_customer (id,c_name,c_sex,c_ceroNo,c_ceroType,c_age)
- values (#{id},#{name},#{sex},#{ceroNo},#{ceroType},#{age})
- </insert>
- </mapper>
接口 CustomerDao
- package com.soft.mybatis.dao;
- import com.soft.mybatis.model.Customer;
- /**
- * Created by xuweiwei on 2017/9/10.
- */
- public interface CustomerDao {
- int add(Customer customer);
- }
实现类 CustomerDaoImpl
- package com.soft.mybatis.dao.impl;
- import com.soft.mybatis.Util.SqlsessionUtil;
- import com.soft.mybatis.dao.CustomerDao;
- import com.soft.mybatis.model.Customer;
- import org.apache.ibatis.session.SqlSession;
- /**
- * Created by xuweiwei on 2017/9/10.
- */
- public class CustomerDaoImpl implements CustomerDao {
- public int add(Customer customer) {
- SqlSession sqlSession = null;
- try {
- sqlSession = SqlsessionUtil.getSqlSession();
- int key = sqlSession.insert("customer.insert", customer);
- // commit
- sqlSession.commit();
- return key;
- } catch (Exception e) {
- sqlSession.rollback();
- e.printStackTrace();
- } finally {
- SqlsessionUtil.closeSession(sqlSession);
- }
- return 0;
- }
- }
准备工作完毕,下面进行测试。
执行前的数据
测试类 CustomerDaoImplTest
- package com.soft.mybatis.dao.impl;
- import com.soft.mybatis.dao.CustomerDao;
- import com.soft.mybatis.model.Customer;
- import org.junit.Test;
- import static org.junit.Assert.*;
- /**
- * Created by xuweiwei on 2017/9/10.
- */
- public class CustomerDaoImplTest {
- private CustomerDao customerDao = new CustomerDaoImpl();
- @Test
- public void add() throws Exception {
- Customer customer = new Customer();
- customer.setName("全球鹰1");
- customer.setAge(15);
- customer.setCeroNo("888888888888");
- customer.setCeroType(2);
- customer.setSex(1);
- int result = customerDao.add(customer);
- System.out.println("插入结果 : "+result);
- System.out.println("插入主键id : "+customer.getId());
- }
- }
测试结果
数据库
可以看到新增的数据的主键已经获取到了。
注意点:获取主键,一定要从穿进去的model中获取。
附 新增customer表的建表ddl
- CREATE TABLE `t_customer` (
- `id` varchar(50) NOT NULL,
- `c_name` varchar(20) DEFAULT NULL COMMENT '姓名',
- `c_sex` tinyint(4) DEFAULT NULL COMMENT '性别',
- `c_ceroNo` varchar(18) DEFAULT NULL COMMENT '证件号码',
- `c_ceroType` tinyint(4) DEFAULT NULL COMMENT '1 身份证 2其他',
- `c_age` int(3) DEFAULT NULL COMMENT '年龄',
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8
insert主键返回 selectKey使用的更多相关文章
- Mybatis里Mapper映射sql文件里insert的主键返回selectKey使用
有时候新增一条数据,知道新增成功即可,但是有时候,需要这条新增数据的主键,以便逻辑使用,再将其查询出来明显不符合要求,效率也变低了. 这时候,通过一些设置,mybatis可以将insert的数据的主键 ...
- 存储过程返回update结果集和insert主键
update teacher set name ='111' where id in(286,289);print @@rowcount;--或select将查出,是@@rowcount,不是@row ...
- MyBatis主键返回
在使用MyBatis做持久层时,insert语句默认是不返回记录的主键值,而是返回插入的记录条数:如果业务层需要得到记录的主键时,可以通过配置的方式来完成这个功能. 比如在表的关联关系中,将数据插入主 ...
- mybatis由浅入深day01_4.7根据用户名称模糊查询用户信息_4.8添加用户((非)自增主键返回)
4.7 根据用户名称模糊查询用户信息 4.7.1 映射文件 使用User.xml,添加根据用户名称模糊查询用户信息的sql语句. 4.7.2 程序代码 控制台: 4.8 添加用户 4.8.1 映射文件 ...
- mybatis主键返回的实现
向数据库中插入数据时,大多数情况都会使用自增列或者UUID做为主键.主键的值都是插入之前无法知道的,但很多情况下我们在插入数据后需要使用刚刚插入数据的主键,比如向两张关联表A.B中插入数据(A的主键是 ...
- mybatis的执行流程 #{}和${} Mysql自增主键返回 resultMap 一对多 多对一配置
n Mybatis配置 全局配置文件SqlMapConfig.xml,配置了Mybatis的运行环境等信息. Mapper.xml文件即Sql映射文件,文件中配置了操作数据库的Sql语句.此文件需要在 ...
- mybatis入门--主键返回(九)
自增主键返回 mysql自增主键,执行insert提交之前自动生成一个自增主键. 通过mysql函数获取到刚插入记录的自增主键: LAST_INSERT_ID() 是insert之后调用此函数. 修改 ...
- mybatis+oracle 完成插入数据库,并将主键返回的注意事项
mybatis+oracle 完成插入数据库,并将主键返回的注意事项一条插入语句就踩了不少的坑,首先我的建表语句是: create table t_openapi_batch_info( BATCH_ ...
- (四)mybatis 的主键返回
目录 文章目录 自增主键(LAST_INSERT_ID()) 非自增主键(UUID() ) 自增主键(LAST_INSERT_ID()) 在映射关系文件中配置 <!--插入用户--> &l ...
随机推荐
- python中使用redis发布订阅者模型
redis发布订阅者模型: Redis提供了发布订阅功能,可以用于消息的传输,Redis的发布订阅机制包括三个部分,发布者,订阅者和Channel.发布者和订阅者都是Redis客户端,Channel则 ...
- php框架之phalcon
1.开发助手 1) 下载 git clone https://github.com/phalcon/cphalcon.git git clone https://github.com/phalcon/ ...
- Flutter内置ICON
由于有时打不开flutter的icon官网 https://material.io/tools/icons/?style=baseline 截图存下icon 如果看不清 Ctrl + 恢复Ctr ...
- 迁移git
转自:https://www.darrenfang.com/2016/03/transferring-a-repository/ 因为更换服务器,需要将原来的 git 项目迁移到新的服务器上,需要保留 ...
- nginx配置反向代理CAS单点登录应用
新增如下配置即可: location /cas { proxy_pass http://172.16.20.155:8080/cas; proxy_redirect default; proxy_re ...
- 上板子在线抓波发现app_rdy一直为低
现象 使用Xilinx的MIG IP测试外挂DDR3的读写发现一段很短的时间后app_rdy恒为低,并且最后一个读出的数据全是F. (1)不读写数据,app_rdy正常为高,MIG IP初始化信号为高 ...
- 《Exception团队》第一次作业:团队亮相
一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 深入了解软件思想,强化编程技术 二.正文 1. ...
- Exp1 PC平台逆向破解
本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShell,会返 ...
- I2C(四)linux3.4(写代码)
title: I2C(四)linux3.4(写代码) date: 2019/1/29 17:18:42 toc: true --- I2C(四)linux3.4(写代码) 老师的参考代码 https: ...
- 【Unity游戏开发】你真的了解UGUI中的IPointerClickHandler吗?
一.引子 马三在最近的开发工作中遇到了一个比较有意思的bug:“TableViewCell上面的某些自定义UI组件不能响应点击事件,并且它的父容器TableView也不能响应点击事件,但是TableV ...