说明:
1、Example类仅支持单表查询,对于联表查询,尚无方法,需要自己在mybatis里面写接口和xml,根据具体的sql语句去查询。
2、泛型里面的类就是被查表的具体实体类,且new Example(实体类.class);
3、dao层必须继承基础Mapper<被查实体类>,才能用里面的方法,也可以自己拓展方法。
举例如下:
1)、
@Override
public List<LibraryCatalog> list() {
Example example = new Example(LibraryCatalog.class);
example.createCriteria().andEqualTo("useStatus", true);
example.orderBy("orderNum");
List<LibraryCatalog> libraryCatalogs = libraryCatalogMapper.selectByExample(example);
return libraryCatalogs;
}
2)、
@Override
public LibraryProductBrand findOne(String id) {
Example example = new Example(LibraryProductBrand.class);
example.createCriteria().andEqualTo("id", id)
.andEqualTo("useStatus", false);
return libraryProductBrandMapper.selectOneByExample(example);
}
3)、
@Override
public PageInfo<LibraryProductBrand> list(Integer pageNum, Integer pageSize, LibraryProductBrand libraryProductBrand) {
//查询所有未被删除的商品品牌
if (pageNum != null && pageSize != null) {
PageHelper.startPage(pageNum, pageSize);
}
Example example = PageDataConvert.getWhere(libraryProductBrand);
example.and().andEqualTo("useStatus", false);
example.orderBy("createTime").desc();
return new PageInfo<>(libraryProductBrandMapper.selectByExample(example));
}
本文详细介绍了MyBatis中Example类的高级使用技巧,包括单表查询、条件筛选及排序方法,同时展示了如何结合PageHelper实现分页查询。通过具体代码示例,帮助读者理解如何在实际项目中灵活运用Example进行高效数据操作。
561

被折叠的 条评论
为什么被折叠?



