@mapper注解的使用_SpringBoot-Mybatis通用mapper使用

mybatis是一个很好用的工具,但是编写mapper是一件很麻烦的事,自mybatis 3.0开始可以使用注解的方式,极大的简化了xml的编写量,本地想看看mybatis源码,自己扩展写一个工具,在阅读源码过程中发现一个通用mapper的工具包,感觉不用重复造轮子了,简要记录一下spring boot整合通用mapper的使用。

v2-9279888ba590ef006b612450bbb01c86_b.jpg
  1. 确保可以正常使用mybatis

  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执行的比较早,所以必须有下面的注解
    @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类,该类不能被当做普通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. 业务处理dao层,扩展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等很多方法,需要你进一步探索

  7. 主键id问题

    当使用insert,insertSelective等方法时,希望返回由数据库产生的逐渐,需要在实体类上增加注解

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

    generator="JDBC"表示 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键 ,适用于MySQL,SQL Server等的自增主键。

    或者:

    @Id
    @KeySql(useGeneratedKeys = true)
    private Long id;
  8. 如果实体字段和数据库字段不一致,可以使用@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. 更多细节参见wiki

通用Mapper极大的简化了xml文件的编写,但仍需要少许xml文件,有待进一步优化。同时因为这是一个个人项目,使用不太熟悉不建议使用。

本文由 歧途老农 创作,采用 CC BY 4.0 CN 协议 进行许可。 可自由转载、引用,但需署名作者且注明文章出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值