Mybatis的通用Mapper(实例)与Spring Data JPA的区别:

一:概念

通用mapper:一般在项目中用来简化mybatis对单表的操作,在功能上与Spring Data JPA有点相似.

二:疑问:使用通用mapper会降低代码运行效率吗?

(1)这个东西使用后,会不会降低我们的代码执行效率呢?

答:其实他的原理就是利用反射机制拼出的
XML形式的动态SQL然后去执行,所以你说代码时间消耗肯定也会有一些,但是这是很小很小的,再者在现在项目中,对于提供给前端的接口,通常都会利用些组件进行访问加速(毕竟直接从数据中检索不如在内存中来的快些),比如:redis、memcache、elastic
search、solr等,所以执行效率问题是可以避免或忽略的。

(2)如果有些方法不想使用,例如:用户的接口服务不能使用删除方法也不想暴漏出来,该怎么办呢?

答:我们通常会定义增、删、改、查四个基础Mapper接口,你可以按需要引入进行使用,其实即使你使用了公共的CrudMapper(代表增删改查都在一起的类)也没有问题,只要你在你的service层不要放出删除方法也是可以的。

三:示例 在分布式项目中

1:在api的位置引入依赖

<dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
        </dependency>

2:在service模块application.yml文件中配置,当然service的pom文件中肯定引入了api

mybatis:
  configuration:
    #通用mapper开启驼峰匹配
    map-underscore-to-camel-case: true      camel驼峰的意思
    #Mapper文件位置
  mapper-locations: classpath:mapper/*Mapper.xml
   #别名扫描
  type-aliases-package: com.shangcheng.goods.pojo

3:编写实体类:在这里的注解与 Spring Data JPA 有些相似

@Table(name="tb_brand")  //映射表名
public class Brand implements Serializable {

	@Id    //主键
	private Integer id;//品牌id
	private String name;//品牌名称
	private String image;//品牌图片地址
	private String letter;//品牌的首字母
	private Integer seq;//排序

4:编写Dao层接口,需要继承Mapper<实体类>,单表操作,不需要写具体的dao层方法,Mapper里已经定义好了,与Spring Data JPA中的JpaRepository相似.

public interface BrandMapper extends Mapper<Brand> {

5:编写Service 与实现类,在启动类上配置开启注解@MapperScan(basePackages = {“com.dianshang.goods.dao”})

四:通用mapper的方法:

Mapper3接口有两种形式,一种是提供了一个方法的接口。还有一种是不提供方法,但是继承了多个单方法的接口,一般是某类方法的集合。
例如SelectMapper是一个单方法的接口,BaseSelectMapper是一个继承了4个基础查询方法的接口。

基础接口 Select
接口:SelectMapper
方法:List select(T record);

说明:根据实体中的属性值进行查询,查询条件使用等号

接口:SelectByPrimaryKeyMapper
方法:T selectByPrimaryKey(Object key);

说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号

接口:SelectAllMapper
方法:List selectAll();

说明:查询全部结果,select(null)方法能达到同样的效果

接口:SelectOneMapper
方法:T selectOne(T record);

说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号

接口:SelectCountMapper
方法:int selectCount(T record);

说明:根据实体中的属性查询总数,查询条件使用等号

基础接口Insert
接口:InsertMapper
方法:int insert(T record);

说明:保存一个实体,null的属性也会保存,不会使用数据库默认值

接口:InsertSelectiveMapper
方法:int insertSelective(T record);

说明:保存一个实体,null的属性不会保存,会使用数据库默认值

基础接口Update
接口:UpdateByPrimaryKeyMapper
方法:int updateByPrimaryKey(T record);

说明:根据主键更新实体全部字段,null值会被更新

接口:UpdateByPrimaryKeySelectiveMapper
方法:int updateByPrimaryKeySelective(T record);

说明:根据主键更新属性不为null的值

基础接口Delete
接口:DeleteMapper
方法:int delete(T record);

说明:根据实体属性作为条件进行删除,查询条件使用等号

接口:DeleteByPrimaryKeyMapper
方法:int deleteByPrimaryKey(Object key);

说明:根据主键字段进行删除,方法参数必须包含完整的主键属性

Base组合接口
接口:BaseSelectMapper
方法:包含上面Select的4个方法

接口:BaseInsertMapper
方法:包含上面Insert的2个方法

接口:BaseUpdateMapper
方法:包含上面Update的2个方法

接口:BaseDeleteMapper
方法:包含上面Delete的2个方法

CURD组合接口
接口:BaseMapper
方法:继承了base组合接口中的4个组合接口,包含完整的CRUD方法

Example方法
接口:SelectByExampleMapper
方法:List selectByExample(Object example);

说明:根据Example条件进行查询 重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列

接口:SelectCountByExampleMapper
方法:int selectCountByExample(Object example);

说明:根据Example条件进行查询总数

接口:UpdateByExampleMapper
方法:int updateByExample(@Param(“record”) T record, @Param(“example”) Object example);

说明:根据Example条件更新实体record包含的全部属性,null值会被更新

接口:UpdateByExampleSelectiveMapper
方法:int updateByExampleSelective(@Param(“record”) T record, @Param(“example”) Object example);

说明:根据Example条件更新实体record包含的不是null的属性值

接口:DeleteByExampleMapper
方法:int deleteByExample(Object example);

说明:根据Example条件删除数据

Example组合接口
接口:ExampleMapper
方法:包含上面Example中的5个方法

Condition方法
Condition方法和Example方法作用完全一样,只是为了避免Example带来的歧义,提供的的Condition方法

接口:SelectByConditionMapper
方法:List selectByCondition(Object condition);

说明:根据Condition条件进行查询

接口:SelectCountByConditionMapper
方法:int selectCountByCondition(Object condition);

说明:根据Condition条件进行查询总数

接口:UpdateByConditionMapper
方法:int updateByCondition(@Param(“record”) T record, @Param(“example”) Object condition);

说明:根据Condition条件更新实体record包含的全部属性,null值会被更新

接口:UpdateByConditionSelectiveMapper
方法:int updateByConditionSelective(@Param(“record”) T record, @Param(“example”) Object condition);

说明:根据Condition条件更新实体record包含的不是null的属性值

接口:DeleteByConditionMapper
方法:int deleteByCondition(Object condition);

说明:根据Condition条件删除数据

Condition组合接口
接口:ConditionMapper
方法:包含上面Condition中的5个方法

RowBounds
默认为内存分页,可以配合PageHelper实现物理分页

接口:SelectRowBoundsMapper
方法:List selectByRowBounds(T record, RowBounds rowBounds);

说明:根据实体属性和RowBounds进行分页查询

接口:SelectByExampleRowBoundsMapper
方法:List selectByExampleAndRowBounds(Object example, RowBounds rowBounds);

说明:根据example条件和RowBounds进行分页查询

接口:SelectByConditionRowBoundsMapper
方法:List selectByConditionAndRowBounds(Object condition, RowBounds rowBounds);

说明:根据example条件和RowBounds进行分页查询,该方法和selectByExampleAndRowBounds完全一样,只是名字改成了Condition

RowBounds组合接口
接口:RowBoundsMapper
方法:包含上面RowBounds中的前两个方法,不包含selectByConditionAndRowBounds

Special特殊接口
这些接口针对部分数据库设计,不是所有数据库都支持

接口:InsertListMapper
方法:int insertList(List recordList);

说明:批量插入,支持批量插入的数据库可以使用,例如MySQL,H2等,另外该接口限制实体包含id属性并且必须为自增列

接口:InsertUseGeneratedKeysMapper
方法:int insertUseGeneratedKeys(T record);

说明:插入数据,限制为实体包含id属性并且必须为自增列,实体配置的主键策略无效

MySQL专用
接口:MySqlMapper
继承方法:int insertList(List recordList);
继承方法:int insertUseGeneratedKeys(T record);

说明:该接口不包含方法,继承了special中的InsertListMapper和InsertUseGeneratedKeysMapper

SQLServer专用
由于sqlserver中插入自增主键时,不能使用null插入,不能在insert语句中出现id。
注意SqlServer的两个特有插入方法都使用了
@Options(useGeneratedKeys = true, keyProperty = “id”)
这就要求表的主键为id,且为自增,如果主键不叫id可以看高级教程中的解决方法。
另外这俩方法和base中的插入方法重名,不能同时存在!
如果某种数据库和SqlServer这里类似,也可以使用这些接口(需要先测试)。
经测试,PostgreSQL可以采用。

接口:InsertMapper
方法:int insert(T record);

说明:插入数据库,null值也会插入,不会使用列的默认值

接口:InsertSelectiveMapper
方法:int insertSelective(T record);

说明:插入数据库,null的属性不会保存,会使用数据库默认值

接口:SqlServerMapper

说明:这是上面两个接口的组合接口。

**

Mapper接口 接口:Mapper 该接口兼容Mapper2.x版本,继承了BaseMapper, ExampleMapper,
RowBoundsMapper三个组合接口。

**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值