mp的简单使用

创建springboot项目时,使用阿里的地址,勾选mp的依赖

 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
 </dependency>

Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能

@Component
@Mapper
public interface StuDao extends BaseMapper<Student> {

在mp的功能之上,可追加需要的功能

public interface IStudentServiceQuick extends IService<Student> {

    //追加的功能
    public Boolean saveStudent(Student stu) ;
    public Boolean update(Student stu);
    public Boolean delete(int id);
    public IPage<Student> getPage(int currentPage, int PageSize, Student stu);
    public IPage<Student> getPage(int currentPage, int PageSize);
}

service的实现类 ,当然 接口追加的功能需要实现


@Service
public class IStudentServiceQuickImpl extends ServiceImpl<StuDao, Student> implements IStudentServiceQuick {

mp里的方法使用


@ResponseBody
@Controller
@RequestMapping("/students")
public class StudentController2 {

    @Autowired
     IStudentServiceQuick studentService;

//获取所有数据list
    @GetMapping
    public R getAll(){
       return  new R(true,studentService.list());
    }
//添加save
    @PostMapping
    public R save(@RequestBody Student student) throws IOException {
        if (student.name.equals("123")) throw new IOException();
        boolean flag = studentService.save(student);
        return new R(flag,flag ? "添加成功^_^" : "添加失败-_-!");
    }
//删除remove
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable int id){
        return new R(studentService.removeById(id));
    }

//修改update
    @PutMapping
    public R updateById(@RequestBody Student student){
        return new R(studentService.updateById(student));
    }
//通过id查
    @GetMapping("/{id}")
    public R queryById(@PathVariable int id){
        return new R(true,studentService.getById(id));
    }
//分页
  @GetMapping("/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Student stu){
        IPage<Student> page = studentService.getPage(currentPage, pageSize,stu);
        if (currentPage  >  page.getPages())
            page = studentService.getPage((int)page.getPages(),pageSize,stu);
        return new R( true , page);
    }
}

使用分页功能需要添加拦截器


@Configuration
public class MPConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor  interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

模糊查询`

 //模糊查询两种写法
    @Test
    void queryBy1(){
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.like("name","村");
        System.out.println(stuDao.selectList(qw));
    }

  @Test
    void queryBy2(){
        String name ="村";
        LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper();
        //若传来的数据为空
        lqw.like(name!=null ,Student::getName,name);
//        lqw.like(Student::getName,name);
        System.out.println(stuDao.selectList(lqw));
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值