MyBatis Plus最底层的就是封装工具代码,具有以下功能:
1.根据数据库的表动态生成对应mapper层、service层、controller层代码
2.mapper层代码自动带有对应的表单的接口方法,并且底层根据实体类来逆向生成对应的基本sql语句,除了复杂的sql歪,无需声明mapper配置文件
3.service层的代码自动带有对应的基本业务操作
4.controller层的代码创建对应的控制器类
5.pojo层自动生成表对应的实体类
开发环境的搭建:
1.导包:在原有SSM的jar包的基础上增加了MyBatisPlus的jar即可
2.开发:结构大概是这样的:
例子:查看数据
entity:
package com.bornwon.iot.web.modules.acs.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import java.io.Serializable;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* 实体类
*
* @author Fw
* @since 2020-07-17
*/
@Data
@TableName("t_gate_user")
@ApiModel(value = "GateUser对象", description = "GateUser对象")
public class GateUser extends Model implements Serializable{
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@ApiModelProperty(value = "主键")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
* 用户id
*/
@ApiModelProperty(value = "用户id")
private Integer userId;
/**
* 用户名
*/
@ApiModelProperty(value = "用户名")
private String name;
/**
* 用户角色 0普通用户 3超级管理员
*/
@ApiModelProperty(value = "用户角色 0普通用户 3超级管理员")
private Integer role;
/**
* 指纹数据
*/
@ApiModelProperty(value = "指纹数据")
private byte[] fingerData;
/**
* 是否已删除
*/
@ApiModelProperty(value = "是否已删除")
private Integer isDeleted;
@Override
protected Serializable pkVal() {
return this.id;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getFingerData() {
return fingerData;
}
public void setFingerData(byte[] fingerData) {
this.fingerData = fingerData;
}
public Integer getIsDeleted() {
return isDeleted;
}
public void setIsDeleted(Integer isDeleted) {
this.isDeleted = isDeleted;
}
}
insert into t_gate_user(`name`,user_id,finger_data)
values (#{name},#{userId},{fingerData,typeHandler=org.apache.ibatis.type.BlobTypeHandler})
----------------------------------------------------------------------------------------
mapper接口:
public interface GateUserMapper extends BaseMapper {
List selectGateUserPage(IPage page, GateUserVO gateUser,Integer equipId);
boolean addGateUser(GateUser user);
}
public interface IGateUserService extends IService {
IPage selectGateUserPage(IPage page, GateUserVO gateUser,Integer id);
boolean addGateUser(String userArray,String siteKey,Integer equipId);
}
-------------------------------------------------------------------------------------
public class UserServiceImpl extends ServiceImpl implements IGateUserService {
@Override
public IPage selectGateUserPage(IPage page, GateUserVO gateUser,Integer equipId) {
return page.setRecords(baseMapper.selectGateUserPage(page, gateUser,id));
}
@Override
public boolean addGateUser(GateUser user) {
//返回添加的状态
return R.status(baseMapper.addGateUser(gateUser));
}
}
@RestController
@AllArgsConstructor
@RequestMapping("/devops/acs")
@Api(value = "", tags = "接口")
public class GateUserController extends BaseController {
private IGateUserService gateUserService;
/**
* 详情
*/
@GetMapping("/detail")
@ApiOperationSupport(order = 1)
@ApiOperation(value = "详情", notes = "传入gateUser")
public R detail(GateUser gateUser) {
GateUser detail = gateUserService.getOne(Condition.getQueryWrapper(gateUser));
return R.data(GateUserWrapper.build().entityVO(detail));
}
@GetMapping("/list")
@ApiOperationSupport(order = 2)
@ApiOperation(value = "分页", notes = "传入gateUser")
public R> list(GateUserVO gateUser, Query query) {
return R.data(gateUserService.selectGateUserPage(Condition.getPage(query), gateUser,id));
}
/**
* 新增
*/
@PostMapping("/save")
@ApiOperationSupport(order = 4)
@ApiOperation(value = "新增", notes = "传入gateUser")
public R save(@Valid @RequestBody GateUser gateUser) {
return R.status(gateUserService.addGateUser(gateUser));
}
/**
* 修改
*/
@PostMapping("/update")
@ApiOperationSupport(order = 5)
@ApiOperation(value = "修改", notes = "传入gateUser")
public R update(@Valid @RequestBody GateUser gateUser) {
return R.status(gateUserService.updateById(gateUser));
}
/**
* 新增或修改
*/
@PostMapping("/submit")
@ApiOperationSupport(order = 6)
@ApiOperation(value = "新增或修改", notes = "传入gateUser")
public R submit(@Valid @RequestBody GateUser gateUser) {
return R.status(gateUserService.saveOrUpdate(gateUser));
}
/**
* 删除
*/
@PostMapping("/remove")
@ApiOperationSupport(order = 8)
@ApiOperation(value = "删除", notes = "传入ids")
public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
return R.status(gateUserService.removeByIds(Func.toIntList(ids)));
}
}
其中遇到的问题:
数据类型为Blob时,在声明实体类是类型为 byte[]
插入:fingerData.getBytes();
取出:(如果需要转换为Blob) =》fingerData = new String(gateUser.getFingerData(), "UTF-8");