SpringBoot整合MyBatis Generator可以帮助我们快速生成实体类、接口、mapper.xml文件,可以提高开发的效率,但是每次新增表都要执行一次Generator生成相应的代码,这样重复的操作生成增删查改重复的的代码,有没有一种通用的接口,不用生成mapper.xml文件,就可以调用通用接口的方法,实现单表的增删改查操作呢,这就是tk.mybatis.mapper 支持单表操作,不支持通用的多表联合查询。
File–>New–>Project–>Spring Initializr 选择JDK版本初始化URL–>Next
填写项目名称(这里用mapper)及其他信息
选择项目依赖web、mybatis、mysql
填写项目保存路径
tk.mybatis
mapper-spring-boot-starter
2.0.2
注意:添加了mapper-spring-boot-starter就不用再添加mapper了
4.0.0
com.xyz
mapper
0.0.1-SNAPSHOT
jar
mapper
SpringBoot 整合 Mybatis 通用 Mapper
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
tk.mybatis
mapper-spring-boot-starter
2.0.2
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
添加controller、service、dao、baseDao、entity
package com.xyz.mapper.baseDao;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* @Description: 公用Mapper接口
* @Date: 2018/9/22 17:44
* @Author: xyz
*/
public interface IBaseDao extends Mapper,MySqlMapper {
}
注意:公用接口IBaseDao要单独的存在另一个包和普通dao分开,以免启动类的@MapperScan扫描到会报错
#端口配置
server:
port: 8088
#jdbc配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/video?useUnicode=true&characterEncoding=utf8
username: xyz
password: xyz
#mybatis配置
mybatis:
#实体类所在包名
type-aliases-package: com.xyz.mapper.entity
#通用mapper配置
mapper:
#公用接口类路径
mappers: com.xyz.mapper.baseDao.IBaseDao
identity: MYSQL
注意:mybatis.mapper-locations=classpath:mapper/*.xml就不用配置了,因为不生成mapper.xml文件
package com.xyz.mapper.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Repository;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@Repository
@Table(name = "t_manager")//mybatis通用接口mapper依赖JPA实体类采用JPA
public class TManager implements Serializable {
// 主键 自动递增
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
// 管理员编号
@Column(name = "manguid")
private String manguid;
// 姓名
@Column(name = "name")
private String name;
// 邮箱
@Column(name = "email")
private String email;
// 密码
@Column(name = "password")
private String password;
// 头像
@Column(name = "avatar")
private String avatar;
// 国籍
@Column(name = "country")
private String country;
// 省
@Column(name = "province")
private String province;
// 市
@Column(name = "city")
private String city;
// 注册时间 格式化
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name = "regtime")
private Date regtime;
// 注册IP
@Column(name = "regip")
private String regip;
// 状态
@Column(name = "status")
private Integer status;
private static final long serialVersionUID = 1L;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getManguid() {
return manguid;
}
public void setManguid(String manguid) {
this.manguid = manguid == null ? null : manguid.trim();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar == null ? null : avatar.trim();
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country == null ? null : country.trim();
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province == null ? null : province.trim();
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city == null ? null : city.trim();
}
public Date getRegtime() {
return regtime;
}
public void setRegtime(Date regtime) {
this.regtime = regtime;
}
public String getRegip() {
return regip;
}
public void setRegip(String regip) {
this.regip = regip == null ? null : regip.trim();
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", manguid=").append(manguid);
sb.append(", name=").append(name);
sb.append(", email=").append(email);
sb.append(", password=").append(password);
sb.append(", avatar=").append(avatar);
sb.append(", country=").append(country);
sb.append(", province=").append(province);
sb.append(", city=").append(city);
sb.append(", regtime=").append(regtime);
sb.append(", regip=").append(regip);
sb.append(", status=").append(status);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
注意:Mybatis通用接口mapper依赖JPA,所以实体类得用JPA建立对象和表的映射关系
package com.xyz.mapper.dao;
import com.xyz.mapper.baseDao.IBaseDao;
import com.xyz.mapper.entity.TManager;
/**
* @Description: 管理员通用接口继承公用接口,注意泛型
* @Date: 2018/9/22 18:35
* @Author: xyz
*/
public interface IManagerDao extends IBaseDao{
}
package com.xyz.mapper.service;
import com.xyz.mapper.entity.TManager;
import java.util.List;
/**
* @Description: 管理员服务层接口
* @Date: 2018/9/22 18:39
* @Author: xyz
*/
public interface IManagerService {
int deleteByPrimaryKey(Integer id);
int insert(TManager manager);
TManager selectByPrimaryKey(Integer id);
List selectAll();
int updateByPrimaryKey(TManager manager);
}
package com.xyz.mapper.service.impl;
import com.xyz.mapper.dao.IManagerDao;
import com.xyz.mapper.entity.TManager;
import com.xyz.mapper.service.IManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Description: 管理员实现层
* @Date: 2018/9/22 18:41
* @Author: xyz
*/
@Service
public class ManagerServiceImpl implements IManagerService {
@Autowired
private IManagerDao managerDao;
@Override
public int deleteByPrimaryKey(Integer id) {
int row = managerDao.deleteByPrimaryKey(id);
return row;
}
@Override
public int insert(TManager manager) {
return managerDao.insert(manager);
}
@Override
public TManager selectByPrimaryKey(Integer id) {
return managerDao.selectByPrimaryKey(id);
}
@Override
public List selectAll() {
return managerDao.selectAll();
}
@Override
public int updateByPrimaryKey(TManager manager) {
return managerDao.updateByPrimaryKey(manager);
}
}
package com.xyz.mapper.controller;
import com.xyz.mapper.entity.TManager;
import com.xyz.mapper.service.IManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Description: 管理员访问控制层
* @Date: 2018/9/22 18:46
* @Author: xyz
*/
@RestController
public class ManagerController {
@Autowired
private IManagerService managerService;
/**
* @Description: 添加管理员,返回添加数量
* @MethodName: insert
* @param: manager
* @ReturnType: int
*/
@PostMapping(value = "/manager/insert")
public int insert(TManager manager) {
return managerService.insert(manager);
}
/**
* @Description: 根据id删除管理员,返回删除数量
* @MethodName: deleteByPrimaryKey
* @param: id
* @ReturnType: int
*/
@PostMapping(value = "/manager/deleteByPrimaryKey")
public int deleteByPrimaryKey(@RequestParam(value = "id", required = true) Integer id) {
int row = managerService.deleteByPrimaryKey(id);
return row;
}
/**
* @Description: 根据id更改管理员信息, 返回更改数量
* @MethodName: updateByPrimaryKey
* @param: manager
* @ReturnType: int
*/
@PostMapping(value = "/manager/updateByPrimaryKey")
public int updateByPrimaryKey(TManager manager) {
return managerService.updateByPrimaryKey(manager);
}
/**
* @Description: 查询单个管理员对象
* @MethodName: selectByPrimaryKey
* @param: id
* @ReturnType: com.xyz.mapper.entity.TManager
*/
@PostMapping(value = "/manager/selectByPrimaryKey")
public TManager selectByPrimaryKey(Integer id) {
return managerService.selectByPrimaryKey(id);
}
/**
* @Description: 查询所有管理员不分页
* @MethodName: selectAll
* @param:
* @ReturnType: java.util.List
*/
@PostMapping(value = "/manager/selectAll")
public List selectAll() {
List managerList = managerService.selectAll();
return managerList;
}
}
package com.xyz.mapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.xyz.mapper.dao")//注意MapperScan要导tk的包,不然会报NoSuchMethodException
public class MapperApplication {
public static void main(String[] args) {
SpringApplication.run(MapperApplication.class, args);
}
}
Mybatis 通用接口Mapper虽然省去了手写SQL,但是Mapper依赖于JPA,需要手写模型用注解建立模型和表的映射关系,Mapper也局限于单表操作。