目录
语法展示
基础的增删改查
分页查询
语法指导
删除操作
条件操作
@Mapper
public interface UserMapper extends BaseMapper {
}
public interface UserService extends IService {
}
@Service
public class UserServiceImpl extends ServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
}
@RestController
@RequestMapping("/test/user")
public class UserController {
@Autowired
private UserService userService;
}
查询所有,参数为构造器 : userMapper.selectList(null)
根据id添加数据,参数为实体类对象: userMapper.insert(user);
根据id修改数据,参数为实体类对象: userMapper.updateById(user)
根据id删除数据,参数为int / Long : userMapper.deleteById(5L)
根据id查询数据,餐位数int / Long : userMapper.selectById(4L)
R> getUsersByNamec(Page page, String name);
@Override
public R> getUsersByNamec(Page page, String name) {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
return R.success(userMapper.selectPage(page, queryWrapper));
}
// 根据姓名分页查询用户
@GetMapping("/getUsersByName")
public IPage getUsersByName(@RequestParam(defaultValue = "1") Long current,
@RequestParam(defaultValue = "2") Long size,
@RequestParam(required = false) String name) {
// 构建分页对象
Page userpage = new Page<>(current, size);
//返回对象得到分页所有数据
long pages = userPage.getPages(); //总页数
long current = userPage.getCurrent(); //当前页
List records = userPage.getRecords(); //查询数据集合
long total = userPage.getTotal(); //总记录数
boolean hasNext = userPage.hasNext(); //下一页
boolean hasPrevious = userPage.hasPrevious(); //上一页
// 调用服务方法进行分页查询
return userService.getUsersByName(page, name);
}
* mp复杂查询
* eq 就是 equal等于
* ne就是 not equal不等于
*
* gt 就是 greater than大于
* ge 就是 greater than or equal 大于等于
* lt 就是 less than小于
* le 就是 less than or equal 小于等于
*
* in 就是 in 包含(数组)
* between 就是 在2个条件之间(包括边界值)
* notBetween
*
* isNull 就是 等于null
* isNotNull 不等于
*
* like 就是 模糊查询
* notLike
* likeLeft
* likeRight
*
* orderBy
* orderByDesc
* orderByAsc
批量删除: userMapper.deleteBatchIds(Arrays.asList(8, 9, 10));
简单删除:
//简单条件删除
@Test
public void testDeleteByMap() {
HashMap map = new HashMap<>();
// name,age 为字段 ---> lucy,20 为值
map.put("name", "lucy");
map.put("age", 20);
int result = userMapper.deleteByMap(map);
System.out.println(result);
}
// 查询年龄 > 21 的
public void testQuery() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper
.ge("age", 21);
List users = userMapper.selectList(queryWrapper);
System.out.println(users);
}
// 查询name 是 Tom的
@Test
public void testSelectOne() {
QueryWrapperqueryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "Tom");
User user = userMapper.selectOne(queryWrapper);//只能返回一条记录,多余一条则抛出异常
System.out.println(user);
}
// 查询年龄在20 ~ 30之间的
@Test
public void testSelectCount() {
QueryWrapperqueryWrapper = new QueryWrapper<>();
queryWrapper.between("age", 20, 30);
Integer count = userMapper.selectCount(queryWrapper); //返回数据数量
System.out.println(count);
}
查询条件构建: 使用 QueryWrapper 对象 queryWrapper 构建查询条件。在这个例子中,通过 .select("name", "age") 指定要查询的字段,.like("name", "e") 表示名字中包含字母 "e",.likeRight("email", "5") 表示邮箱以数字 "5" 结尾。
@Test
public void testSelectMaps() {
QueryWrapperqueryWrapper = new QueryWrapper<>();
queryWrapper
.select("name", "age")
.like("name", "y")
.likeRight("email", "5");
List