通用mapper根据条件修改,查询Example的实例函数以及例子
条件:
Example example =new Example(User.class);
Criteria criteria = example.createCriteria();
注意:
criteria.andEqualTo(property,value);
property 对应的是实体类中的属性字段
1,根据条件查询
/**
*根据名称,id,ntype 等获取其对应节点总数
*用于判断名称是否重复
*/
public int getCountByNameAndUId(String name,Long id, Long userId){
Example example =new Example(User.class);
Criteria criteria =example.createCriteria();
criteria.andEqualTo("name",name);
criteria.andNotEqualTo("id",id);
criteria.andEqualTo("userId",uid);
int count =categoryMapper.selectCountByExample(example);
}
等效于:
select count(0) from user where name =#{name} and id != #{id} and uid !=#{uid}
2, and or 查询
public Example test(list<Long>ids ,String name, String des){
Example example =new Example(User.getClass());
//where 条件
Criteria criteria =example.createCriteria();
Criteria criteria1 =example.createCriteria();
criteria.andIn("id",ids);
criteria1.orLike("des", "%"+des+"%");
critera1.orLike("name","%" +name+"%");
example.and(criteria1);
return example;
}
等效于
where id in (#{ids}) and (name like concat( '%', #{name}, '%' ) or des like concat('%', #{des}, '%'))
3,根据条件修改
Example example =new Example(User.getClass());
//where 条件
Criteria criteria =example.createCriteria();
criteria.andEqualTo("name",name);
userMapper.updateByExampleSelective(user,example);
等效于:
Update user set ... where name =#{name}
参照:
MyBatis的Mapper接口以及Example的实例函数及详解(http://blog.csdn.net/biandous/article/details/65630783)
通用mapper
网友评论