Java Code Examples for com.baomidou.mybatisplus.core.conditions.query.QueryWrapper#select()

The following examples show how to use com.baomidou.mybatisplus.core.conditions.query.QueryWrapper#select() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlogServiceImpl.java    From mogu_blog_v2 with Apache License 2.0 6 votes vote down vote up
@Override
public IPage<Blog> getBlogPageByLevel(Page<Blog> page, Integer level, Integer useSort) {
    QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(BaseSQLConf.LEVEL, level);
    queryWrapper.eq(BaseSQLConf.STATUS, EStatus.ENABLE);
    queryWrapper.eq(BaseSQLConf.IS_PUBLISH, EPublish.PUBLISH);

    if (useSort == 0) {
        queryWrapper.orderByDesc(BaseSQLConf.CREATE_TIME);
    } else {
        queryWrapper.orderByDesc(BaseSQLConf.SORT);
    }

    //因为首页并不需要显示内容,所以需要排除掉内容字段
    queryWrapper.select(Blog.class, i -> !i.getProperty().equals(SysConf.CONTENT));

    return blogMapper.selectPage(page, queryWrapper);
}
 
Example 2
Source File: BlogServiceImpl.java    From mogu_blog_v2 with Apache License 2.0 6 votes vote down vote up
@Override
public IPage<Blog> getBlogByTime(Long currentPage, Long pageSize) {
    QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
    Page<Blog> page = new Page<>();
    page.setCurrent(currentPage);
    page.setSize(pageSize);
    queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    queryWrapper.eq(BaseSQLConf.IS_PUBLISH, EPublish.PUBLISH);
    queryWrapper.orderByDesc(SQLConf.CREATE_TIME);

    //因为首页并不需要显示内容,所以需要排除掉内容字段
    queryWrapper.select(Blog.class, i -> !i.getProperty().equals(SQLConf.CONTENT));

    IPage<Blog> pageList = blogService.page(page, queryWrapper);
    List<Blog> list = pageList.getRecords();

    list = setBlog(list);
    pageList.setRecords(list);
    return pageList;
}
 
Example 3
Source File: BlogServiceImpl.java    From mogu_blog_v2 with Apache License 2.0 6 votes vote down vote up
@Override
public IPage<Blog> getListByBlogSortUid(String blogSortUid, Long currentPage, Long pageSize) {
    //分页
    Page<Blog> page = new Page<>();
    page.setCurrent(currentPage);
    page.setSize(pageSize);

    QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
    queryWrapper.eq(BaseSQLConf.IS_PUBLISH, EPublish.PUBLISH);
    queryWrapper.eq(SQLConf.BLOG_SORT_UID, blogSortUid);

    //因为首页并不需要显示内容,所以需要排除掉内容字段
    queryWrapper.select(Blog.class, i -> !i.getProperty().equals(SQLConf.CONTENT));
    IPage<Blog> pageList = blogService.page(page, queryWrapper);

    //给博客增加标签和分类
    List<Blog> list = blogService.setTagAndSortByBlogList(pageList.getRecords());
    pageList.setRecords(list);
    return pageList;
}
 
Example 4
Source File: BlogServiceImpl.java    From mogu_blog_v2 with Apache License 2.0 6 votes vote down vote up
@Override
public IPage<Blog> searchBlogByAuthor(String author, Long currentPage, Long pageSize) {
    QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();

    Page<Blog> page = new Page<>();
    page.setCurrent(currentPage);
    page.setSize(pageSize);

    queryWrapper.eq(SQLConf.AUTHOR, author);
    queryWrapper.eq(BaseSQLConf.IS_PUBLISH, EPublish.PUBLISH);
    queryWrapper.eq(BaseSQLConf.STATUS, EStatus.ENABLE);
    queryWrapper.orderByDesc(SQLConf.CREATE_TIME);

    queryWrapper.select(Blog.class, i -> !i.getProperty().equals(SysConf.CONTENT));

    IPage<Blog> pageList = blogService.page(page, queryWrapper);
    List<Blog> list = pageList.getRecords();
    list = blogService.setTagAndSortAndPictureByBlogList(list);
    pageList.setRecords(list);
    return pageList;
}
 
Example 5
Source File: CrudTest.java    From spring-boot-demo with MIT License 5 votes vote down vote up
@Test
public void testSelectMaxId() {
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.select("max(id) as id");
    User user = mapper.selectOne(wrapper);
    System.out.println("maxId=" + user.getId());
    List<User> users = mapper.selectList(Wrappers.<User>lambdaQuery().orderByDesc(User::getId));
    Assert.assertEquals(user.getId().longValue(), users.get(0).getId().longValue());
}
 
Example 6
Source File: TagServiceImpl.java    From mogu_blog_v2 with Apache License 2.0 5 votes vote down vote up
@Override
public String tagSortByCite() {
    // 定义Map   key:tagUid,  value: 引用量
    Map<String, Integer> map = new HashMap<>();

    QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
    tagQueryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    List<Tag> tagList = tagService.list(tagQueryWrapper);
    // 初始化所有标签的引用量
    tagList.forEach(item -> {
        map.put(item.getUid(), 0);
    });

    QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    queryWrapper.eq(SQLConf.IS_PUBLISH, EPublish.PUBLISH);
    // 过滤content字段
    queryWrapper.select(Blog.class, i -> !i.getProperty().equals(SQLConf.CONTENT));
    List<Blog> blogList = blogService.list(queryWrapper);

    blogList.forEach(item -> {
        String tagUids = item.getTagUid();
        List<String> tagUidList = StringUtils.changeStringToString(tagUids, SysConf.FILE_SEGMENTATION);
        for (String tagUid : tagUidList) {
            if (map.get(tagUid) != null) {
                Integer count = map.get(tagUid) + 1;
                map.put(tagUid, count);
            } else {
                map.put(tagUid, 0);
            }
        }
    });

    tagList.forEach(item -> {
        item.setSort(map.get(item.getUid()));
        item.setUpdateTime(new Date());
    });
    tagService.updateBatchById(tagList);
    return ResultUtil.result(SysConf.SUCCESS, MessageConf.OPERATION_SUCCESS);
}
 
Example 7
Source File: BlogServiceImpl.java    From mogu_blog_v2 with Apache License 2.0 5 votes vote down vote up
@Override
public IPage<Blog> searchBlogByTag(String tagUid, Long currentPage, Long pageSize) {
    Tag tag = tagService.getById(tagUid);
    if (tag != null) {
        HttpServletRequest request = RequestHolder.getRequest();
        String ip = IpUtils.getIpAddr(request);
        //从Redis取出数据,判断该用户24小时内,是否点击过该标签
        String jsonResult = redisUtil.get(RedisConf.TAG_CLICK + RedisConf.SEGMENTATION + ip + "#" + tagUid);
        if (StringUtils.isEmpty(jsonResult)) {

            //给标签点击数增加
            int clickCount = tag.getClickCount() + 1;
            tag.setClickCount(clickCount);
            tag.updateById();
            //将该用户点击记录存储到redis中, 24小时后过期
            redisUtil.setEx(RedisConf.TAG_CLICK + RedisConf.SEGMENTATION + ip + RedisConf.WELL_NUMBER + tagUid, clickCount + "",
                    24, TimeUnit.HOURS);
        }
    }
    QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
    Page<Blog> page = new Page<>();
    page.setCurrent(currentPage);
    page.setSize(pageSize);

    queryWrapper.like(SQLConf.TagUid, tagUid);
    queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    queryWrapper.eq(BaseSQLConf.IS_PUBLISH, EPublish.PUBLISH);
    queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
    queryWrapper.select(Blog.class, i -> !i.getProperty().equals(SysConf.CONTENT));
    IPage<Blog> pageList = blogService.page(page, queryWrapper);
    List<Blog> list = pageList.getRecords();
    list = blogService.setTagAndSortAndPictureByBlogList(list);
    pageList.setRecords(list);
    return pageList;
}
 
Example 8
Source File: BlogSortServiceImpl.java    From mogu_blog_v2 with Apache License 2.0 5 votes vote down vote up
@Override
public String blogSortByCite() {
    // 定义Map   key:tagUid,  value: 引用量
    Map<String, Integer> map = new HashMap<>();

    QueryWrapper<BlogSort> blogSortQueryWrapper = new QueryWrapper<>();
    blogSortQueryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    List<BlogSort> blogSortList = blogSortService.list(blogSortQueryWrapper);
    // 初始化所有标签的引用量
    blogSortList.forEach(item -> {
        map.put(item.getUid(), 0);
    });

    QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    queryWrapper.eq(SQLConf.IS_PUBLISH, EPublish.PUBLISH);
    // 过滤content字段
    queryWrapper.select(Blog.class, i -> !i.getProperty().equals(SQLConf.CONTENT));
    List<Blog> blogList = blogService.list(queryWrapper);

    blogList.forEach(item -> {
        String blogSortUid = item.getBlogSortUid();
        if (map.get(blogSortUid) != null) {
            Integer count = map.get(blogSortUid) + 1;
            map.put(blogSortUid, count);
        } else {
            map.put(blogSortUid, 0);
        }
    });

    blogSortList.forEach(item -> {
        item.setSort(map.get(item.getUid()));
        item.setUpdateTime(new Date());
    });
    blogSortService.updateBatchById(blogSortList);

    return ResultUtil.result(SysConf.SUCCESS, MessageConf.OPERATION_SUCCESS);
}
 
Example 9
Source File: HappyChatSpringBootMainTest.java    From netty-chat with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueueProducer() throws Exception {
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.select("distinct groupNumber");
    List list = accountMapper.selectObjs(queryWrapper);
    System.out.println("finish");
}
 
Example 10
Source File: MyBatisAccessor.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <K extends Comparable, T extends IdentityObject<K>> K maximumIdentity(Class<T> clazz, K from, K to) {
	MyBatisMetadata metadata = metadatas.get(clazz);
	BaseMapper mapper = template.getMapper(metadata.getMapperClass());
	QueryWrapper<?> query = new QueryWrapper<>();
	query.select(maximumIdSqls.get(metadata.getOrmClass()));
	query.between(metadata.getColumnName(metadata.getPrimaryName()), from, to);
	List<K> values = mapper.selectObjs(query);
	return values.get(0);
}
 
Example 11
Source File: MyBatisAccessor.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> K minimumIdentity(Class<T> clazz, K from, K to) {
	MyBatisMetadata metadata = metadatas.get(clazz);
	BaseMapper mapper = template.getMapper(metadata.getMapperClass());
	QueryWrapper<?> query = new QueryWrapper<>();
	query.select(minimumIdSqls.get(metadata.getOrmClass()));
	query.between(metadata.getColumnName(metadata.getPrimaryName()), from, to);
	List<K> values = mapper.selectObjs(query);
	return values.get(0);
}
 
Example 12
Source File: RestDictTypeController.java    From Guns with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 查询所有字典
 *
 * @author stylefeng
 * @Date 2019-03-13
 */
@RequestMapping("/listTypes")
public ResponseData listTypes() {

    QueryWrapper<RestDictType> objectQueryWrapper = new QueryWrapper<>();
    objectQueryWrapper.select("dict_type_id", "code", "name");

    List<RestDictType> list = this.restDictTypeService.list(objectQueryWrapper);
    return new SuccessResponseData(list);
}
 
Example 13
Source File: DictTypeController.java    From Guns with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 查询所有字典
 *
 * @author stylefeng
 * @Date 2019-03-13
 */
@ResponseBody
@RequestMapping("/listTypes")
public ResponseData listTypes() {

    QueryWrapper<DictType> objectQueryWrapper = new QueryWrapper<>();
    objectQueryWrapper.select("dict_type_id", "code", "name");

    List<DictType> list = this.dictTypeService.list(objectQueryWrapper);
    return new SuccessResponseData(list);
}
 
Example 14
Source File: BlogServiceImpl.java    From mogu_blog_v2 with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Object> getBlogByKeyword(String keywords, Long currentPage, Long pageSize) {
    final String keyword = keywords.trim();
    QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
    queryWrapper.and(wrapper -> wrapper.like(SQLConf.TITLE, keyword).or().like(SQLConf.SUMMARY, keyword));
    queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
    queryWrapper.eq(SQLConf.IS_PUBLISH, EPublish.PUBLISH);
    queryWrapper.select(Blog.class, i -> !i.getProperty().equals(SQLConf.CONTENT));
    queryWrapper.orderByDesc(SQLConf.CLICK_COUNT);
    Page<Blog> page = new Page<>();
    page.setCurrent(currentPage);
    page.setSize(pageSize);

    IPage<Blog> iPage = blogService.page(page, queryWrapper);

    List<Blog> blogList = iPage.getRecords();

    List<String> blogSortUidList = new ArrayList<>();
    Map<String, String> pictureMap = new HashMap<>();
    final StringBuffer fileUids = new StringBuffer();

    blogList.forEach(item -> {

        // 获取图片uid
        blogSortUidList.add(item.getBlogSortUid());
        if (StringUtils.isNotEmpty(item.getFileUid())) {
            fileUids.append(item.getFileUid() + SysConf.FILE_SEGMENTATION);
        }

        // 给标题和简介设置高亮
        item.setTitle(getHitCode(item.getTitle(), keyword));
        item.setSummary(getHitCode(item.getTitle(), keyword));

    });

    // 调用图片接口,获取图片
    String pictureList = null;
    if (fileUids != null) {
        pictureList = this.pictureFeignClient.getPicture(fileUids.toString(), SysConf.FILE_SEGMENTATION);
    }
    List<Map<String, Object>> picList = webUtil.getPictureMap(pictureList);

    picList.forEach(item -> {
        pictureMap.put(item.get(SQLConf.UID).toString(), item.get(SQLConf.URL).toString());
    });

    Collection<BlogSort> blogSortList = new ArrayList<>();
    if (blogSortUidList.size() > 0) {
        blogSortList = blogSortService.listByIds(blogSortUidList);
    }

    Map<String, String> blogSortMap = new HashMap<>();
    blogSortList.forEach(item -> {
        blogSortMap.put(item.getUid(), item.getSortName());
    });

    // 设置分类名 和 图片
    blogList.forEach(item -> {
        if (blogSortMap.get(item.getBlogSortUid()) != null) {
            item.setBlogSortName(blogSortMap.get(item.getBlogSortUid()));
        }

        //获取图片
        if (StringUtils.isNotEmpty(item.getFileUid())) {
            List<String> pictureUidsTemp = StringUtils.changeStringToString(item.getFileUid(), SysConf.FILE_SEGMENTATION);
            List<String> pictureListTemp = new ArrayList<>();

            pictureUidsTemp.forEach(picture -> {
                pictureListTemp.add(pictureMap.get(picture));
            });
            // 只设置一张标题图
            if (pictureListTemp.size() > 0) {
                item.setPhotoUrl(pictureListTemp.get(0));
            } else {
                item.setPhotoUrl("");
            }
        }
    });


    Map<String, Object> map = new HashMap<>();

    // 返回总记录数
    map.put(SysConf.TOTAL, iPage.getTotal());

    // 返回总页数
    map.put(SysConf.TOTAL_PAGE, iPage.getPages());

    // 返回当前页大小
    map.put(SysConf.PAGE_SIZE, pageSize);

    // 返回当前页
    map.put(SysConf.CURRENT_PAGE, iPage.getCurrent());

    // 返回数据
    map.put(SysConf.BLOG_LIST, blogList);

    return map;
}
 
Example 15
Source File: BlogServiceImpl.java    From mogu_blog_v2 with Apache License 2.0 4 votes vote down vote up
@Override
public IPage<Blog> searchBlogByBlogSort(String blogSortUid, Long currentPage, Long pageSize) {
    BlogSort blogSort = blogSortService.getById(blogSortUid);
    if (blogSort != null) {
        HttpServletRequest request = RequestHolder.getRequest();
        String ip = IpUtils.getIpAddr(request);

        //从Redis取出数据,判断该用户24小时内,是否点击过该分类
        String jsonResult = redisUtil.get(RedisConf.TAG_CLICK + RedisConf.SEGMENTATION + ip + RedisConf.WELL_NUMBER + blogSortUid);

        if (StringUtils.isEmpty(jsonResult)) {

            //给标签点击数增加
            int clickCount = blogSort.getClickCount() + 1;
            blogSort.setClickCount(clickCount);
            blogSort.updateById();

            //将该用户点击记录存储到redis中, 24小时后过期
            redisUtil.setEx(RedisConf.TAG_CLICK + RedisConf.SEGMENTATION + ip + RedisConf.WELL_NUMBER + blogSortUid, clickCount + "",
                    24, TimeUnit.HOURS);
        }

    }

    QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();

    Page<Blog> page = new Page<>();
    page.setCurrent(currentPage);
    page.setSize(pageSize);

    queryWrapper.eq(SQLConf.BLOG_SORT_UID, blogSortUid);
    queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
    queryWrapper.eq(BaseSQLConf.IS_PUBLISH, EPublish.PUBLISH);
    queryWrapper.eq(BaseSQLConf.STATUS, EStatus.ENABLE);

    queryWrapper.select(Blog.class, i -> !i.getProperty().equals(SysConf.CONTENT));
    IPage<Blog> pageList = blogService.page(page, queryWrapper);
    List<Blog> list = pageList.getRecords();
    list = blogService.setTagAndSortAndPictureByBlogList(list);
    pageList.setRecords(list);
    return pageList;
}
 
Example 16
Source File: MyBatisAccessor.java    From jstarcraft-core with Apache License 2.0 4 votes vote down vote up
@Override
public <K extends Comparable, I, T extends IdentityObject<K>> Map<K, I> queryIdentities(Class<T> clazz, String name, StorageCondition<I> condition) {
	MyBatisMetadata metadata = metadatas.get(clazz);
	BaseMapper mapper = template.getMapper(metadata.getMapperClass());
	QueryWrapper<?> query = new QueryWrapper<>();
	String id = metadata.getColumnName(metadata.getPrimaryName());
	String column = metadata.getColumnName(name);
	query.select(id, column);
	ConditionType type = condition.getType();
	I[] values = condition.getValues();
	switch (type) {
	case All:
		break;
	case Between:
		query.between(column, values[0], values[1]);
		break;
	case Equal:
		query.eq(column, values[0]);
		break;
	case Higher:
		query.gt(column, values[0]);
		break;
	case In:
		query.in(column, values);
		break;
	case Lower:
		query.lt(column, values[0]);
		break;
	case Unequal:
		query.ne(column, values[0]);
		break;
	}
	List<Map<String, Object>> list = mapper.selectMaps(query);
	switch (getCaseStrategy()) {
	case LOWER:
		id = id.toLowerCase();
		column = column.toLowerCase();
		break;
	case UPPER:
		id = id.toUpperCase();
		column = column.toUpperCase();
		break;
	}
	Map<K, I> map = new HashMap<>();
	for (Map<String, Object> element : list) {
		map.put((K) element.get(id), (I) element.get(column));
	}
	return map;
}