MyBatis-通用Mapper-tk.mybatis的使用

MyBatis-通用Mapper[更新中]

tk.mybatis的使用

前言

使用MyBatis開發,如果是普通是同MyBatis進行開發,那麼就需要在xml文件中編寫大量的SQL。當資料庫表結構發生改動時,對應的所有的SQL及其實體類都需要更改,這樣開發的效率就有點低。

什麼是Mapper

為了解決這個問題,使用通用Mapper;通用Mapper基於MyBatis的插件,開發人員不需要編寫SQL,不需要在DAO中增加對應的方法,只要寫好實體類就能支援自動添加響應的增刪查改方法。

如何使用Mapper

在Spring Boot 中使用tk.mapper:

Maven依賴:在Maven工程的commom子工程中設置。

<!-- 通用Mapper插件 -->
 <dependency>
      <groupId>tk.mybatis</groupId>
      <artifactId>mapper-spring-boot-starter</artifactId>
      <version>2.1.5</version>
 </dependency>

逆向工程

使用逆向工程創建資料庫對應的實體類pojo、mapper下的xml文件。

–見另外的文章–

mapper繼承了tk.mapper後,便擁有了Mapper的所有通用方法

Mapper通用SQL

Select

方法:List<T> select(T record);
說明:根據實體中的屬性值進行查詢,查詢條件使用等號

方法:T selectByPrimaryKey(Object key);
說明:根據主鍵欄位進行查詢,方法參數必須包含完整的主鍵屬性,查詢條件使用等號

方法:List<T> selectAll();
說明:查詢全部結果,select(null)方法能達到同樣的效果

方法:T selectOne(T record);
說明:根據實體中的屬性進行查詢,只能有一個返回值,有多個結果是拋出異常,查詢條件使用等號

方法:int selectCount(T record);
說明:根據實體中的屬性查詢總數,查詢條件使用等號

Insert

方法:int insert(T record);
說明:保存一個實體,null的屬性也會保存,不會使用資料庫默認值

方法:int insertSelective(T record);
說明:保存一個實體,null的屬性不會保存,會使用資料庫默認值

Update

方法:int updateByPrimaryKey(T record);
說明:根據主鍵更新實體全部欄位,null值會被更新

方法:int updateByPrimaryKeySelective(T record);
說明:根據主鍵更新屬性不為null的值

Delete

方法:int delete(T record);
說明:根據實體屬性作為條件進行刪除,查詢條件使用等號

方法:int deleteByPrimaryKey(Object key);
說明:根據主鍵欄位進行刪除,方法參數必須包含完整的主鍵屬性

Example方法

方法:List<T> selectByExample(Object example);
說明:根據Example條件進行查詢
重點:這個查詢支援通過Example類指定查詢列,通過selectProperties方法指定查詢列

方法:int selectCountByExample(Object example);
說明:根據Example條件進行查詢總數

方法:int updateByExample(@Param("record") T record, @Param("example") Object example);
說明:根據Example條件更新實體record包含的全部屬性,null值會被更新

方法:int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
說明:根據Example條件更新實體record包含的不是null的屬性值

方法:int deleteByExample(Object example);
說明:根據Example條件刪除數據

如何使用

vo類:實體類,負責傳輸數據。這裡以工程中的 renderSixNewItems() 方法為例,方法的功能是,查詢類別下的6個items。由於Mapper提供的SQL方法是不足的,所以需要使用Example來配合使用,搭建複雜的SQL。

    @Override
    public SixNewItemVo renderSixNewItems(Integer rootCatId){
        // category 獲取實體類
        // categoryMapper.selectByPrimaryKey(rootCatId); 通過主鍵從資料庫中查詢該實體類
        Category category = categoryMapper.selectByPrimaryKey(rootCatId);
        //SixNewItemVo -- vo -- value object 值對象 用於業務層之間的數據傳輸。僅僅是包含數據,就是提取實體類的部分數據進行傳輸。
        SixNewItemVo sixNewItemVo = new SixNewItemVo();
        sixNewItemVo.setRootCatName(category.getName());
        sixNewItemVo.setSlogan(category.getSlogan());
        sixNewItemVo.setBgColor(category.getBgColor());
        sixNewItemVo.setCatImage(category.getCatImage());
        //獲取到 getName、getSlogan、getBgColor、getCatImage
        /**
         * 實體類
         * 使用Example來實現其他SQL
         * 當前的需求是:根據category的rootCatId來查詢同一類別下的items,即一個大類下的商品
         * */
        //example 使用Items的類名,即表示我這個example是為Items服務的,接收的數據也就是Items類
        Example example = new Example(Items.class);
        //createCriteria 創建查詢條件、 andEqualTo 某個列等於***
        example.createCriteria().andEqualTo("rootCatId",rootCatId);

        System.out.println("id : " + rootCatId);
        //排序
        example.orderBy("sellCounts").desc();
        PageHelper.startPage(1,6);

        //把example作為查詢條件放入Mapper提供的selectByExample查詢
        //items 為查詢結果
        List<Items> items = itemsMapper.selectByExample(example);
        List<ItemsVo> itemsVos = new ArrayList<>();

        for (Items item : items) {
            ItemsVo itemsVo = new ItemsVo();
            itemsVo.setItemId(item.getId());
            itemsVo.setItemName(item.getItemName());


            ItemsImg itemsImg = new ItemsImg();
            itemsImg.setItemId(item.getId());
            itemsImg.setIsMain(1);

            List<ItemsImg> itemsImgs = itemsImgMapper.select(itemsImg);
            itemsVo.setItemUrl(itemsImgs.get(0).getUrl());
            itemsVos.add(itemsVo);
            System.out.println("itemsVo_id : " + itemsVo.getItemId());
        }

        sixNewItemVo.setItemsVoList(itemsVos);
        return sixNewItemVo;
    }

引用鏈接:

Mybatis通用Mapper(tk.mybatis)的使用://blog.csdn.net/lijingjingchn/article/details/84819536

Java 的 VO類: //blog.csdn.net/jikefzz1095377498/article/details/79237618

Tags: