In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the role of selectKey tags". In daily operation, I believe that many people have doubts about the role of selectKey tags. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "what is the role of selectKey tags?" Next, please follow the editor to study!
1. Why use selectKey
Database primary keys include self-increment and non-self-increment. Sometimes adding a piece of data not only needs to know the success, but may also need this new primary key for the logic behind. At this time, querying the database will be a bit time-consuming and time-consuming. We can use selectKey to help us get the new primary key.
two。 Specific implementation of demo
The easiest steps to query a database
2.1 controller
@ Controller
Public class SelectKeyController {
@ Autowired
SelectKeyServiceImpl selectKeyService
Public Integer String () {
Goods goods = new Goods ()
Goods.setAmount ("100")
Goods.setGname ("braised pork")
Goods.setMid ("666666")
Goods.setPrice ("25")
Int insert = selectKeyService.insert (goods)
System.out.println ("number of successful execution:" + insert)
System.out.println (goods.getId ())
Return goods.getId ()
}
}
2.2 service
@ Service
Public class SelectKeyServiceImpl implements SelectKeyService {
@ Autowired
SelectKeyMapper selectKeyMapper
@ Override
Public int insert (Goods goods) {
Int insert = selectKeyMapper.insert (goods)
Return insert
}
}
2.3 mapper
Public interface SelectKeyMapper {
Int insert (Goods goods)
}
2.4 entity classes (written according to your own database tables)
@ Data
Public class Goods {
/ / self-increasing primary key
Private Integer id
Private String mid
Private String gname
Private String price
Private String amount
Private String imageName
}
3.mapper.xml file
SELECT LAST_INSERT_ID ()
INSERT INTO Goods (MID,GNAME,PRICE,AMOUNT,imageName)
VALUES (# {mid}, # {gname}, # {price}, # {amount}, # {imageName})
SelectKey will put the result of SELECT LASTINSERTID () into the primary key of the incoming model. The attribute name of the primary key in the model corresponding to keyProperty, here is the id in Goods, because it corresponds to the primary key of the database order AFTER means that SELECT LASTINSERTID () executes after insert execution, and BEFORE means that SELECT LASTINSERTID () executes before insert execution, so you can't get the primary key. This is suitable for the type of primary key that is not self-increasing.
At this point, the study of "what is the role of selectKey tags" 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.