900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > mybatis注解方式返回insert的id

mybatis注解方式返回insert的id

时间:2023-06-19 00:28:17

相关推荐

mybatis注解方式返回insert的id

以前用xml配置的方式久了,突然有一天公司有个项目用的是注解方式,网上找了很多无法满足我需求,于是....还是少废话,上代码吧

controller

/*** 插入并返回刚插入的记录的id例子,实际使用中下面的方法void可以改为返回i和object.getId,以便客户端判断是否插入成功并且返回新对象的id*/@PostMapping("/insertObject")public String insertObject() {WhitsInformation whitsInformation = new WhitsInformation();whitsInformation.setComName("testname");whitsInformation.setAddress("testaddress");int i = demoService.insertObject(whitsInformation);//i表示insert一条记录成功System.out.println(i);System.out.println("whitsInformation2.getId()=="+whitsInformation.getId());return "newUUID="+whitsInformation.getId();}

service省略吧,你懂的......

mapper如下,具体不解释

@Insert("INSERT INTO yytest(id,comName,address) VALUES(#{wi.id},#{Name},#{wi.address})")@SelectKey(statement="select replace(UUID(),'-','') as id", keyProperty="wi.id", before=true, statementType=StatementType.STATEMENT,resultType=String.class) public int insertObject(@Param("wi") WhitsInformation whitsInformation);

具体可以根据需要返回自己想要的,我是返回UUID

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。