JavaEE_Mybatis 获取插入后自增主键的值


在使用SSM 框架编写业务代码的时候,我们有时候有这样的需求,需要获取到新增数据项的自增id .


这时候可以通过以下的方式进行获取:



方式一:

<insert id="insert" parameterType="com._180.test.bean.UserTesta">
    insert into user_testa (name)
    values (#{name,jdbcType=VARCHAR})
    <selectKey keyProperty="id" resultType="int" order="AFTER">
      SELECT LAST_INSERT_ID();
    </selectKey>
  </insert>

     使用<selectKey> 标签,就会在数据库自动生成 id 之后,将id 的值返回给 Java 程序中的对象,那么product 实例中的id 值就会被正确设置。

     SELECT LAST_INSERT_ID() 这一语法,根据使用数据库类型的不同,有可能不同,本例中的语句仅适用与MySQL




方式二:

  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com._180.test.bean.UserTesta" useGeneratedKeys="true">
    insert into user_testa
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="name != null">
        name,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>


    增加了   useGeneratedKeys=”true” ,这一设置指定了 “id” 属性将会由数据库来自动生成,keyProperty ="id" 指定 Product 类中的 id 属性,keyColumn="id" 则指定了Product 表中的列名 id




JavaBean

package com._180.test.bean;

public class UserTesta {
    private Integer id;

    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }
}


接口定义:

package com._180.test.mapper;


import com._180.test.bean.UserTesta;

public interface UserTestaMapper {

    int deleteByPrimaryKey(Integer id);

    int insert(UserTesta record);

    int insertSelective(UserTesta record);

    UserTesta selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(UserTesta record);

    int updateByPrimaryKey(UserTesta record);
}


测试类:

package com._180.test;

import com._180.test.bean.UserTesta;
import com._180.test.mapper.UserTestaMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.InputStream;

/**
 * Created by szh on 2017/11/20.
 */
public class UserTestaMapperTest {

    private ApplicationContext applicationContext;
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void setup() throws Exception {
        String resource = "mybatis/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
    }

    @Test
    public void testInsert() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserTestaMapper userTestaMapper = sqlSession.getMapper(UserTestaMapper.class);

        UserTesta usera = new UserTesta();
        usera.setName("hellok");

        userTestaMapper.insert(usera);
        sqlSession.commit();

        System.out.println("id : " + usera.getId());
    }

    @Test
    public void testInsertSelective() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserTestaMapper userTestaMapper = sqlSession.getMapper(UserTestaMapper.class);

        UserTesta usera = new UserTesta();
        usera.setName("hello");

        userTestaMapper.insertSelective(usera);
        sqlSession.commit();

        System.out.println("id : " + usera.getId());
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值