SpringBoot2.0系列教程(八)Springboot框架集成通用 Mapper功能

Hello大家好,本章我们集成通用 Mapper功能 。有问题可以联系我mr_beany@163.com。另求各路大神指点,感谢

一:什么是通用 Mapper

通用 Mapper 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及Example 相关的单表操作。通用 Mapper 是为了解决 MyBatis 使用中 90% 的基本操作,使用它可以很方便的进行开发,可以节省开发人员大量的时间。

二:添加mapper依赖

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

三:修改MybatisConfigurer配置文件

这里我们重新配置MapperScannerConfigurer

打开MybatisConfigurer 

将import org.mybatis.spring.mapper.MapperScannerConfigurer;修改为

import tk.mybatis.spring.mapper.MapperScannerConfigurer;

四:创建通用mapper,service,serviceImpl

创建core→universal文件夹

创建以下文件

1、Mapper

package com.example.demo.core.universal;

import tk.mybatis.mapper.common.BaseMapper;
import tk.mybatis.mapper.common.ConditionMapper;
import tk.mybatis.mapper.common.IdsMapper;
import tk.mybatis.mapper.common.special.InsertListMapper;

/**
 * @Description: MyBatis Mapper插件接口
 * @author 张瑶
 * @date 2018/4/22 21:15
 */
public interface Mapper<T> extends BaseMapper<T>, ConditionMapper<T>, IdsMapper<T>, InsertListMapper<T> {
}

2、Service

package com.example.demo.core.universal;

import org.apache.ibatis.exceptions.TooManyResultsException;
import tk.mybatis.mapper.entity.Condition;

import java.util.List;

/**
 * @author 张瑶
 * @Description: Service层基础接口,其他Service接口请继承该接口
 * @date 2018/4/18 11:25
 */
public interface Service<T> {

    /**
     * @param model
     * @Description: 持久化
     * @Reutrn Integer
     */
    Integer insert(T model);

    /**
     * @param id
     * @Description: 通过主鍵刪除
     * @Reutrn Integer
     */
    Integer deleteById(String id);

    /**
     * @param ids
     * @Description: 批量刪除 eg:ids -> “1,2,3,4”
     * @Reutrn Integer
     */
    Integer deleteByIds(String ids);

    /**
     * @param model
     * @Description: 更新
     * @Reutrn Integer
     */
    Integer update(T model);

    /**
     * @param id
     * @Description: 通过ID查找
     * @Reutrn T
     */
    T selectById(String id);

    /**
     * @param fieldName
     * @param value
     * @throws TooManyResultsException
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值