First, the method analysis in the mapper interface
Functions and methods in mapper interface
method | Function Description |
---|---|
int countByExample(UserExample example) thorws SQLException | count by condition |
int deleteByPrimaryKey(Integer id) thorws SQLException | delete by primary key |
int deleteByExample(UserExample example) thorws SQLException | Query by condition |
String/Integer insert(User record) thorws SQLException | Insert data (return value is ID) |
User selectByPrimaryKey(Integer id) thorws SQLException | Query by primary key |
ListselectByExample(UserExample example) thorws SQLException | Query by condition |
ListselectByExampleWithBLOGs(UserExample example) thorws SQLException | Query by criteria (including BLOB fields). It will only be generated if the field type in the data table is binary. |
int updateByPrimaryKey(User record) thorws SQLException | Update by primary key |
int updateByPrimaryKeySelective(User record) thorws SQLException | Update fields whose value is not null by primary key |
int updateByExample(User record, UserExample example) thorws SQLException | Update by condition |
int updateByExampleSelective(User record, UserExample example) thorws SQLException | Conditionally update fields whose value is not null |
Second, the example instance analysis
In the reverse engineering of mybatis, an instance and an example corresponding to the instance will be generated. The example is used to add conditions, which is equivalent to the part behind where
xxxExample example = new xxxExample();
Criteria criteria = new Example().createCriteria();
method | illustrate |
---|---|
example.setOrderByClause("field name ASC"); | Add ascending order condition, DESC is descending order |
example.setDistinct(false) | Remove duplicates, boolean type, true to select non-duplicate records. |
criteria.andXxxIsNull | Add the condition that the field xxx is null |
criteria.andXxxIsNotNull | Add the condition that the field xxx is not null |
criteria.andXxxEqualTo(value) | Add xxx field equal to value condition |
criteria.andXxxNotEqualTo(value) | Add xxx field is not equal to value condition |
criteria.andXxxGreaterThan(value) | Add xxx field greater than value condition |
criteria.andXxxGreaterThanOrEqualTo(value) | Add the condition that the xxx field is greater than or equal to the value |
criteria.andXxxLessThan(value) | Add xxx field less than value condition |
criteria.andXxxLessThanOrEqualTo(value) | Add the condition that the xxx field is less than or equal to the value |
criteria.andXxxIn(List<?>) | Add xxx field value in List<? >Conditions |
criteria.andXxxNotIn(List<?>) | Add xxx field value not in List<? >Conditions |
criteria.andXxxLike(“%”+value+”%”) | Add fuzzy query condition with xxx field value as value |
criteria.andXxxNotLike(“%”+value+”%”) | Add fuzzy query conditions where the value of the xxx field is not value |
criteria.andXxxBetween(value1,value2) | Add the condition that the xxx field value is between value1 and value2 |
criteria.andXxxNotBetween(value1,value2) | Add the condition that the xxx field value is not between value1 and value2 |
3. Application Examples
1. Inquiry
① selectByPrimaryKey()
User user = XxxMapper.selectByPrimaryKey(100); //相当于select * from user where id = 100
- 1
② selectByExample() 和 selectByExampleWithBLOGs()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<?>list = XxxMapper.selectByExample(example);
//相当于:select * from user where username = 'wyw' and username is null order by username asc,email desc
Note: The file XxxExample.java generated by iBator reverse engineering contains a static inner class Criteria. The method in Criteria is to define the query condition after the SQL statement where.
2. Insert data
①insert()
User user = new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("admin");
user.setPassword("admin")
user.setEmail("[email protected]");
XxxMapper.insert(user);
//相当于:insert into user(ID,username,password,email) values ('dsfgsdfgdsfgds','admin','admin','[email protected]');
3. Update data
①updateByPrimaryKey()
User user =new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("wyw");
user.setPassword("wyw");
user.setEmail("[email protected]");
XxxMapper.updateByPrimaryKey(user);
//相当于:update user set username='wyw', password='wyw', email='[email protected]' where id='dsfgsdfgdsfgds'
②updateByPrimaryKeySelective()
User user = new User();
user.setId("dsfgsdfgdsfgds");
user.setPassword("wyw");
XxxMapper.updateByPrimaryKey(user);
//相当于:update user set password='wyw' where id='dsfgsdfgdsfgds'
③ updateByExample() 和 updateByExampleSelective()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
User user = new User();
user.setPassword("wyw");
XxxMapper.updateByPrimaryKeySelective(user,example);
//相当于:update user set password='wyw' where username='admin'
updateByExample() updates all fields, including fields that are null. It is recommended to use updateByExampleSelective() to update the fields you want to update
4. Delete data
①deleteByPrimaryKey()
XxxMapper.deleteByPrimaryKey(1); //相当于:delete from user where id=1
②deleteByExample()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
XxxMapper.deleteByExample(example);
//相当于:delete from user where username='admin'
5. Query data quantity
①countByExample()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
int count = XxxMapper.countByExample(example);
//相当于:select count(*) from user where username='wyw'