mybatis3中几个@Provider的使用方式
其中
@SelectProvider
type指定类
method指定方法
参数会在mapper接口的调用中自动传入
下面为构造ListFilter的SQL的一种写法(关键字,排序,分页)
/**
* 根据条件查询所有的知识库
*
* @param listRequest
* @return
*/
@SelectProvider(type = KbaseDirMapper.getAllKbaseListProvider.class, method = "getAllKbaseList")
List<KbaseDir> getAllKbaseList(ListRequest listRequest);
class getAllKbaseListProvider {
public String getAllKbaseList(ListRequest listRequest) {
String sql = "select * from t_kbase_dir where res_type = 0";
// 拼接关键字
if (!CollectionUtils.isEmpty(listRequest.getFilters())) {
for (ListFilter filter : listRequest.getFilters()) {
if (StringUtils.isNotEmpty(filter.getKeywordType()) && filter.getKeyword() != null) {
sql += " and " + filter.getKeywordType() + "='" + filter.getKeyword() + "' ";
}
}
}
// 拼接排序
if (StringUtils.isNotEmpty(listRequest.getOrderBy()) && StringUtils.isNotEmpty(listRequest.getOrder())) {
sql += " order by " + listRequest.getOrderBy() + " " + listRequest.getOrder();
}
// 拼接分页
sql += " limit "+listRequest.getPageNo()+","+listRequest.getPageSize();
return sql;
}
}
查询语句为:
select * from t_kbase_dir where res_type = 0 order by create_date asc limit 1,10
网友评论