栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis-Plus BaseMapper的用法详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis-Plus BaseMapper的用法详解

1、如何使用baseMapper进行数据库的操作。

2、使用baseMapper进行插入实体时如何让UUID的主键自动生成。

Student实体类,其中id属性主键为UUID

package com.huixiaoer.ant.api.model.bean;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;

public class Student {
  
  @TableId(type= IdType.UUID)
  private String id;

  
  private String userName;

  
  private Integer age;

  
  private String phone;

  
  public Student(String id, String userName, Integer age, String phone) {
    this.id = id;
    this.userName = userName;
    this.age = age;
    this.phone = phone;
  }

  
  public Student() {
    super();
  }

  
  public String getId() {
    return id;
  }

  
  public void setId(String id) {
    this.id = id == null ? null : id.trim();
  }

  
  public String getUserName() {
    return userName;
  }

  
  public void setUserName(String userName) {
    this.userName = userName == null ? null : userName.trim();
  }

  
  public Integer getAge() {
    return age;
  }

  
  public void setAge(Integer age) {
    this.age = age;
  }

  
  public String getPhone() {
    return phone;
  }

  
  public void setPhone(String phone) {
    this.phone = phone == null ? null : phone.trim();
  }
}

StudnetVI实体类,用户从页面接收参数

package com.huixiaoer.ant.api.model.vi;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(value = "student对象",description = "学生对象student")
public class StudentVI {


  
  @ApiModelProperty(value="学生姓名",name="userName",required=true)
  private String userName;

  
  @ApiModelProperty(value="年龄",name="age",required=true)
  private Integer age;

  
  @ApiModelProperty(value="手机号",name="phone",required=true)
  private String phone;


  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

}

StudentPlusMapper接口类,实现baseMapper用来实现Mybatis-Plus的增强功能。

package com.huixiaoer.ant.api.repository.mysql.mapper;

import com.baomidou.mybatisplus.core.mapper.baseMapper;
import com.huixiaoer.ant.api.model.bean.Student;

public interface StudentPlusMapper extends baseMapper {
  //不需要实现,这时候就可以调用baseMapper的增删改查了
}

StudentService业务接口类

package com.huixiaoer.ant.api.service;

import com.huixiaoer.ant.api.model.bean.Student;
import com.huixiaoer.ant.api.model.bean.StudentExample;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectKey;

import java.util.List;

public interface StudentService {
  
  long countByExample(StudentExample example);

  
  int deleteByExample(StudentExample example);

  
  @Insert({
    "insert into student (id, user_name, ",
    "age, phone)",
    "values (#{id,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR}, ",
    "#{age,jdbcType=INTEGER}, #{phone,jdbcType=VARCHAR})"
  })
  @SelectKey(statement="select uuid_short()", keyProperty="id", before=true, resultType=String.class)
  int insert(Student record);

  
  int insertSelective(Student record);

  
  List selectByExample(StudentExample example);

  
  int updateByExampleSelective(@Param("record") Student record, @Param("example") StudentExample example);

  
  int updateByExample(@Param("record") Student record, @Param("example") StudentExample example);
}

StudentServiceImpl业务接口实现类,这里将mybatis-plus的mapper接口注入其中。

package com.huixiaoer.ant.api.service.impl;

import com.huixiaoer.ant.api.model.bean.Student;
import com.huixiaoer.ant.api.model.bean.StudentExample;
import com.huixiaoer.ant.api.repository.mysql.mapper.StudentMapper;
import com.huixiaoer.ant.api.repository.mysql.mapper.StudentPlusMapper;
import com.huixiaoer.ant.api.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class StudentServiceImpl implements StudentService {

  @Autowired
  private StudentMapper studentMapper;

  @Autowired
  private StudentPlusMapper studentPlusMapper;

  @Override
  public long countByExample(StudentExample example) {
    return 0;
  }

  @Override
  public int deleteByExample(StudentExample example) {
    return 0;
  }

  @Override
  public int insert(Student record) {
    return studentPlusMapper.insert(record);
  }

  @Override
  public int insertSelective(Student record) {
    return studentMapper.insertSelective(record);
  }

  @Override
  public List selectByExample(StudentExample example) {
    return null;
  }

  @Override
  public int updateByExampleSelective(Student record, StudentExample example) {
    return 0;
  }

  @Override
  public int updateByExample(Student record, StudentExample example) {
    return 0;
  }
}

SchoolController类,实现前端和后台的交互。自动注入学生业务实现类。

package com.huixiaoer.ant.api.controller;

import com.huixiaoer.ant.api.common.constant.ResultCode;
import com.huixiaoer.ant.api.model.bean.Student;
import com.huixiaoer.ant.api.model.vi.StudentVI;
import com.huixiaoer.ant.api.service.impl.StudentServiceImpl;
import com.huixiaoer.ant.api.util.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;


@Slf4j
@RestController
@Api(tags = "学校 相关接口")
public class SchoolController {

  @Autowired
  private StudentServiceImpl studentService;

  @Autowired
  private HttpServletRequest request;

  @ApiOperation(value = "增加学生信息")
  @PostMapping(value = "/insert/student")
  public CommonResult insertStudent(@RequestBody @ApiParam(name="学生对象",value="传入json格式",required=true) StudentVI studentVI) {
    try {
      Student student = new Student();
//      student.setId(UUID.randomUUID().toString());
      student.setUserName(studentVI.getUserName());
      student.setAge(studentVI.getAge());
      student.setPhone(studentVI.getPhone());
      studentService.insert(student);
    } catch (Exception e) {
      System.out.println(e);
      return CommonUtil.buildResponse(ResultCode.SYSTEM_ERROR, ResultCode.SYSTEM_ERROR_MSG);
    }
    return CommonUtil.buildResponse(ResultCode.SUCCESS, ResultCode.SUCCESS_MSG);
  }

}

补充一下Mybatis的xml文件




 
  
  
   
   
   
   
  
 
 
  
  
   
    
     
      

 
  and ${criterion.condition}
 
 
  and ${criterion.condition} #{criterion.value}
 
 
  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
 
 
  and ${criterion.condition}
  
   #{listItem}
  
 

      
     
    
   
  
 
 
  
  
   
    
     
      

 
  and ${criterion.condition}
 
 
  and ${criterion.condition} #{criterion.value}
 
 
  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
 
 
  and ${criterion.condition}
  
   #{listItem}
  
 

      
     
    
   
  
 
 
  
  id, user_name, age, phone
 
 
  
  select count(*) from student
  
   
  
 
 
  
  update student
  
   
    id = #{record.id,jdbcType=VARCHAR},
   
   
    user_name = #{record.userName,jdbcType=VARCHAR},
   
   
    age = #{record.age,jdbcType=INTEGER},
   
   
    phone = #{record.phone,jdbcType=VARCHAR},
   
  
  
   
  
 
 
  
  update student
  set id = #{record.id,jdbcType=VARCHAR},
   user_name = #{record.userName,jdbcType=VARCHAR},
   age = #{record.age,jdbcType=INTEGER},
   phone = #{record.phone,jdbcType=VARCHAR}
  
   
  
 

各种UUID生成策略,生成的UUID值进行比较。

到此这篇关于Mybatis-Plus baseMapper的用法详解的文章就介绍到这了,更多相关Mybatis-Plus baseMapper内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/132255.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号