@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestSpringMP {
@Autowired
private UserMapper userMapper;
/**
* 查询所有
*/
@Test
public void testSelectList() {
List<User> users = this.userMapper.selectList(null);
for (User user : users)
System.out.println(user);
}
/**
* 查询一个
*/
@Test
public void testSelectById() {
User user = userMapper.selectById(7L);
System.out.println(user);
}
/**
* 查询多个
*/
@Test
public void testSelectByIds() {
List<User> users = userMapper.selectBatchIds(Arrays.asList(1L, 2L));
for (User user : users) {
System.out.println(user);
}
}
/**
* 查询根据条件
*/
@Test
public void testSelectByConditions() {
User user = new User();
user.setAge(20);
QueryWrapper<User> wrapper = new QueryWrapper<>(user);
User user1 = userMapper.selectOne(wrapper);
System.out.println(user1);
}
/**
* 查询数据的条数
*/
@Test
public void testSelectCount() {
// QueryWrapper<Object> wrapper = new QueryWrapper<>();
Integer count = userMapper.selectCount(null);
System.out.println(count);
}
/**
* 查询集合
*/
@Test
public void testSelectLists() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.like("email","cn");
List<User> users = userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
/**
* 查询页数
*/
@Test
public void testSelectPages() {
/* QueryWrapper<User> wrapper = new QueryWrapper<User>();
wrapper.gt("age", 20); //年龄大于20岁*/
Page<User> page = new Page<>(1,3);
IPage<User> iPage = userMapper.selectPage(page, null);
System.out.println("总记录数:"+iPage.getTotal());
System.out.println("总页数:"+iPage.getPages());
List<User> users = iPage.getRecords();
for (User user : users) {
System.out.println(user);
}
}
/**
* 插入
*/
@Test
public void testInsert() {
User user = new User();
user.setAge(20);
user.setMail("test@itcast.cn");
user.setName("曹操");
user.setUserName("caocao");
user.setPassword("123456");
int result = userMapper.insert(user);
System.out.println("数据库受影响的行数:"+result);//数据库受影响的行数
System.out.println("id:"+user.getId());
}
/**
* 根据id更新
*/
@Test
public void testUpdate() {
User user = new User();
user.setAge(18);
user.setMail("test@itcast.cn");
user.setName("小牛");
user.setUserName("xiaoniu");
user.setPassword("123456");
user.setId(7L);
userMapper.updateById(user);
}
/**
* 根据条件更新
*/
@Test
public void testUpdateByConditions() {
User user = new User();
user.setAge(20);
user.setName("小牛1");
user.setUserName("xiaoniu667");
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name","小牛"); //字段的名字
userMapper.update(user,wrapper);
}
/**
* 根据条件更新1
*/
@Test
public void testUpdateByConditions1() {
UpdateWrapper<User> wrapper = new UpdateWrapper<>();
wrapper.eq("name", "小牛1").set("age", 21);/*满足名字为小牛1,修改他的年龄为21*/
userMapper.update(null, wrapper);
}
/**
* 删除操作根据id
*/
@Test
public void testDelete() {
this.userMapper.deleteById(6L);
}
/**
* 删除操作根据条件
*/
@Test
public void testDeleteByConditions() {
HashMap<String, Object> map = new HashMap<>();
map.put("name","小牛1");
this.userMapper.deleteByMap(map);
}
/**
* 删除操作根据条件1
*/
@Test
public void testDeleteByConditions1() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name","张三");
userMapper.delete(wrapper);
}
/**
* 删除操作根据条件2
*/
@Test
public void testDeleteByConditions2() {
User user = new User();
user.setAge(20);
user.setName("李四");
QueryWrapper<User> wrapper = new QueryWrapper<>(user);
userMapper.delete(wrapper);
}
/**
* 删除操作根据Ids
*/
@Test
public void testDeleteByIds() {
userMapper.deleteBatchIds(Arrays.asList(3L,4L));
}
}
|