@SpringBootTest
class SpringbootMybatisplusApplicationTests {
@Autowired
UserMapper userMapper;
/*查询所有*/
@Test
void findAll() {
List<User> users = userMapper.selectList(null);
for(User user:users){
System.out.println(user);
}
}
/*插入测试*/
@Test
public void testInsert(){
User user = new User();
user.setName("王一宁");
user.setPassword("123456");
user.setUserName("吾皇万岁");
user.setEmail("123456789@163.com");
user.setAge(23);
int result = userMapper.insert(user);
System.out.println("受影响的行数"+result);
//获取自增长的id plus 回填user对象中
System.out.println("id:"+user.getId());
}
/*测试更具id 查询*/
@Test
public void testSelectById(){
User user = userMapper.selectById(1L);
System.out.println(user);
//User(id=1, userName=wang, password=null, name=王一宁, age=23, email=1506773471@qq.com, address=null)
}
/*测试更新根据id*/
@Test
public void testUpdateById(){
User user = new User();
user.setId(1L);
user.setAge(19);
user.setEmail("110");
userMapper.updateById(user);
}
/*根据条件更新*/
@Test
public void testUpdate(){
User user = new User();
user.setAge(19);
user.setEmail("110");
//设置条件
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("user_name","wang");
userMapper.update(user,wrapper);
}
/*根据id删除*/
@Test
public void deleteById(){
int i = userMapper.deleteById(2L);
System.out.println(i);
}
/*吧多条件删除条件封装到Map中*/
@Test
public void deleteByMap(){
Map<String,Object> map = new HashMap<>();
map.put("user_name","wang");
map.put("password","123456");
userMapper.deleteByMap(map);
}
/*条件查询*/
@Test
public void delete(){
//设置条件
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("user_name","wang").eq("password","123456");
userMapper.delete(wrapper);
}
}