SpringBoot-Mybatis通用mapper使用

mybatis是一個(gè)很好用的工具,但是編寫mapper是一件很麻煩的事,自mybatis 3.0開始可以使用注解的方式,極大的簡化了xml的編寫量,本地想看看mybatis源碼,自己擴(kuò)展寫一個(gè)工具,在閱讀源碼過程中發(fā)現(xiàn)一個(gè)通用mapper的工具包,感覺不用重復(fù)造輪子了,簡要記錄一下spring boot整合通用mapper的使用。

202005251726LljqF400
  1. 確??梢哉J褂胢ybatis

  2. pom引入依賴包,starter需要配合@Mapper注解使用,這里采用這種方式,或者使用@MapperScan注解,@tk.mybatis.spring.annotation.MapperScan(basePackages = "掃描包")配合原生mapper使用。

    <dependency>
      <groupId>tk.mybatis</groupId>
      <artifactId>mapper-spring-boot-starter</artifactId>
      <version>{version}</version>
    </dependency>
    

    我使用的版本是2.0.2

  3. Mybatis 掃描配置(Deprecated, spring 自動配置)

    @Configuration
    //TODO 注意,由于MapperScannerConfigurer執(zhí)行的比較早,所以必須有下面的注解
    @AutoConfigureAfter(MybatisAutoConfiguration.class)
    public class MyBatisMapperScannerConfig {
        @Bean
        public MapperScannerConfigurer mapperScannerConfigurer() {
            MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
            mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
            mapperScannerConfigurer.setBasePackage("org.springboot.sample.mapper");
            Properties properties = new Properties();
            // 這里要特別注意,不要把MyMapper放到 basePackage 中,也就是不能同其他Mapper一樣被掃描到。 
            properties.setProperty("mappers", MyMapper.class.getName());
            properties.setProperty("notEmpty", "false");
            properties.setProperty("IDENTITY", "MYSQL");
            mapperScannerConfigurer.setProperties(properties);
            return mapperScannerConfigurer;
        }
    }
    
  4. 新建BaseMapper類,該類不能被當(dāng)做普通Mapper一樣被掃描 ,不加@Mapper注解,或者放在不同文件夾

    package com.zj.mapper;
    
    import tk.mybatis.mapper.common.Mapper;
    import tk.mybatis.mapper.common.MySqlMapper;
    
    public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T> {
    }
    
  5. 業(yè)務(wù)處理dao層,擴(kuò)展BaseMapper

    package com.zj.mapper;
    
    import com.zj.model.OrderInfo;
    import org.apache.ibatis.annotations.Mapper;
    
    @Mapper
    public interface OrderInfoMapper extends BaseMapper<OrderInfo> {}
    
  6. 其他和使用普通mybatis一致,service層部分代碼

    orderInfoMapper.insertSelective(info);
    OrderInfo info = orderInfoMapper.selectByPrimaryKey(id);
    

    通用mapper提供常用的一些操作方法: deleteByPrimaryKey, insert, insertSelective, selectByPrimaryKey, updateByPrimaryKeySelective, updateByPrimaryKey, insertList等很多方法,需要你進(jìn)一步探索????

  7. 主鍵id問題

    當(dāng)使用insert,insertSelective等方法時(shí),希望返回由數(shù)據(jù)庫產(chǎn)生的逐漸,需要在實(shí)體類上增加注解

    @Id
    @GeneratedValue(generator = "JDBC")
    private Long orderInfoId;
    

    generator="JDBC"表示 MyBatis 使用 JDBC 的 getGeneratedKeys 方法來取出由數(shù)據(jù)庫內(nèi)部生成的主鍵 ,適用于MySQL,SQL Server等的自增主鍵。

    或者:

    @Id
    @KeySql(useGeneratedKeys = true)
    private Long id;
    
  8. 如果實(shí)體字段和數(shù)據(jù)庫字段不一致,可以使用@Column注解,其他注解 參見注解

     @Column(name="SCORE_SUM")
     private String sumScore;
    
  9. MBG生成參見https://github.com/abel533/Mapper/wiki/4.1.mappergenerator,demo見 git@github.com:silloy/mybatis-generator.git

  10. 更多細(xì)節(jié)參見wiki

通用Mapper極大的簡化了xml文件的編寫,但仍需要少許xml文件,有待進(jìn)一步優(yōu)化。同時(shí)因?yàn)檫@是一個(gè)個(gè)人項(xiàng)目,使用不太熟悉不建議使用。

本文由 歧途老農(nóng) 創(chuàng)作,采用 CC BY 4.0 CN 協(xié)議 進(jìn)行許可。 可自由轉(zhuǎn)載、引用,但需署名作者且注明文章出處。如轉(zhuǎn)載至微信公眾號,請?jiān)谖哪┨砑幼髡吖娞柖S碼。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容