以前用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