Java Code Examples for tk.mybatis.mapper.weekend.WeekendSqls

The following examples show how to use tk.mybatis.mapper.weekend.WeekendSqls. These examples are extracted from open source projects. 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
@GetMapping("/_useSqlhelper_over_pageHelper")
public Page list_sqlhelper_over_pageHelper(
        @RequestParam(name = "pageNo") Integer pageNo,
        @RequestParam(name = "pageSize") Integer pageSize,
        @RequestParam(name = "sort", required = false) String sort,
        @RequestParam(value = "countColumn", required = false) String countColumn) {

    Page page = PageHelper.offsetPage(pageNo, pageSize);
    // Page page = PageHelper.startPage(pageNo, pageSize, sort);
    page.setCountColumn(countColumn);
    PageHelper.orderBy(" id desc");

    WeekendSqls weekendSqls = WeekendSqls.custom()
            .andLike("name", "%zhangsan%")
            .andGreaterThanOrEqualTo("age", 10);
    Example example = Example.builder(User.class).setDistinct(false).where(weekendSqls).build();

    JSON jsons = JSONBuilderProvider.simplest();

    List<User> users = userDao.selectByExample(example);
    String json = jsons.toJson(users);
    System.out.println(json);
    json = jsons.toJson(users);
    System.out.println(json);
    PageInfo pageInfo1 = new PageInfo(page);
    json = jsons.toJson(pageInfo1);
    System.out.println(json);
    PageInfo pageInfo2 = new PageInfo(users);
    json = jsons.toJson(pageInfo2);
    System.out.println(json);
    return page;
}
 
Example 2
Source Project: jvue-admin   Source File: JvueOAuth2UserServiceImpl.java    License: MIT License 5 votes vote down vote up
@Override
public JwtUserDetails updateUser(String accountType, String account) {

	Example example = new Example.Builder(JvueOauthUser.class)
			.where(WeekendSqls.<JvueOauthUser>custom().andEqualTo(JvueOauthUser::getAccountType, accountType)
					.andEqualTo(JvueOauthUser::getAccountId, account))
			.build();

	JvueOauthUser oauthUser = oauthUserMapper.selectOneByExample(example);
	JvueUser user;

	if (oauthUser == null) {
		user = new JvueUser();
		user.setUsername(account + "#" + accountType);
		user.setSuperUser(YesOrNoEnum.No.ordinal());
		user.setStatus(1);
		userMapper.insertSelective(user);
		
		oauthUser = new JvueOauthUser();
		oauthUser.setUserId(user.getId());
		oauthUser.setAccountId(account);
		oauthUser.setAccountType(accountType);

		oauthUserMapper.insertSelective(oauthUser);
	} else {
		user = userMapper.selectByPrimaryKey(oauthUser.getUserId());
	}

	List<JvueExRole> userRoles = exUserMapper.selectRoles(user.getId());
	List<Integer> roles;
       if (userRoles.isEmpty()) {
           roles = new ArrayList<>();
       } else {
           roles = userRoles.stream().map(role->role.getId()).collect(Collectors.toList());
       }
       return new JwtUserDetails(user.getId(), user.getUsername(), user.getPassword(),
               user.getSuperUser(), user.getNickname(), user.getEmail(), userRoles, roles);
}
 
Example 3
Source Project: jvue-admin   Source File: JvueRoleServiceImpl.java    License: MIT License 5 votes vote down vote up
@Override
    @Cacheable(value = "rolePage", key = "#roles")
    public ModuleAndPages<Integer> findModuleAndPage(List<Integer> roles) {

        Assert.notEmpty(roles, "未授权");
        
        // 根据权限查询对应的菜单和modules/segments
        ModuleAndPages<Integer> moduleAndPages = new ModuleAndPages<>();

//        Example examplePage = new Example.Builder(JvueRolePage.class)
//                .where(WeekendSqls.<JvueRolePage>custom().andIn(JvueRolePage::getRoleId, roles))
//                .build();
        
        List<JvueExPage> rolePages = exRolePageMapper.selectPageByRole(roles);

        List<Integer> moduleIds = rolePages.stream().map(page -> page.getModuleId()).distinct()
                .collect(Collectors.toList());

        Example exampleModule = new Example.Builder(JvueModule.class)
                .where(WeekendSqls.<JvueModule>custom().andIn(JvueModule::getId, moduleIds))
                .build();
        List<JvueModule> modules = jvueModuleMapper.selectByExample(exampleModule);

        // 查询角色对应的segments
        MultiValueMap<Integer, Integer> segmentMap = new LinkedMultiValueMap<>();

        List<JvueExRoleSegment> roleSegments = jvueRoleSegmentMapper.selectSegmentByRole(roles);
        
        for (JvueExRoleSegment rs: roleSegments) {
            segmentMap.add(rs.getSegment().getPageId(), rs.getSegment().getSegmentCode());
        }
        
        // 生成返回结果
        moduleAndPages.setPages(rolePages);
        moduleAndPages.setModules(modules);
        moduleAndPages.setSegments(segmentMap);

        return moduleAndPages;
    }
 
Example 4
Source Project: jvue-admin   Source File: JvuePageServiceImpl.java    License: MIT License 5 votes vote down vote up
@Override
public ModuleAndPages<Integer> findModuleAndPage() {

    ModuleAndPages<Integer> moduleAndPages = new ModuleAndPages<>();
    List<JvueExPage> pages = jvueExPageMapper.findByModuleId(null);

    List<Integer> moduleIds = pages.stream().map(page -> page.getModuleId()).distinct()
            .collect(Collectors.toList());
    
    Example example = new Example.Builder(JvueModule.class)
            .where(WeekendSqls.<JvueModule>custom().andIn(JvueModule::getId, moduleIds))
            .build();

    List<JvueModule> modules = jvueModuleMapper.selectByExample(example);
    
    List<JvueSegment> segments = jvueSegmentMapper.selectAll();

    MultiValueMap<Integer, Integer> segmentMap = new LinkedMultiValueMap<>();
    for (JvueSegment s: segments) {
        segmentMap.add(s.getPageId(), s.getSegmentCode());
    }
    
    moduleAndPages.setPages(pages);
    moduleAndPages.setModules(modules);
    moduleAndPages.setSegments(segmentMap);

    return moduleAndPages;
}
 
Example 5
Source Project: jvue-admin   Source File: JvuePageServiceImpl.java    License: MIT License 5 votes vote down vote up
@Override
public List<JvuePage> findByModule(Integer moduleId) {
    Example example = new Example.Builder(JvuePage.class)
            .where(WeekendSqls.<JvuePage>custom().andEqualTo(JvuePage::getModuleId, moduleId).andIsNull(JvuePage::getParentId))
            .build();
    return jvuePageMapper.selectByExample(example);
}
 
Example 6
Source Project: sms   Source File: MessageServiceImpl.java    License: MIT License 5 votes vote down vote up
@Override
public int updateMessageValidateStatus(Message message) {
	Message updateMessage = new Message();
	updateMessage.setValidateStatus(Columns.ValidateStatus.YES);
	Example example = new Example.Builder(Message.class)
			.where(WeekendSqls.<Message>custom()
					.andEqualTo(Message::getId, message.getId())
					.andEqualTo(Message::getValidateStatus, message.getValidateStatus()))//CAS
			.build();
	int result = messageMapper.updateByExampleSelective(updateMessage, example);
	logger.info("{}更新验证码状态结果{}", message, result);
	return result;
}
 
Example 7
Source Project: sms   Source File: MessageServiceImpl.java    License: MIT License 5 votes vote down vote up
@Override
public Message queryLatestMessage(String mobile, Template template) {
	Instant expire = Instant.now().minusSeconds(template.getValidateCodeExpire());
	Example example = new Example.Builder(Message.class)
			.where(WeekendSqls.<Message>custom()
					.andEqualTo(Message::getMobile, mobile)
					.andEqualTo(Message::getTemplateId, template.getId())
					.andGreaterThan(Message::getSendStatus, Columns.SendStatus.FAILURE)
					.andGreaterThan(Message::getCreateDate, Date.from(expire)))
			.orderByDesc("id")//此处等同于create_date,但是具有更好的性能
			.build();
	PageHelper.startPage(1, 1, false);
	return messageMapper.selectOneByExample(example);
}
 
Example 8
Source Project: sms   Source File: MessageServiceImpl.java    License: MIT License 5 votes vote down vote up
@Override
public List<Message> querySendingMessages(Date fromDate, String channel) {
	Example example = new Example.Builder(Message.class)
			.where(WeekendSqls.<Message>custom()
					.andEqualTo(Message::getSendStatus, Columns.SendStatus.SENDING)
					.andEqualTo(Message::getChannel, channel)
					.andGreaterThan(Message::getCreateDate, fromDate))
			.orderByDesc("id")
			.build();
	PageHelper.startPage(1, 100, false);
	return messageMapper.selectByExample(example);
}
 
Example 9
@GetMapping("/_useMyBatis")
public PagingResult list_useMyBatis(
        @RequestParam(name = "pageNo", required = false) Integer pageNo,
        @RequestParam(name = "pageSize", required = false) Integer pageSize,
        @RequestParam(name = "sort", required = false) String sort,
        @RequestParam(value = "count", required = false) boolean count,
        @RequestParam(value = "useLastPageIfPageOut", required = false) boolean useLastPageIfPageOut,
        @RequestParam(value = "namelike", required = false, defaultValue = "") String namelike,
        @RequestParam(value = "grateAge", required = false, defaultValue = "10") int age,
        @RequestParam(value = "testTenant", required = false, defaultValue = "false") boolean testTenant,
        @RequestParam(value = "tenantId", required = false, defaultValue = "1") String tenantId) {

    JSON jsons = JSONBuilderProvider.simplest();

    User queryCondition = new User();
    queryCondition.setAge(age);
    queryCondition.setName(namelike);


    WeekendSqls weekendSqls = WeekendSqls.custom()
            .andLike("name", "%" + namelike + "%")
            .andGreaterThanOrEqualTo("age", age);
    Example example = Example.builder(User.class).setDistinct(false).where(weekendSqls).build();

    // 正常查询
    List<User> users = userDao.selectByExample(example);
    String json = jsons.toJson(users);
    System.out.println("正常查询");
    System.out.println(json);
    System.out.println("=====================================");
    // 用 RowBounds分页查询
    users = userDao.selectByExampleAndRowBounds(example, new RowBounds(pageNo == null ? 1 : pageNo, pageSize == null ? -1 : pageSize));
    json = jsons.toJson(users);
    System.out.println("使用row bounds 查询");
    System.out.println(json);
    System.out.println("=====================================");

    // 使用 SqlHelper API 分页查询
    PagingRequest request = SqlPaginations.preparePagination(pageNo == null ? 1 : pageNo, pageSize == null ? -1 : pageSize, sort);
    request.setEscapeLikeParameter(false);
    users = userDao.selectByExample(example);
    System.out.println("使用 SqlHelper API 分页查询");
    System.out.println(jsons.toJson(request));
    request.setCount(count);
    request.setUseLastPageIfPageOut(useLastPageIfPageOut);
    json = jsons.toJson(request.getResult());
    System.out.println(json);

    json = jsons.toJson(users);
    System.out.println(json);
    return request.getResult();
}
 
Example 10
Source Project: jvue-admin   Source File: JvueUserServiceImpl.java    License: MIT License 4 votes vote down vote up
@Override
    @CacheEvict(value = "JwtUserDetailsService", key = "#result.username")
    public JvueUser updateRoles(Long id, List<Integer> roles) {
        Assert.notNull(id, "用户ID不能为空");
        JvueUserRole userRole = new JvueUserRole();
        userRole.setUserId(id);
        List<JvueUserRole> userRoles = userRoleMapper.select(userRole);
        
        Collection<Integer> deleds;
        Collection<Integer> addeds;
        
        List<Integer> existes = userRoles.stream().map(r -> r.getRoleId()).collect(Collectors.toList());
        
        if (existes != null) {
            if (roles != null && roles.size() > 0) {
                deleds = CollectionUtils.subtract(existes, roles);
                addeds = CollectionUtils.subtract(roles, existes);
            } else {
                // 删除所有
                userRoleMapper.delete(userRole);
                deleds = null;
                addeds = null;
            }
        } else {
            deleds = null;
            addeds = roles;
        }
        
        if (deleds != null && deleds.size() > 0) {
//            SearchCriteria<UserRole> searchCriteria = new SearchCriteria<>();
//            searchCriteria.add(JpaRestrictions.eq("userId", id, false));
//            searchCriteria.add(JpaRestrictions.in("roleId", deleds, false));
//            List<UserRole> dRoles = userRoleMapper.findAll(searchCriteria);
//            userRoleMapper.deleteAll(dRoles);
            
            Example example = new Example.Builder(JvueUserRole.class).where(
                    WeekendSqls.<JvueUserRole>custom().andEqualTo(JvueUserRole::getUserId, id).andIn(JvueUserRole::getRoleId, deleds))
                    .build();
            
            userRoleMapper.deleteByExample(example);
        }
        
        if (addeds != null && addeds.size() > 0) {
            
            for (Integer roleId: addeds) {
                JvueUserRole role = new JvueUserRole();
                role.setUserId(id);
                role.setRoleId(roleId);
                userRoleMapper.insertSelective(role);
            }
        }
        
        return getOne(id);
    }