Mybatis Mapper interface and Example instance function and SQL statement correspondence
First, the method analysis in the mapper interface
Functions and methods in the mapper interface
method | Function Description |
---|---|
int countByExample(UserExample example) thorws SQLException | Count by condition |
int deleteByPrimaryKey(Integer id) thorws SQLException | Press the primary key to delete |
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 | Press the primary key to query |
ListselectByExample(UserExample example) thorws SQLException | Query by condition |
ListselectByExampleWithBLOGs(UserExample example) thorws SQLException | Query by condition (including BLOB field). It will only be generated if the field type in the data table is binary. |
int updateByPrimaryKey(User record) thorws SQLException | Press the primary key to update |
int updateByPrimaryKeySelective(User record) thorws SQLException | Press the primary key to update the field whose value is not null |
int updateByExample(User record, UserExample example) thorws SQLException | Conditional update |
int updateByExampleSelective(User record, UserExample example) thorws SQLException | Conditionally update fields whose value is not null |
Two, example instance analysis
In 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 | Explanation |
---|---|
example.setOrderByClause ("Field name ASC"); | Add ascending order condition, DESC is descending order |
example.setDistinct(false) | Remove duplicates, boolean type, true is to select non-duplicate records. |
criteria.andXxxIsNull | Add the condition that field xxx is null |
criteria.andXxxIsNotNull | Add the condition that 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 xxx field greater than or equal to value condition |
criteria.andXxxLessThan(value) | Add xxx field less than value condition |
criteria.andXxxLessThanOrEqualTo(value) | Add xxx field less than or equal to value condition |
criteria.andXxxIn(List<?>) | Add xxx field value in List <? > Conditions |
criteria.andXxxNotIn(List<?>) | Add xxx field value not in List <? > Conditions |
criteria.andXxxLike(“%”+value+”%”) | 添加xxx字段值为value的模糊查询条件 |
criteria.andXxxNotLike(“%”+value+”%”) | 添加xxx字段值不为value的模糊查询条件 |
criteria.andXxxBetween(value1,value2) | 添加xxx字段值在value1和value2之间条件 |
criteria.andXxxNotBetween(value1,value2) | 添加xxx字段值不在value1和value2之间条件 |
三、应用举例
1.查询
① selectByPrimaryKey()
User user = XxxMapper.selectByPrimaryKey(100); //相当于select * from user where id = 1001
② 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 desc1234567
注:在iBator逆向工程生成的文件XxxExample.java中包含一个static的内部类Criteria,Criteria中的方法是定义SQL 语句where后的查询条件。
2.插入数据
①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]');1234567
3.更新数据
①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'1234567
②updateByPrimaryKeySelective()
User user = new User();
user.setId("dsfgsdfgdsfgds");
user.setPassword("wyw");
XxxMapper.updateByPrimaryKey(user);
//相当于:update user set password='wyw' where id='dsfgsdfgdsfgds'12345
③ 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'1234567
updateByExample()更新所有的字段,包括字段为null的也更新,建议使用 updateByExampleSelective()更新想更新的字段
4.删除数据
①deleteByPrimaryKey()
XxxMapper.deleteByPrimaryKey(1); //相当于:delete from user where id=11
②deleteByExample()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
XxxMapper.deleteByExample(example);
//相当于:delete from user where username='admin'12345
5.查询数据数量
①countByExample()
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
int count = XxxMapper.countByExample(example);
//相当于:select count(*) from user where username='wyw'