In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to achieve tk.Mybatis insert data acquisition Id". In daily operation, I believe that many people have doubts about how to achieve tk.Mybatis insertion data acquisition Id. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to achieve tk.Mybatis insertion data acquisition Id". Next, please follow the editor to study!
1. Problem description
Several code writing methods will have different ID return values, which we will analyze one by one below.
2. Problem analysis.
First of all, is inserted and written. The source code is as follows:
SysUser .java
/ * * user Management (SysUser) entity Class * * @ author Chen * @ since 2020-05-06 14:16:48 * / @ Data@ApiModel ("user Management") public class SysUser implements Serializable {private static final long serialVersionUID = 993569543176442301L; / * * No. * / @ Id private Long id; / * * user name * / private String name / * * nickname * / private String nickName; / * avatar * / private String avatar; / * password * / private String password; / * encryption salt * / private String salt; / * * email * / private String email; / * Mobile phone number * / private String mobile / * status 0: disabled 1: normal * / private Object status; / * * Institution ID * / private Long deptId; / * * creator * / private String createBy; / * creation time * / private Date createTime; / * Updater * / private String lastUpdateBy / * Update time * / private Date lastUpdateTime; / * whether to delete-1: deleted 0: normal * / private Object delFlag;}
Test class
@ Test void insert () {SysUser sysUser = new SysUser (); sysUser.setName ("zhangsan3"); sysUser.setNickName ("Zhang San3"); SysUser insert = sysUserService.insert (sysUser); System.out.println (insert);}
Result output:
SysUser (id=null, name=zhangsan3, nickName= Zhang San3, avatar=null, password=null, salt=null, email=null, mobile=null, status=null, deptId=null, createBy=null, createTime=null, lastUpdateBy=null, lastUpdateTime=null, delFlag=null)
Yes, what we got is id=null, which obviously doesn't meet our expectations. So modify the code. Change only the test class. For example, enter id=0. The source code is as follows
@ Test void insert () {SysUser sysUser = new SysUser (); sysUser.setId (0L); sysUser.setName ("zhangsan6"); sysUser.setNickName ("Zhang San6"); SysUser insert = sysUserService.insert (sysUser); System.out.println (insert);}
Run the program again, and the console output is as follows:
SysUser (id=0, name=zhangsan4, nickName= Zhang San4, avatar=null, password=null, salt=null, email=null, mobile=null, status=null, deptId=null, createBy=null, createTime=null, lastUpdateBy=null, lastUpdateTime=null, delFlag=null)
Well, it does seem to output id=0, which is in line with our expectations, but is it really so? Let's see what the database restrictions look like.
Yes, our real id is 38. There's a problem here. Just add @ GeneratedValue (strategy = GenerationType.IDENTITY) to the entity class for reference. So we did it. Modify the entity class as follows:
/ * * user Management (SysUser) entity Class * * @ author Chen * @ since 2020-05-06 14:16:48 * / @ Data@ApiModel ("user Management") public class SysUser implements Serializable {private static final long serialVersionUID = 993569543176442301L; / * * No. * / @ Id @ GeneratedValue (strategy = GenerationType.IDENTITY) private Long id; / * * user name * / private String name / * * nickname * / private String nickName; / * avatar * / private String avatar; / * password * / private String password; / * encryption salt * / private String salt; / * * email * / private String email; / * Mobile phone number * / private String mobile / * status 0: disabled 1: normal * / private Object status; / * * Institution ID * / private Long deptId; / * * creator * / private String createBy; / * creation time * / private Date createTime; / * Updater * / private String lastUpdateBy / * Update time * / private Date lastUpdateTime; / * whether to delete-1: deleted 0: normal * / private Object delFlag;}
It's mainly changed here. OK, continue running the program, and the result is as follows:
SysUser (id=0, name=zhangsan5, nickName= Zhang San5, avatar=null, password=null, salt=null, email=null, mobile=null, status=null, deptId=null, createBy=null, createTime=null, lastUpdateBy=null, lastUpdateTime=null, delFlag=null)
Id here is still zero, what the heck. And look at the database.
It's obviously not zero. So you can't set the properties of id here. Modify the test class. The source code is as follows
@ Test void insert () {SysUser sysUser = new SysUser (); sysUser.setName ("zhangsan6"); sysUser.setNickName ("Zhang San6"); SysUser insert = sysUserService.insert (sysUser); System.out.println (insert);}
Continue to run the program, and the results are as follows:
SysUser (id=40, name=zhangsan6, nickName= Zhang San6, avatar=null, password=null, salt=null, email=null, mobile=null, status=null, deptId=null, createBy=null, createTime=null, lastUpdateBy=null, lastUpdateTime=null, delFlag=null)
Id=40 this time, check the database.
It's obviously a match.
At this point, the study on "how to achieve tk.Mybatis insert data acquisition Id" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.