在使用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>
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());
}
}