In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how mybatis to achieve the end of the new save automatically return to the main key id, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Mybatis automatically returns the primary key id1. Id1 after adding save. Working with scen
The entity class with id is returned automatically after id is null,save in the entity class before the save operation.
@ Override public ChartPagePanel save (ChartPagePanel entity) {UserDetails user = SecurityContextHolder.getUserDetails (); entity.setCreateUser (user.getUsername ()); entity.setLastModifyUser (user.getUsername ()); / / id in entity is null chartPagePanelMapper.save (entity); / / entity / / savePanelManage (entity) with id is returned automatically after save operation; where entity has id savePanelManage (entity); return entity } @ Transactional public void savePanelManage (ChartPagePanel entity) {if (entity.getChartPageManges ()! = null & & entity.getChartPageManges (). Size () > 0) {Map map = new HashMap (); for (int I = 0; I < entity.getChartPageManges (). Size (); iSum +) {int manageId = entity.getChartPageManges (). Get (I). GetId (); map.put ("manageId", manageId) Map.put ("panelId", entity.getId ()); chartPagePanelManageMapper.save (map);}} 2. In principle, Mybatis configures useGeneratedKeys= "true" keyProperty= "id" insert into chart_page_panel (`parent_ id`, `position`, `name`, `create_ time`, `create_ user`, `last_modify_ time`, `last_modify_ user`) values (# {parentId}, # {position}, # {name}, NOW (), # {createUser}, NOW () # {lastModifyUser}) the save method in mybatis or mybatis-plus returns the primary key value 1.mapper.xml
Method:
UseGeneratedKeys= "true" keyProperty= "id" keyColumn= "id"
Explanation:
Define useGeneratedKeys as true in xml and return the value of the primary key id. KeyColumn and keyProperty represent the database record primary key field and the java object member attribute name, respectively.
INSERT INTO adjust_bill_info (external_bill_id, warehouse_code, warehouse_name) VALUES (# {externalBillId}, # {warehouseCode}, # {warehouseName}); in 2.service or dao
Note: the result obtained in this way is the number of rows affected!
If you want to get the primary key id value, you need to get it from the incoming object!
Long id = aTranscationMapper.saveBill (adjustBillInfo); System.out.println ("= number of affected rows saved:" + id+ "saved id value is:" + adjustBillInfo.getId ())
The output shows:
= number of affected rows saved: 1 saved id value is: 191
After the insert of mybatis-plus, return the primary key id, and get the id directly from the passed object!
BizApplicationFormMapper.insert (form)
System.out.println ("=" + form.getId ())
The above is all the contents of the article "how mybatis can automatically return the primary key id after the end of the new save". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.