mybatis之通用mapper的使用(springBoot 整合)
2019-01-17 10:55
579 查看
-
什么是通用mapper
通用mapper 可以极大的方便开发人员进行ORM,提供极其方便的单表增删改查。
什么是通用mapper,一句话简单说,它就是个辅助mybatis极简单表开发的组件。它不是为了替代mybatis,而是让mybatis的开发更方便。
可以按照自己的需要选择通用方法,还能很方便的开发自己的通用方法。 -
怎么用通用mapper
首先导入通用mapper的依赖
<!-- 通用mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>4.0.0</version> </dependency>
在springBoot配置文件中配置通用mapper参数:
Yml
mapper: mappers: - tk.mybatis.mapper.common.Mapper - tk.mybatis.mapper.common.Mapper2 notEmpty: true
properties
mapper.mappers=tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.common.Mapper2 mapper.notEmpty=true
接着我们就可以在我们的Mapper层继承通用Mapper来使用它
package cn.bluethink.eguan.core.mapper; import cn.bluethink.eguan.core.entity.EgOtactionEntity; import tk.mybatis.mapper.common.Mapper; //通用mapper要一个实体类作为参数,作为数据库查询表 public interface EgOtactionMapper extends Mapper<EgOtactionEntity>{ }
让我们来看看通用mapper里都有什么方法,有什么作用
package tk.mybatis.mapper.common; /** * 通用Mapper接口,其他接口继承该接口即可 * <p/> * <p>这是一个例子,自己扩展时可以参考</p> * <p/> * <p>项目地址 : <a href="https://github.com/abel533/Mapper" target="_blank">https://github.com/abel533/Mapper</a></p> * * @param <T> 不能为空 * @author liuzh */ @tk.mybatis.mapper.annotation.RegisterMapper public interface Mapper<T> extends BaseMapper<T>, ExampleMapper<T>, RowBoundsMapper<T>, Marker { }
再看看mapper中四个方法中各自又有什么方法,首先是BaseMapper
package tk.mybatis.mapper.common; import tk.mybatis.mapper.common.base.BaseDeleteMapper; import tk.mybatis.mapper.common.base.BaseInsertMapper; import tk.mybatis.mapper.common.base.BaseSelectMapper; import tk.mybatis.mapper.common.base.BaseUpdateMapper; /** * 通用Mapper接口,其他接口继承该接口即可 * <p/> * <p>这是一个例子,自己扩展时可以参考</p> * <p/> * <p>项目地址 : <a href="https://github.com/abel533/Mapper" target="_blank">https://github.com/abel533/Mapper</a></p> * * @param <T> 不能为空 * @author liuzh */ @tk.mybatis.mapper.annotation.RegisterMapper public interface BaseMapper<T> extends BaseSelectMapper<T>, BaseInsertMapper<T>, BaseUpdateMapper<T>, BaseDeleteMapper<T> { }
通过名字就知道是基本的CURD,我们点进去BaseSelectMapper
package tk.mybatis.mapper.common.base; import tk.mybatis.mapper.annotation.RegisterMapper; import tk.mybatis.mapper.common.base.select.*; /** * 通用Mapper接口,基础查询 * * @param <T> 不能为空 * @author liuzh */ @RegisterMapper public interface BaseSelectMapper<T> extends SelectOneMapper<T>, SelectMapper<T>, SelectAllMapper<T>, SelectCountMapper<T>, SelectByPrimaryKeyMapper<T>, ExistsWithPrimaryKeyMapper<T> { }
点进去BaseInsertMapper
package tk.mybatis.mapper.common.base; import tk.mybatis.mapper.annotation.RegisterMapper; import tk.mybatis.mapper.common.base.insert.InsertMapper; import tk.mybatis.mapper.common.base.insert.InsertSelectiveMapper; /** * 通用Mapper接口,基础查询 * * @param <T> 不能为空 * @author liuzh */ @RegisterMapper public interface BaseInsertMapper<T> extends InsertMapper<T>, InsertSelectiveMapper<T> { }
点进去BaseUpdateMapper
package tk.mybatis.mapper.common.base; import tk.mybatis.mapper.annotation.RegisterMapper; import tk.mybatis.mapper.common.base.update.UpdateByPrimaryKeyMapper; import tk.mybatis.mapper.common.base.update.UpdateByPrimaryKeySelectiveMapper; /** * 通用Mapper接口,基础查询 * * @param <T> 不能为空 * @author liuzh */ @RegisterMapper public interface BaseUpdateMapper<T> extends UpdateByPrimaryKeyMapper<T>, UpdateByPrimaryKeySelectiveMapper<T> { }
点进去BaseDeleteMapper
package tk.mybatis.mapper.common.base; import tk.mybatis.mapper.annotation.RegisterMapper; import tk.mybatis.mapper.common.base.delete.DeleteByPrimaryKeyMapper; import tk.mybatis.mapper.common.base.delete.DeleteMapper; /** * 通用Mapper接口,基础删除 * * @param <T> 不能为空 * @author liuzh */ @RegisterMapper public interface BaseDeleteMapper<T> extends DeleteMapper<T>, DeleteByPrimaryKeyMapper<T> { }
我们回到通用mapper的四个方法中点进去ExampleMapper看一下有什么方法
package tk.mybatis.mapper.common; import tk.mybatis.mapper.common.example.*; /** * 通用Mapper接口,Example查询 * * @param <T> 不能为空 * @author liuzh */ @tk.mybatis.mapper.annotation.RegisterMapper public interface ExampleMapper<T> extends SelectByExampleMapper<T>, SelectOneByExampleMapper<T>, SelectCountByExampleMapper<T>, DeleteByExampleMapper<T>, UpdateByExampleMapper<T>, UpdateByExampleSelectiveMapper<T> { }
再看看RowBoundsMapper里有什么方法
package tk.mybatis.mapper.common; import tk.mybatis.mapper.common.rowbounds.SelectByExampleRowBoundsMapper; import tk.mybatis.mapper.common.rowbounds.SelectRowBoundsMapper; /** * 通用Mapper接口,带RowBounds参数的查询 * <p/> * 配合分页插件PageHelper可以实现物理分页 * <p/> * PageHelper - http://git.oschina.net/free/Mybatis_PageHelper * * @param <T> 不能为空 * @author liuzh */ @tk.mybatis.mapper.annotation.RegisterMapper public interface RowBoundsMapper<T> extends SelectByExampleRowBoundsMapper<T>, SelectRowBoundsMapper<T> { }
还有一个Mark接口:标记接口,继承该接口的接口,在MapperScannerConfigurer#setMarkerInterface时,会自动注册到通用Mapper
以上是通用mapper里的所有方法,对单表操作能力非常强
需要注意的是我们通用mapper的参数实体类:
- 类属性要保持和数据库字段名一致,不一致的要用@Column(name=“数据库字段名”)注解该字段
- 实体类上要用@Table(name=“表名”)
- 表字段是主键,实体类属性要用@Id
相关文章推荐
- 在idea里使用SpringBoot整合MyBatis时遇到的Mapper扫描不到的问题
- SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper实例详解
- Mybatis通用Mapper(springboot环境下使用)
- 文章标题Spring Boot干货系列:(十一)数据存储篇-Spring Boot整合Mybatis通用Mapper插件
- Spring Boot 学习笔记(十一)——整合MyBatis通用Mapper插件
- Springboot 2.0 + SSM + druid + 通用Mapper插件 + lombck +mybatis-generator-lombok-plugin 整合范例
- 二十一、SpringBoot整合Mybatis、通用mapper和pageHelp
- springboot中mybatis通用mapper以及代码生成工具使用
- spring boot使用通用mapper(tk.mapper) ,id自增和回显等问题
- (十一)Spring Boot整合Mybatis使用druid实现多数据源自动切换
- SpringBoot整合Mybatis使用Druid数据库连接池
- Spring Boot集成Mybatis及通用Mapper
- 关于spring boot整合mybatis使用oracle数据库出现could not load:oracle.jdbc.driver.OracleDriver问题的终极解决方案
- SpringBoot整合Mybatis扫描不到Mapper的问题
- 如何整合Spring和Mybatis的思路分析(使用面向接口的mapper代理,不用再去写Dao的实现类)
- Spring Boot整合mybatis使用
- Spring Boot MyBatis 通用Mapper插件集成
- SpringBoot + Mybatis + Mapper + PageHelper + Spring Batch 整合
- 关于Spring Boot集成MyBatis、通用Mapper、PageHelper
- spring Boot 整合mybatis 如何使用多数据源