版权声明:请勿用于任何商业用途的文章转载,转载请说明出处!
1.dao层接口使用Select注解写SQL:
重点:@Param(“ew”) Wrapper参数是必须,因为${ew.customSqlSegment} 底层其实就是where 条件,所以为了保证Wrapper不为空,service层代码中的Wrapper至少需要有一个条件:1 = 1
@Override
@Select("select a.code as code , b.name as name , b.barcode as barcode , a.ware_code as wareCode , c.name as wareName , a.qty as qty , a.oprice as oprice , a.total as total , " +
" a.id as id , a.create_by as createBy , a.create_date as createDate , a.update_by as updateBy , a.update_date as updateDate , a.status as status , a.remarks as remarks " +
"from sku_stock a , goods b , warehouse c " +
"${ew.customSqlSegment} and a.code = b.code and a.ware_code = c.code")
IPage<SkuStock> selectPage(IPage<SkuStock> page, @Param("ew")Wrapper<SkuStock> queryWrapper);
2.service层代码示例:
service父类封装的findPage方法:
/**
* 封装findPage
* @param entity
* @param search Map中的key:";"为保留关键字,拆分数组,仅支持最大长度2的数组,
* 下标0:QueryWrapper查询条件中的列名(支持多表关联查询的表别名 + 列名方式,需要dao层接口支持)
* 下标1: QueryWrapper中不同的查询条件,eq:等于,ge:大于等..... todo:请自行完善Mybatis eq、ne、gt、lt、ge、le等
* Map中的value:QueryWrapper需要查询的值
* @param args QueryWrapper中order by 排序数组
* @return
*/
public IPage<T> findPage(T entity , Map<String , Object> search , String... args){
long current = 1L;
long size = 10L;
if (EmptyUtil.isNoEmpty(ReflexUtil.getFieldValue(entity , "page")) && (long) ReflexUtil.getFieldValue(entity , "page") != 0){
current = (long) ReflexUtil.getFieldValue(entity , "page");
}
if (EmptyUtil.isNoEmpty(ReflexUtil.getFieldValue(entity , "limit")) && (long) ReflexUtil.getFieldValue(entity , "limit") != 0){
size = (long) ReflexUtil.getFieldValue(entity , "limit");
}
QueryWrapper<T> queryWrapper;
if (EmptyUtil.isNoEmpty(search)){
queryWrapper = new QueryWrapper<>();
for (Map.Entry<String , Object> entry:search.entrySet()
) {