一、为什么要用通用Mapper?
这段实时间自己在空余时间,改变了一下SpringBoot项目中使用mybatis的方式。之前一直都是使用的mybatis-generator插件生成一大堆Mapper相关的java文件和xml文件,虽然都是自动生成的,但总觉得不够干净,于是便想着做点什么——使用通用Mapper。
通用Mapper(tk.mybatis)封装了数据库表基本的CRUD功能,我们可以直接在业务方法中进行调用。使用之后,再也看不到那么多大同小异的自动生成的mapper相关文件。当然了,因为我们使用的数据库持久化框架是mybatis,很多时候,我们需要自定义查询,那么定义相关的java文件和xml文件是必须的。
集成的过程中,并不太顺利,网上找了各种资料,很混乱。所以自己在这里总结一下,同大家分享。
二、集成步骤
1.添加相关依赖
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
tk.mybatis
mapper-spring-boot-starter
2.0.4
2.自定义Mapper继承通用Mapper
package com.gui.star.dal.common;
import tk.mybatis.mapper.common.Mapper;
public interface CommonMapper extends Mapper {
}
3.所有自己的xxxMapper.java继承第2步定义的Mapper
package com.gui.star.dal.mapper;
import com.gui.star.dal.common.CommonMapper;
import com.gui.star.dal.model.User;
public interface UserMapper extends CommonMapper{
}
4.定义通用的service使用封装好的CRUD操作
package com.gui.star.biz.common;
import java.util.List;
public interface BaseService {
List findAll();
T findById(Long id);
int save(T t);
int update(T t);
int remove(Long id);
}
package com.gui.star.biz.common;
import java.util.List;
import com.gui.star.dal.common.CommonMapper;
public abstract class BaseServiceImpl implements BaseService {
public abstract CommonMapper getMapper();
@Override
public List findAll() {
return getMapper().selectAll();
}
@Override
public T findById(Long id) {
return getMapper().selectByPrimaryKey(id);
}
@Override
public int save(T t) {
return getMapper().insert(t);
}
@Override
public int remove(Long id) {
return getMapper().deleteByPrimaryKey(id);
}
@Override
public int update(T t) {
return getMapper().updateByPrimaryKey(t);
}
}
5.业务service继承通用的BaseService
package com.gui.star.biz.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gui.star.biz.common.BaseServiceImpl;
import com.gui.star.biz.service.UserService;
import com.gui.star.biz.vo.UserVo;
import com.gui.star.common.form.UserForm;
import com.gui.star.common.util.DomainUtil;
import com.gui.star.common.util.ObjectUtil;
import com.gui.star.dal.common.CommonMapper;
import com.gui.star.dal.mapper.UserMapper;
import com.gui.star.dal.model.User;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class UserServiceImpl extends BaseServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public CommonMapper getMapper() {
return this.userMapper;
}
}
6.配置Mapper扫描已经修改配置文件
启动类中增加MapperScan注解扫描我们的Mapper
package com.gui.star.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import tk.mybatis.spring.annotation.MapperScan;
/**
* SpringBoot启动类
*
*
*/
@SpringBootApplication
@ComponentScan(basePackages = "com.gui")
@MapperScan(basePackages = "com.gui.star.dal.mapper")
@EnableAutoConfiguration
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
注意:这里的MapperScan对应导入的包是tk.mybatis.spring.annotation.MapperScan。
application.yml配置文件中mybatis的配置:
mybatis:
mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.gui.star.dal.model # 注意:对应实体类的路径
7.对应的实体类
package com.gui.star.dal.model;
import java.util.Date;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
public class User {
/**
* 标识主键
* 定义ID的生成规则,每次保存后返回ID
*/
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String loginname;
private String password;
private String email;
private String phone;
private Date createdAt;
private Date updatedAt;
private Byte deleted;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname == null ? null : loginname.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
public Byte getDeleted() {
return deleted;
}
public void setDeleted(Byte deleted) {
this.deleted = deleted;
}
}