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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值