In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to understand mybatis generated files, many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Explanation of the generation file of mybatis reverse engineering I. method analysis in mapper interface
Functions and methods in mapper Interface
Method function description int countByExample (UserExample example) thorws SQLException by conditional count int deleteByPrimaryKey (Integer id) thorws SQLException press primary key delete int deleteByExample (UserExample example) thorws SQLException conditional query String/Integer insert (User record) thorws SQLException insertion data (return value is ID) User selectByPrimaryKey (Integer id) thorws SQLException press primary key query ListselectByExample (UserExample example) thorws SQLException conditional query ListselectByExampleWithBLOGs (UserExample example) thorws SQLException conditional query (including BLOB field) It occurs only if the field type in the data table is binary. Int updateByPrimaryKey (User record) thorws SQLException updates int updateByPrimaryKeySelective (User record) thorws SQLException according to primary key update fields whose value is not null int updateByExample (User record, UserExample example) thorws SQLException update int updateByExampleSelective (User record, UserExample example) thorws SQLException conditionally update fields whose values are not null II. Example instance parsing
In the reverse engineering of mybatis, instances and their corresponding example,example are generated to add conditions, which is equivalent to the later part of where.
XxxExample example = new xxxExample ()
Criteria criteria = new Example () .createCriteria ()
The method explains example.setOrderByClause ("field name ASC"), adds ascending order arrangement condition, DESC is descending order example.setDistinct (false) to remove repetition, Boolean type, true is selected non-repetitive record. Criteria.andXxxIsNull add field xxx is null conditional criteria.andXxxIsNotNull add field xxx is not null conditional criteria.andXxxEqualTo (value) add xxx field is equal to value conditional criteria.andXxxNotEqualTo (value) add xxx field is not equal to value conditional criteria.andXxxGreaterThan (value) add xxx field greater than value conditional criteria.andXxxGreaterThanOrEqualTo (value) add xxx field greater than or equal to value conditional criteria.andXxxLessThan (value) add xxx field less than value conditional criteria.andXxxLessThanOrEqualTo (value) add xxx word Field less than or equal to value condition criteria.andXxxIn (List) add xxx field value in List condition criteria.andXxxNotIn (List) add xxx field value not in List condition criteria.andXxxLike ("%" + value+ "%") add fuzzy query condition criteria.andXxxNotLike ("%" + value+ "%") with xxx field value of value ("%" + value+ "%") add fuzzy query condition criteria.andXxxBetween (value1) whose xxx field value is not value Value2) add xxx field value between value1 and value2 condition criteria.andXxxNotBetween (value1,value2) add xxx field value is not between value1 and value2 condition 3, application example 1. Query
① selectByPrimaryKey ()
User user = XxxMapper.selectByPrimaryKey; / / equivalent to select * from user where id = 100
② selectByExample () and selectByExampleWithBLOGs ()
UserExample example = new UserExample (); Criteria criteria = example.createCriteria (); criteria.andUsernameEqualTo ("wyw"); criteria.andUsernameIsNull (); example.setOrderByClause ("username asc,email desc"); Listlist = XxxMapper.selectByExample (example); / / equivalent to: select * from user where username = 'wyw' and username is null order by username asc,email desc
Note: the method in the inner class Criteria,Criteria that contains a static in the file XxxExample.Java generated by iBator reverse engineering is to define the query condition after the SQL statement where.
two。 Insert data
① insert ()
User user = new User (); user.setId ("dsfgsdfgdsfgds"); user.setUsername ("admin"); user.setPassword ("admin") user.setEmail ("wyw@163.com"); XxxMapper.insert (user); / / equivalent to: insert into user (ID,username,password,email) values ('dsfgsdfgdsfgds','admin','admin','wyw@126.com'); 3. Update data
① updateByPrimaryKey ()
User user = new User (); user.setId ("dsfgsdfgdsfgds"); user.setUsername ("wyw"); user.setPassword ("wyw"); user.setEmail ("wyw@163.com"); XxxMapper.updateByPrimaryKey (user); / / equivalent to: update user set username='wyw', password='wyw', email='wyw@163.com' where id='dsfgsdfgdsfgds'
② updateByPrimaryKeySelective ()
User user = new User (); user.setId ("dsfgsdfgdsfgds"); user.setPassword ("wyw"); XxxMapper.updateByPrimaryKey (user); / / equivalent to: update user set password='wyw' where id='dsfgsdfgdsfgds'
③ updateByExample () and updateByExampleSelective ()
UserExample example = new UserExample (); Criteria criteria = example.createCriteria (); criteria.andUsernameEqualTo ("admin"); User user = new User (); user.setPassword ("wyw"); XxxMapper.updateByPrimaryKeySelective (user,example); / / equivalent to: update user set password='wyw' where username='admin'
UpdateByExample () updates all fields, including those whose fields are null. It is recommended to use updateByExampleSelective () to update the fields you want to update.
4. Delete data
① deleteByPrimaryKey ()
XxxMapper.deleteByPrimaryKey (1); / / equivalent to: delete from user where id=1
② deleteByExample ()
UserExample example = new UserExample (); Criteria criteria = example.createCriteria (); criteria.andUsernameEqualTo ("admin"); XxxMapper.deleteByExample (example); / / equivalent to: delete from user where username='admin'
Quantity
① countByExample ()
UserExample example = new UserExample (); Criteria criteria = example.createCriteria (); criteria.andUsernameEqualTo ("wyw"); int count = XxxMapper.countByExample (example); / / equivalent to: select count (*) from user where username='wyw' after reading the above, do you have any further understanding of how to understand the mybatis generation file? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.