Search in sources :

Example 6 with User

use of com.maxqiu.demo.entity.User in project demo-SpringBoot by Max-Qiu.

the class TestUserMapper method selectOne.

/**
 * 根据条件查找一个
 */
@Test
void selectOne() {
    // SELECT id,username,age,email FROM smp_user WHERE (age > ? AND age < ?)
    LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
    queryWrapper.gt(User::getAge, 50).lt(User::getAge, 100);
    // 慎用!!!除非保证查询后的结果只有一条或者为空,否则会抛出异常 Expected one result (or null) to be returned by selectOne(), but found: 5
    User user = userMapper.selectOne(queryWrapper);
    System.out.println(user);
}
Also used : User(com.maxqiu.demo.entity.User) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with User

use of com.maxqiu.demo.entity.User in project demo-SpringBoot by Max-Qiu.

the class TestCustomUserMapper method rowBoundsTest.

/**
 * MyBatis 自带分页(逻辑分页)
 */
@Test
void rowBoundsTest() {
    // 设置分页条件,offset 代表第 i+1 条数据,limit 代表取几条。如下示例中,取第二条数据,共取 5 条
    RowBounds rowBounds = new RowBounds(1, 5);
    // select * from smp_user WHERE username like concat('%',?,'%')
    List<User> users = userMapper.rowBoundList(rowBounds, Maps.newHashMap("username", "%"));
    // 虽然 select * 查出了 2 条数据,但是集合中仅一条数据,因为分页设置从第二条数据开始取
    System.out.println(users.size());
    users.forEach(System.out::println);
}
Also used : User(com.maxqiu.demo.entity.User) RowBounds(org.apache.ibatis.session.RowBounds) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with User

use of com.maxqiu.demo.entity.User in project demo-SpringBoot by Max-Qiu.

the class ListOperationsTest method push.

/**
 * RPUSH 在列表右侧插值
 *
 * RPUSHX 仅key存在时在列表右侧插值
 *
 * LPUSH 在列表左侧插值
 *
 * LPUSHX 仅key存在时在列表左侧插值
 */
@Test
@Order(2)
void push() {
    // RPUSH 在列表右侧插入单个元素
    stringListOperations.rightPush("list1", "a");
    assertEquals(1, stringListOperations.size("list1"));
    // RPUSH 在列表右侧插入多个元素
    stringListOperations.rightPushAll("list1", "b", "c", "d");
    // stringListOperations.rightPushAll("list1", Arrays.asList("b", "c", "d"));
    assertEquals(4, stringListOperations.size("list1"));
    // RPUSHX 仅key存在时在列表右侧插值
    stringListOperations.rightPushIfPresent("list1", "e");
    assertEquals(5, stringListOperations.size("list1"));
    // LPUSH 在列表左侧插入单个元素
    stringListOperations.leftPush("list2", "a");
    assertEquals(1, stringListOperations.size("list2"));
    // LPUSH 在列表左侧插入多个元素
    stringListOperations.leftPushAll("list2", "b", "c");
    // stringListOperations.leftPushAll("list2", Arrays.asList("d", "e"));
    assertEquals(3, stringListOperations.size("list2"));
    // LPUSHX 仅key存在时在列表左侧插值
    stringListOperations.leftPushIfPresent("list2", "d");
    assertEquals(4, stringListOperations.size("list2"));
    // LINSERT 插入元素
    // 在指定元素左侧插入
    stringListOperations.leftPush("list1", "b", "hello");
    assertEquals("hello", stringListOperations.index("list1", 1));
    // 在指定元素右侧插入
    stringListOperations.rightPush("list2", "c", "world");
    assertEquals("world", stringListOperations.index("list2", 2));
    // 插入的类型需要相同,对应类型使用对应的Operations
    // 例如:int类型
    integerListOperations.rightPushAll("nums", 1, 2, 3);
    assertEquals(3, integerListOperations.size("nums"));
    // 例如:User类型
    User user = new User(1, "tom", new BigDecimal("165.3"));
    userListOperations.rightPush("users", user);
    User user2 = userListOperations.rightPop("users");
    assertEquals(user, user2);
    // 清空数据
    redisTemplate.delete(Arrays.asList("list1", "list2", "nums", "users"));
}
Also used : User(com.maxqiu.demo.entity.User) BigDecimal(java.math.BigDecimal) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 9 with User

use of com.maxqiu.demo.entity.User in project demo-SpringBoot by Max-Qiu.

the class IndexController method helloWorld.

/**
 * 入门 生产者
 */
@GetMapping("hello-world")
public Integer helloWorld() {
    System.out.println("~~~~Sent:" + i);
    // 发送字符串
    rabbitTemplate.convertAndSend(// 发送到哪个队列,不填写该参数时默认为空队列
    "hello-world", // 具体消息内容
    i + "");
    // 发送数字
    rabbitTemplate.convertAndSend("hello-world", i);
    // 发送对象
    rabbitTemplate.convertAndSend("hello-world", new User(i, "TOM"));
    return i++;
}
Also used : User(com.maxqiu.demo.entity.User) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 10 with User

use of com.maxqiu.demo.entity.User 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)

Aggregations

User (com.maxqiu.demo.entity.User)61 Test (org.junit.jupiter.api.Test)55 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)55 ArrayList (java.util.ArrayList)9 LambdaUpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper)5 BigDecimal (java.math.BigDecimal)3 Order (org.junit.jupiter.api.Order)3 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)2 LambdaQueryChainWrapper (com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper)2 SearchHit (org.springframework.data.elasticsearch.core.SearchHit)2 NativeSearchQuery (org.springframework.data.elasticsearch.core.query.NativeSearchQuery)2 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)2 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 IPage (com.baomidou.mybatisplus.core.metadata.IPage)1 LambdaUpdateChainWrapper (com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper)1 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)1 MyPage (com.maxqiu.demo.model.MyPage)1 ParamSome (com.maxqiu.demo.model.ParamSome)1 List (java.util.List)1 RowBounds (org.apache.ibatis.session.RowBounds)1