Example解析

本文总结自:https://github.com/abel533/Mapper/wiki/6.example,旨在提供一些基本概念上的解释

Example类用于条件查询,以代替冗长的"select from xxxxxx"

Example可分为两类

1、MBG生成的Example

例如:

CountryExample example = new CountryExample();
example.createCriteria().andCountrynameLike("A%"); //无需设置属性名,此处为给名为CountryName的属性设值
example.or().andIdGreaterThan(100);
example.setDistinct(true);
int count = mapper.deleteByExample(example);//这是一个Example方法

备注:criteria相当于条件查询中的"where xxx"

MBG提供的Example与model中的类的类名对应

2、通用Mapper提供的通用Example

通用Mapper 提供的一个类,这个类和 MBG 生成的相比,需要自己设置属性名。这个类还额外提供了更多的方法。

例如:

Example example = new Example(Country.class); //提供类名
example.setForUpdate(true);
example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
example.or().andLessThan("id", 41);
List<Country> countries = mapper.selectByExample(example);

-------------------------------------------------------------------------------------------------------------------------------

Example方法即mapper如何使用example的方法

方法有许多,有两种类型的定义方式:

List<T> selectByExample(Object example);

int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);

所有Example方法中的example类型都是Object类型,这是因为通用 Mapper 支持所有符合Example结构的参数。

例如通过MBG生成的CountryExample、UserExample类。还有通用Mapper中提供的通用Example,以及支持Java8方法引用的Weekend类型。

猜你喜欢

转载自www.cnblogs.com/yanze/p/10647718.html
今日推荐