mybaits-plus 看這篇文章就夠了

mybaits-plus 看這篇文章就夠了

mybaits-plus

工欲善其事,必先利其器。現在就讓我們看看廣大碼農打怪升級利器之–mybaiits-plus.

基本的crud操作

關於之前的一些基本操作可以看我上一篇的文卓:
mybaits-plus的一些基本操作
接下來我直接擼實戰
新增

 @Test
    void add(){
        //在接口繼承basemapper父類,拿到他實現的方法(可能會遇到的小坑 插入或更新的字段有 空字符串 或者 null)
        testPlanMapper.insert(T);
        //單條新增 實現IService接口,拿到他的批量新增,批量修改,批量刪除的方法
        testPlanService.save(T);
        //批量新增
        testPlanService.saveBatch(new ArrayList<>());
    }

修改

   @Test
    void update(){
        TestPlan testPlan = new TestPlan();
        //根據條件構造器修改
        testPlanMapper.update(testPlan,new QueryWrapper<T>().eq("id",""));
        //根據id集合修改
        testPlanMapper.updateById(T);
        //單條修改
        testPlanService.update(T);
        //批量修改
        testPlanService.updateBatchById(new ArrayList<>());
    }

刪除

@Test
    void delete(){
        //根據構造條件來刪除制定字段
        testPlanMapper.delete(new QueryWrapper<T>().eq("ss",""));
        //批量刪除id
        testPlanMapper.deleteBatchIds(new ArrayList<>());
        //根據id刪除
        testPlanMapper.deleteById(2);
        //根據map刪除指定數據
        testPlanMapper.deleteByMap(new HashMap<>());
        //批量刪除根據id數組
        testPlanService.deleteTestPlan(new Integer[5]);
    }

查詢

 @Test
    void select(){
        //根據構造器統計某個數據
        testPlanMapper.selectCount(new QueryWrapper<TestPlan>().eq("ss",""));
        //根據構造器查詢列表數據
        testPlanMapper.selectList(new QueryWrapper<TestPlan>().eq("ss",""));
        //根據id查詢單條數據
        testPlanMapper.selectOne(new QueryWrapper<TestPlan>().eq("ss",""));
        //根據構造器參數分頁查詢
        testPlanMapper.selectPage(new Page<>(),new QueryWrapper<TestPlan>().eq("ss",""));
    }

以上的操作基本實現我們正常的開發,其實這個也是方便我們的開發,裏面的原理的還是要從源碼中去看,寫的比較簡單,開發中有踩到坑的歡迎一起交流解決。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章