Mybatis通用mapper获得insert后的数据实体的自增ID

一、pom(基于Spring-Boot)

<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
<dependency>
	<groupId>tk.mybatis</groupId>
	<artifactId>mapper-spring-boot-starter</artifactId>
	<version>1.1.5</version>
</dependency>

二、实体(xxxEntity)类中设置自增ID属性

@Table(name = "${tablename}")
public class XXXEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
}

三、通用mapper

import tk.mybatis.mapper.common.Mapper;

public interface XXXMapper extends Mapper<XXXEntity> {
	/**
	 * 什么都不用写,增删改查由底层的通用Mapper来为我们实现数据实体的增删改查 
	 * 如果需要写复杂SQL,需结合XML来配合通用mapper
	 */
}

四、demo(insert)测试

int vID = XXXMapper.insert(XXXEntity);
if(vID<0){
	throw new BaseException(ResponseMessage.FAIL,"创建XXX失败");
}

System.out.println(XXXEntity.getId());

五、效果展示

猜你喜欢

转载自blog.csdn.net/Appleyk/article/details/81360738