欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MyBatis****生成的Mapper接口和Example的讲解与用法

程序员文章站 2022-03-20 10:15:50
...

一、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;

结果:

MyBatis****生成的Mapper接口和Example的讲解与用法

2.查询数据:selectByExample和 selectByPrimaryKey

selectByPrimaryKey:

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

相当于SQL:
select * from employee where id = 1;

MyBatis****生成的Mapper接口和Example的讲解与用法
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;

MyBatis****生成的Mapper接口和Example的讲解与用法

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) 

MyBatis****生成的Mapper接口和Example的讲解与用法
insertSelective:

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

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

MyBatis****生成的Mapper接口和Example的讲解与用法

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" )

MyBatis****生成的Mapper接口和Example的讲解与用法
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” ) 

MyBatis****生成的Mapper接口和Example的讲解与用法
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

MyBatis****生成的Mapper接口和Example的讲解与用法

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 );

MyBatis****生成的Mapper接口和Example的讲解与用法
deleteByPrimaryKey:

mapper.deleteByPrimaryKey(7);

相当于SQL:
delete from employee where id = ? 

MyBatis****生成的Mapper接口和Example的讲解与用法

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