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