Spring Boot 使用Mybatis 通用 Mapper

Mybatis 通用 Mapper

對單表crud來說,我們不需要每次都去寫一套增刪改查接口,直接複用一套代碼即可,這次通用Mapper插件就派上用場了。
支持 Mybatis-3.2.4 及以上版本

引入依賴

  <!--通用mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
            <exclusions>
            <!-- 排除jdbc依賴-->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

集成通用Mapper

繼承Mapper 即可,T是對應的實體類
在這裏插入圖片描述

存在缺點

1 增加數據的時候,返回主鍵時候存在問題,需要在每個實體類中配置一下主鍵返回策略

通常我們在mybatis中是這樣子寫的主鍵返回

   <insert id="insert" parameterType="com.gmalluser.pojo.UmsMember" keyProperty="id" useGeneratedKeys="true" keyColumn="id">

但現在用了通用mapper 需要配置主鍵返回策略

解決辦法

 @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;//主鍵

2 使用Spring Boot 時候掃描 mapper要使用通用Mapper的MapperScan 掃描器

注意是tk所在的包

@tk.mybatis.spring.annotation.MapperScan("com.gmalluser.dao")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章