您的位置:首页 > 编程语言 > Java开发

mybatis之通用mapper的使用(springBoot 整合)

2019-01-17 10:55 579 查看
  1. 什么是通用mapper
    通用mapper 可以极大的方便开发人员进行ORM,提供极其方便的单表增删改查。
    什么是通用mapper,一句话简单说,它就是个辅助mybatis极简单表开发的组件。它不是为了替代mybatis,而是让mybatis的开发更方便。
    可以按照自己的需要选择通用方法,还能很方便的开发自己的通用方法。

  2. 怎么用通用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的参数实体类:

  1. 类属性要保持和数据库字段名一致,不一致的要用@Column(name=“数据库字段名”)注解该字段
  2. 实体类上要用@Table(name=“表名”)
  3. 表字段是主键,实体类属性要用@Id
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: