MyBatis逆向工程生成的Mapper接口和Example的講解與用法

一、mapper接口中的方法

方法 功能說明
long countByExample(EmployeeExample example) ; 按條件計數
int deleteByExample(EmployeeExample example); 按條件刪除
int deleteByPrimaryKey(Integer id); 按主鍵刪除
int insert(Employee record); 插入數據
int insertSelective(Employee record); 按條件插入數據
List selectByExample(EmployeeExample example); 按條件查詢
Employee selectByPrimaryKey(Integer id); 按主鍵查詢
int updateByExampleSelective(@Param(“record”) Employee record, @Param(“example”) EmployeeExample example); 按條件更新值不爲null的字段
int updateByExample(@Param(“record”) Employee record, @Param(“example”) EmployeeExample example); 按條件更新
int updateByPrimaryKeySelective(Employee record); 按主鍵更新值不爲null的字段
int updateByPrimaryKey(Employee record); 按主鍵更新

二、example實例

mybatis的逆向工程中會生成實例及實例對應的example,example/ XXXExample就是封裝查詢條件,相當於where後面部分
XxxExample.java中包含一個static的內部類Criteria,Criteria就是拼裝查詢條件
xxxExample example = new xxxExample();
Criteria criteria = new Example().createCriteria();

項目 Value
example.setOrderByClause(“字段名 ASC”) 添加升序排列條件,DESC爲降序
example.setDistinct(false) 去除重複,boolean型,true爲選擇不重複的記錄
criteria.andXxxIsNull 添加字段xxx爲null的條件
criteria.andXxxIsNotNull 添加字段xxx不爲null的條件
criteria.andXxxEqualTo(value) 添加xxx字段等於value條件
criteria.andXxxNotEqualTo(value) 添加xxx字段不等於value條件
criteria.andXxxGreaterThan(value) 添加xxx字段大於value條件
criteria.andXxxGreaterThanOrEqualTo(value) 添加xxx字段大於等於value條件
criteria.andXxxLessThan(value) 添加xxx字段小於value條件
criteria.andXxxLessThanOrEqualTo(value) 添加xxx字段小於等於value條件
criteria.andXxxIn(List<?>) 添加xxx字段值在List<?>條件
criteria.andXxxNotIn(List<?>) 添加xxx字段值不在List<?>條件
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.統計數量:countByExample

代碼示例:

// exampple相當於where後面的
EmployeeExample example = new EmployeeExample();
EmployeeExample.Criteria criteria = example.createCriteria();
//添加 username 字段 值爲k 的模糊條件查詢
criteria.andUserNameLike("%k%");
long count = mapper.countByExample(example);

相當於SQL語句:
select count(*) from employee WHERE  user_name like k;

結果:

在這裏插入圖片描述

2.查詢數據:selectByExample和 selectByPrimaryKey

selectByPrimaryKey:

Employee employee = mapper.selectByPrimaryKey(1);
System.out.println(employee);

相當於SQL:
select * from employee where id = 1;

在這裏插入圖片描述
selectByExample:

//exampple相當於where後面的
EmployeeExample example = new EmployeeExample();
EmployeeExample.Criteria criteria = example.createCriteria();
//添加username 字段 值爲g的模糊查詢
criteria.andUserNameLike("%k%");
// 添加 age字段 大於16的條件
criteria.andAgeGreaterThan(16);
//通過age字段進行排序
example.setOrderByClause("age");

List<Employee> list = mapper.selectByExample(example);
for (Employee employee: list){
    System.out.println(employee);
}

相當於SQL:
select *from employee WHERE ( user_name like ? and age > ? ) order by age;

在這裏插入圖片描述

3.插入數據 :insert,insertSelective

Employee employee = new Employee();
employee.setUserName("Mary");
employee.setAge(22);
employee.setEmail("mary.163.com");
employee.setDeptId(1);
mapper.insert(employee);

相當於SQL:
insert into employee (id, user_name, age, email, dept_id) values (null,"Mary“, 22, "mary.163.com", 1) 

在這裏插入圖片描述
insertSelective:

Employee employee = new Employee();
employee.setUserName("bob");
employee.setAge(25);
mapper.insertSelective(employee);

相當於SQL:
insert into employee ( user_name, age ) values ( “bob”, 25 )

在這裏插入圖片描述

4.更新數據: updateByExample和updateByExampleSelective,updateByPrimaryKey和updateByPrimaryKeySelective

updateByExample:

//exampple相當於where後面的
 EmployeeExample example = new EmployeeExample();
 EmployeeExample.Criteria criteria = example.createCriteria();
 criteria.andUserNameEqualTo("kong");
 Employee employee = new Employee();
 employee.setId(3);
 employee.setUserName("Linda");
 employee.setEmail("Linda.162.com");
 employee.setAge(25);
 employee.setDeptId(2);
 mapper.updateByExample(employee,example);

相當於SQL:
update employee set id = 3, user_name = "Linda", age = 25, email = "Linda.162.com", dept_id = 2 WHERE ( user_name = "kong" )

在這裏插入圖片描述
updateByExampleSelective:

EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
//exampple相當於where後面的
EmployeeExample example = new EmployeeExample();
EmployeeExample.Criteria criteria = example.createCriteria();
criteria.andUserNameEqualTo("Jack");
Employee employee = new Employee();
employee.setEmail("jack.162.com");
employee.setAge(30);
mapper.updateByExampleSelective(employee,example);

相當於SQL:
update employee SET age = 30, email = “jack.162.com” WHERE ( user_name = “Jack” ) 

在這裏插入圖片描述
updateByPrimaryKey:

int updateByPrimaryKey(Employee record);
Employee employee = new Employee();
employee.setId(5);
employee.setUserName("kong");
employee.setAge(16);
employee.setEmail("hk.163.com");
mapper.updateByPrimaryKey(employee);

update employee set user_name = kong, age = 16, email = hk.163.com, dept_id = null where id = 5

updateByPrimaryKeySelective:

int updateByPrimaryKeySelective(Employee record);
Employee employee = new Employee();
employee.setId(6);
employee.setUserName("diligentkong");
mapper.updateByPrimaryKeySelective(employee);

相當於SQL:
update employee SET user_name = diligentkong where id = 6

在這裏插入圖片描述

5.刪除數據:deleteByExample和deleteByPrimaryKey

deleteByExample:

//exampple相當於where後面的
EmployeeExample example = new EmployeeExample();
EmployeeExample.Criteria criteria = example.createCriteria();
//添加 年齡在20-30之間的查詢條件
criteria.andAgeBetween(20,30);
mapper.deleteByExample(example);

相當於SQL:
delete from employee WHERE ( age between 20 and 30 );

在這裏插入圖片描述
deleteByPrimaryKey:

mapper.deleteByPrimaryKey(7);

相當於SQL:
delete from employee where id = ? 

在這裏插入圖片描述

參考文章:https://blog.csdn.net/biandous/article/details/65630783

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章