栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MyBatis Plus插入后返回ID主键(自增)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MyBatis Plus插入后返回ID主键(自增)

在MyBatisPlus中添加注解信息即可

//实体类(此处需要添加对应的MyBatisPlus主键信息)
public class Notice{
    private int noticeId;
    private String release_date;
    private String content;
}
//方案一:自定义SQL(自增主键)
 @Insert("insert into notice(release_date,content)values(#{releaseDate},#{content})")
//keyProperty对象的属性,keyColumn数据库表主键
 @Options(useGeneratedKeys = true,keyProperty = "noticeId",keyColumn = "notice_id")
 int addNotice(Notice notice);
//方案二:MyBatisPlus的insert方法(自增主键)
 @TableId(value = "notice_id",type=IdType.AUTO);//需要在实体类的主键属性上添加此句
 private int noticeId;

测试

@Test
public void test2(){
    Notice notice1 = new Notice();
    notice1.setContent("我是测试数据3");
    notice1.setReleaseDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    //自定义SQL
    int notice = activityMapper.addNotice(notice1);
    System.out.println(notice+"------------>"+notice1);
}
@Test
public void test3(){
	Notice notice = new Notice();
    notice.setContent("封装的id");
    notice.setReleaseDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    int i = noticeMapper.insert(notice);
    System.out.println(i+"------->"+notice);
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/925031.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号