Search in sources :

Example 36 with QueryWrapper

use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project demo-SpringBoot by Max-Qiu.

the class TestGroupByAndHaving method testGroup.

/**
 * 分组
 */
@Test
public void testGroup() {
    // SELECT classes_id, count(*) FROM smp_student GROUP BY classes_id
    QueryWrapper<Student> wrapper = new QueryWrapper<>();
    wrapper.select("classes_id, count(*)").groupBy("classes_id");
    List<Map<String, Object>> maplist = studentMapper.selectMaps(wrapper);
    for (Map<String, Object> mp : maplist) {
        System.out.println(mp);
    }
    // lambda语法可能不太方便获取count(*)这种
    // lambdaQueryWrapper groupBy
    LambdaQueryWrapper<Student> lambdaQueryWrapper = new QueryWrapper<Student>().lambda().select(Student::getClassesId).groupBy(Student::getClassesId);
    for (Student user : studentMapper.selectList(lambdaQueryWrapper)) {
        System.out.println(user);
    }
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) Student(com.maxqiu.demo.entity.Student) Map(java.util.Map) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 37 with QueryWrapper

use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project demo-SpringBoot by Max-Qiu.

the class TestOther method testOperationAllTable.

/**
 * 防止操作全表
 *
 * 插件内添加 interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
 *
 * 可在遇到全表操作时,抛出异常
 */
@Test
void testOperationAllTable() {
    userMapper.selectList(new QueryWrapper<>());
    userMapper.deleteById(1L);
    userMapper.insert(new User().setAge(18).setEmail("13"));
    User user = new User();
    user.setUsername("test_update");
    userMapper.update(user, new QueryWrapper<User>().eq("id", 1L));
    try {
        userMapper.update(new User().setAge(18), new QueryWrapper<>());
    } catch (MyBatisSystemException e) {
        System.out.println("发现全表更新");
    }
    try {
        userMapper.delete(new QueryWrapper<>());
    } catch (MyBatisSystemException e) {
        System.out.println("发现全表删除");
    }
    List<User> list = userMapper.selectList(new QueryWrapper<>());
    if (list.size() == 0) {
        System.out.println("数据都被删掉了");
    } else {
        System.out.println("数据还在");
    }
}
Also used : User(com.maxqiu.demo.entity.User) MyBatisSystemException(org.mybatis.spring.MyBatisSystemException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 38 with QueryWrapper

use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project waynboot-mall by wayn111.

the class ChannelServiceImpl method checkChannelNameUnique.

@Override
public String checkChannelNameUnique(Channel channel) {
    long channelId = Objects.isNull(channel.getId()) ? -1L : channel.getId();
    Channel shopChannel = getOne(new QueryWrapper<Channel>().eq("name", channel.getName()));
    if (shopChannel != null && shopChannel.getId() != channelId) {
        return SysConstants.NOT_UNIQUE;
    }
    return SysConstants.UNIQUE;
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Channel(com.wayn.common.core.domain.shop.Channel)

Example 39 with QueryWrapper

use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project waynboot-mall by wayn111.

the class ChannelServiceImpl method checkChannelCodeUnique.

@Override
public String checkChannelCodeUnique(Channel channel) {
    long channelId = Objects.isNull(channel.getId()) ? -1L : channel.getId();
    Channel shopChannel = getOne(new QueryWrapper<Channel>().eq("code", channel.getCode()));
    if (shopChannel != null && shopChannel.getId() != channelId) {
        return SysConstants.NOT_UNIQUE;
    }
    return SysConstants.UNIQUE;
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Channel(com.wayn.common.core.domain.shop.Channel)

Example 40 with QueryWrapper

use of com.baomidou.mybatisplus.core.conditions.query.QueryWrapper in project waynboot-mall by wayn111.

the class DictServiceImpl method deleteDictTypeById.

@Transactional
@Override
public boolean deleteDictTypeById(List<Long> dictIds) {
    for (Long dictId : dictIds) {
        Dict dict = getById(dictId);
        remove(new QueryWrapper<Dict>().eq("parent_type", dict.getValue()));
    }
    return removeByIds(dictIds);
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Dict(com.wayn.common.core.domain.system.Dict) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)723 Transactional (org.springframework.transaction.annotation.Transactional)98 IPage (com.baomidou.mybatisplus.core.metadata.IPage)82 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)74 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)72 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)65 ArrayList (java.util.ArrayList)61 Session (org.apache.shiro.session.Session)61 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)60 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)55 Problem (top.hcode.hoj.pojo.entity.problem.Problem)50 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)45 Date (java.util.Date)44 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 HashMap (java.util.HashMap)34 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)34 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)34 ApiOperation (io.swagger.annotations.ApiOperation)32 HttpSession (javax.servlet.http.HttpSession)31 Judge (top.hcode.hoj.pojo.entity.judge.Judge)30