package com.kuang;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.kuang.mapper.UserMapper;
import com.kuang.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
@SpringBootTest
class MybitisPlusApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
@Test
public void testInsert(){
User user = new User();
user.setName("sdfsdf");
user.setAge(3);
user.setEmail("313@qq.com");
int insert = userMapper.insert(user);
System.out.println(insert);
System.out.println(user);
}
@Test
public void test1OptimisticLocker() {
User user = userMapper.selectById(5L);
user.setName("sdfsd11f");
user.setEmail("sdfsdf@qqq");
userMapper.updateById(user);
}
@Test
public void testSelectById() {
User user = userMapper.selectById(1L);
System.out.println(user);
}
@Test
public void testSelect2() {
String aa = "2021-08-03 01:00:08";
String aa2 = "2021-09-03 01:00:08";
QueryWrapper<User> betweenWrapper = new QueryWrapper<>();
betweenWrapper.between("create_time", aa, aa2);
// SFunction<User, ?> age;
List<User> list = userMapper.selectList(betweenWrapper);
// .lt(User::getCreateTime, "2021-08-30 01:00:08"));
list.forEach(System.out::println);
// User user1 = new User();
//// user1.setId(2L);
// user1.setName("44444");
// userMapper.update(user1,new QueryWrapper<User>()
// .lambda().lt(User::getCreateTime, "2021-08-30 01:00:08"));
// User user = userMapper.selectById(1L);
// System.out.println(user);
}
@Test
public void testUpdate2() {
User user1 = new User();
// user1.setId(2L);
user1.setName("44444");
userMapper.update(user1,new QueryWrapper<User>()
.lambda().lt(User::getCreateTime, "2021-08-30 01:00:08"));
// User user = userMapper.selectById(1L);
// System.out.println(user);
}
@Test
public void testSelectByBatchId() {
List<User> list = userMapper.selectBatchIds(Arrays.asList(1, 2, 3, 4));
System.out.println(list);
}
@Test
public void testUpdate() {
User user = new User();
user.setId(7L);
user.setName("关注狂人说公众号11");
user.setAge(21);
userMapper.updateById(user);
List<User> list = userMapper.selectList(null);
list.forEach(System.out::println);
}
void contextLoads2() {
// for(int i=0; i<10; i++){
// User user = new User();
// user.setAge(11);
// user.setEmail("qq@qq.com");
// user.setName("jame"+i);
// userMapper.insert(user);
// }
// User user = new User();
// user.setId(8L);
// user.setId(3L);
// user.setAge(222);
// user.setEmail("qq@qq.com");
// user.setName("jame");
// userMapper.insert(user);
// User user = new User();
// List<User> list = new ArrayList<>();
// list.add(user );
// assertThat(userMapper.deleteById(4L));
// userMapper.delete(new QueryWrapper<User>()
// .lambda().eq(User::getName, "jame2")
// );
// userMapper.updateById(new User().setId(2L));
// assertThat(mapper.updateById(new User().setId(1L).setEmail("ab@c.c"))).isGreaterThan(0);
// mapper.update(user,null);
// mapper.update(
// new User().setName("mp"),
// Wrappers.<User>lambdaUpdate()
// .set(User::getAge, 3)
// .eq(User::getId, 2)
// );
// User user = mapper.selectById(7);
// mapper.update(null, Wrappers.<User>lambdaUpdate().set(User::getName, "aaa").eq(User::getId, 8));
// System.out.println(user);
// assertThat(mapper.selectById(8).getEmail()).isEqualTo("qq@qq.com");
// mapper.update(
// new User().setEmail("miemie2@baomidou.com"),
// Wrappers.<User>lambdaUpdate()
// .set(User::getAge, null)
// .eq(User::getId, 2)
// );
// System.out.println(i);
// List<User> users = mapper.selectList(null );
// users.forEach(System.out::println);
// System.out.println(users);
}
}