目录
前言
后端业务开发,每个表都要用到单表的 增删改查 等通用方法,而配置了通用Mapper可以极大的方便使用Mybatis单表的增删改查操作。
通用mapper配置
1、添加 maven :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--通用mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.5</version> </dependency> <!-- pagehelp --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency>
2、 Application 启动文件添加 MapperScan 注解
在springboot启动类添加 tk.mybatis 包下 MapperScan 注解
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.springboot.dao")
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
其中 com.springboot.dao 是 dao 层的路径。
3、 Model 添加注解
添加 Table 注解和 Id 注解,
Table id
例如下方的 User 实体:
@Table(name = "t_user")
public class User {
//主键
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增
private Integer id;
}
4、创建 MyMapper
import tk.mybatis.mapper.common.IdsMapper;
import tk.mybatis.mapper.common.Mapper;
public interface MyMapper<T> extends Mapper<T>, IdsMapper<T> {
}
需要实现的通用接口都写在 MyMapper 的继承类中,该类的包不能被 MapperScan 扫描到。
Mapper<T> IdsMapper<T>
5、每个 dao 继承步骤4的 MyMapper
例如 UserDao 继承 MyMapper<User> :
public interface UserDao extends MyMapper<User> {
}
通用service
上面配置只是调用dao层可以有默认的增删改查的方法,还是要在对应的service添加增删查改,所以需要写一个通用service,把公共的方法都抽象到一个基础方法中。
BaseService.java 接口:
public interface BaseService<T> {
/**
* 查询所有
*
* @return 返回所有数据
*/
List<T> selectAll();
/**
* 查询数据数量
* @return
*/
int selectCount();
/**
* 添加
*
* @param t 实体
*
* @return
*/
int save(T t);
/**
* 修改
*
* @param t
* 实体
* @return
*/
int updateByPrimaryKey(T t);
/**
* 根据主键删除
*
* @param t 主键
*
* @return
*/
int deleteByPrimaryKey(int t);
}
BaseServiceImpl 实现类:
public class BaseServiceImpl<T> implements BaseService<T> {
@Autowired
private MyMapper<T> mapper;
@Override
public List<T> selectAll() {
return mapper.selectAll();
}
@Override
public int selectCount() {
return mapper.selectCount(null);
}
@Override
public int save(T t) {
return mapper.insert(t);
}
@Override
public int updateByPrimaryKey(T t) {
return mapper.updateByPrimaryKey(t);
}
@Override
public int deleteByPrimaryKey(int t) {
return mapper.deleteByPrimaryKey(t);
}
}
所有的 service 和 serviceImpl 都分别继承 BaseService 和 BaseServiceImpl ,例如 UserService 和 UserServiceImpl 分别继承 BaseService 和 BaseServiceImpl :
public interface UserService extends BaseService<User>{
}
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService{
}
配置完成之后,在对应的 controller 调用,比如 UserController :
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/add")
public Object add(User user) {
userService.save(user);
return null;
}
@PostMapping("/delete")
public Object delete(@RequestParam Integer id) {
userService.deleteByPrimaryKey(id);
return null;
}
@PostMapping("/update")
public Object update(User user) {
userService.updateByPrimaryKey(user);
return null;
}
@GetMapping("/detail")
public User detail(@RequestParam Integer id) {
User user = userService.selectById(id);
return user;
}
@GetMapping("/list")
public List<User> list() {
List<User> list = userService.list();
return list;
}
}
总结
通用mapper:
- 创建SpringBoot启动文件添加
MapperScan,扫描dao层的包。 - 创建
MyMapper<T>接口,根据自己需求继承要用的接口,比如Mapper<T>。 - 每个dao接口继承
MyMapper<T>接口。
通用service
- 创建
BaseService接口。 BaseServiceImpl实现类,调用MyMapper<T>实现增删改查方法。- 每个
service接口和service实现类分别继承BaseService接口和BaseServiceImpl实现类。 - 每个
controller就能调用通用方法。
遇到的问题
1、启动报错
required a bean of type 'com.jeremy.data.utils.MyMapper' that could not be found.
没有找到
MyMapper对应的bean,无法注入。
解决方案:
1、 SpringBoot 启动文件添加 MapperScan 注解。
2、每个 dao 接口都要继承 MyMapper 。
以上两个步骤 缺一不可 。

