Mybatis-Plus BaseMapper的用法详解
1、如何使用BaseMapper进行数据库的操作。
2、使用BaseMapper进行插入实体时如何让UUID的主键自动生成。
Student实体类,其中id属性主键为UUID
packagecom.huixiaoer.ant.api.model.bean;
importcom.baomidou.mybatisplus.annotation.IdType;
importcom.baomidou.mybatisplus.annotation.TableId;
publicclassStudent{
/**
*
*ThisfieldwasgeneratedbyMyBatisGenerator.
*Thisfieldcorrespondstothedatabasecolumnstudent.id
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
@TableId(type=IdType.UUID)
privateStringid;
/**
*
*ThisfieldwasgeneratedbyMyBatisGenerator.
*Thisfieldcorrespondstothedatabasecolumnstudent.user_name
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
privateStringuserName;
/**
*
*ThisfieldwasgeneratedbyMyBatisGenerator.
*Thisfieldcorrespondstothedatabasecolumnstudent.age
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
privateIntegerage;
/**
*
*ThisfieldwasgeneratedbyMyBatisGenerator.
*Thisfieldcorrespondstothedatabasecolumnstudent.phone
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
privateStringphone;
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodcorrespondstothedatabasetablestudent
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
publicStudent(Stringid,StringuserName,Integerage,Stringphone){
this.id=id;
this.userName=userName;
this.age=age;
this.phone=phone;
}
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodcorrespondstothedatabasetablestudent
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
publicStudent(){
super();
}
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodreturnsthevalueofthedatabasecolumnstudent.id
*
*@returnthevalueofstudent.id
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
publicStringgetId(){
returnid;
}
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodsetsthevalueofthedatabasecolumnstudent.id
*
*@paramidthevalueforstudent.id
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
publicvoidsetId(Stringid){
this.id=id==null?null:id.trim();
}
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodreturnsthevalueofthedatabasecolumnstudent.user_name
*
*@returnthevalueofstudent.user_name
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
publicStringgetUserName(){
returnuserName;
}
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodsetsthevalueofthedatabasecolumnstudent.user_name
*
*@paramuserNamethevalueforstudent.user_name
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
publicvoidsetUserName(StringuserName){
this.userName=userName==null?null:userName.trim();
}
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodreturnsthevalueofthedatabasecolumnstudent.age
*
*@returnthevalueofstudent.age
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
publicIntegergetAge(){
returnage;
}
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodsetsthevalueofthedatabasecolumnstudent.age
*
*@paramagethevalueforstudent.age
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
publicvoidsetAge(Integerage){
this.age=age;
}
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodreturnsthevalueofthedatabasecolumnstudent.phone
*
*@returnthevalueofstudent.phone
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
publicStringgetPhone(){
returnphone;
}
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodsetsthevalueofthedatabasecolumnstudent.phone
*
*@paramphonethevalueforstudent.phone
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
publicvoidsetPhone(Stringphone){
this.phone=phone==null?null:phone.trim();
}
}
StudnetVI实体类,用户从页面接收参数
packagecom.huixiaoer.ant.api.model.vi;
importio.swagger.annotations.ApiModel;
importio.swagger.annotations.ApiModelProperty;
@ApiModel(value="student对象",description="学生对象student")
publicclassStudentVI{
/**
*
*ThisfieldwasgeneratedbyMyBatisGenerator.
*Thisfieldcorrespondstothedatabasecolumnstudent.user_name
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
@ApiModelProperty(value="学生姓名",name="userName",required=true)
privateStringuserName;
/**
*
*ThisfieldwasgeneratedbyMyBatisGenerator.
*Thisfieldcorrespondstothedatabasecolumnstudent.age
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
@ApiModelProperty(value="年龄",name="age",required=true)
privateIntegerage;
/**
*
*ThisfieldwasgeneratedbyMyBatisGenerator.
*Thisfieldcorrespondstothedatabasecolumnstudent.phone
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
@ApiModelProperty(value="手机号",name="phone",required=true)
privateStringphone;
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicIntegergetAge(){
returnage;
}
publicvoidsetAge(Integerage){
this.age=age;
}
publicStringgetPhone(){
returnphone;
}
publicvoidsetPhone(Stringphone){
this.phone=phone;
}
}
StudentPlusMapper接口类,实现BaseMapper用来实现Mybatis-Plus的增强功能。
packagecom.huixiaoer.ant.api.repository.mysql.mapper; importcom.baomidou.mybatisplus.core.mapper.BaseMapper; importcom.huixiaoer.ant.api.model.bean.Student; publicinterfaceStudentPlusMapperextendsBaseMapper{ //不需要实现,这时候就可以调用baseMapper的增删改查了 }
StudentService业务接口类
packagecom.huixiaoer.ant.api.service;
importcom.huixiaoer.ant.api.model.bean.Student;
importcom.huixiaoer.ant.api.model.bean.StudentExample;
importorg.apache.ibatis.annotations.Insert;
importorg.apache.ibatis.annotations.Param;
importorg.apache.ibatis.annotations.SelectKey;
importjava.util.List;
publicinterfaceStudentService{
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodcorrespondstothedatabasetablestudent
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
longcountByExample(StudentExampleexample);
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodcorrespondstothedatabasetablestudent
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
intdeleteByExample(StudentExampleexample);
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodcorrespondstothedatabasetablestudent
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
@Insert({
"insertintostudent(id,user_name,",
"age,phone)",
"values(#{id,jdbcType=VARCHAR},#{userName,jdbcType=VARCHAR},",
"#{age,jdbcType=INTEGER},#{phone,jdbcType=VARCHAR})"
})
@SelectKey(statement="selectuuid_short()",keyProperty="id",before=true,resultType=String.class)
intinsert(Studentrecord);
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodcorrespondstothedatabasetablestudent
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
intinsertSelective(Studentrecord);
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodcorrespondstothedatabasetablestudent
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
ListselectByExample(StudentExampleexample);
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodcorrespondstothedatabasetablestudent
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
intupdateByExampleSelective(@Param("record")Studentrecord,@Param("example")StudentExampleexample);
/**
*ThismethodwasgeneratedbyMyBatisGenerator.
*Thismethodcorrespondstothedatabasetablestudent
*
*@mbg.generatedThuOct3114:09:39CST2019
*/
intupdateByExample(@Param("record")Studentrecord,@Param("example")StudentExampleexample);
}
StudentServiceImpl业务接口实现类,这里将mybatis-plus的mapper接口注入其中。
packagecom.huixiaoer.ant.api.service.impl;
importcom.huixiaoer.ant.api.model.bean.Student;
importcom.huixiaoer.ant.api.model.bean.StudentExample;
importcom.huixiaoer.ant.api.repository.mysql.mapper.StudentMapper;
importcom.huixiaoer.ant.api.repository.mysql.mapper.StudentPlusMapper;
importcom.huixiaoer.ant.api.service.StudentService;
importlombok.extern.slf4j.Slf4j;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
importjava.util.List;
@Service
@Slf4j
publicclassStudentServiceImplimplementsStudentService{
@Autowired
privateStudentMapperstudentMapper;
@Autowired
privateStudentPlusMapperstudentPlusMapper;
@Override
publiclongcountByExample(StudentExampleexample){
return0;
}
@Override
publicintdeleteByExample(StudentExampleexample){
return0;
}
@Override
publicintinsert(Studentrecord){
returnstudentPlusMapper.insert(record);
}
@Override
publicintinsertSelective(Studentrecord){
returnstudentMapper.insertSelective(record);
}
@Override
publicListselectByExample(StudentExampleexample){
returnnull;
}
@Override
publicintupdateByExampleSelective(Studentrecord,StudentExampleexample){
return0;
}
@Override
publicintupdateByExample(Studentrecord,StudentExampleexample){
return0;
}
}
SchoolController类,实现前端和后台的交互。自动注入学生业务实现类。
packagecom.huixiaoer.ant.api.controller;
importcom.huixiaoer.ant.api.common.constant.ResultCode;
importcom.huixiaoer.ant.api.model.bean.Student;
importcom.huixiaoer.ant.api.model.vi.StudentVI;
importcom.huixiaoer.ant.api.service.impl.StudentServiceImpl;
importcom.huixiaoer.ant.api.util.*;
importio.swagger.annotations.*;
importlombok.extern.slf4j.Slf4j;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.web.bind.annotation.*;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjava.util.UUID;
/**
*@authorcreatebyyiqiang.wu
*@create2019/06/12
*@emailyiqiang.wu@huixiaoer.com
*@description登录
*/
@Slf4j
@RestController
@Api(tags="学校相关接口")
publicclassSchoolController{
@Autowired
privateStudentServiceImplstudentService;
@Autowired
privateHttpServletRequestrequest;
@ApiOperation(value="增加学生信息")
@PostMapping(value="/insert/student")
publicCommonResultinsertStudent(@RequestBody@ApiParam(name="学生对象",value="传入json格式",required=true)StudentVIstudentVI){
try{
Studentstudent=newStudent();
//student.setId(UUID.randomUUID().toString());
student.setUserName(studentVI.getUserName());
student.setAge(studentVI.getAge());
student.setPhone(studentVI.getPhone());
studentService.insert(student);
}catch(Exceptione){
System.out.println(e);
returnCommonUtil.buildResponse(ResultCode.SYSTEM_ERROR,ResultCode.SYSTEM_ERROR_MSG);
}
returnCommonUtil.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 distinct fromstudent orderby${orderByClause} deletefromstudent selectuuid_short() insertintostudent id, user_name, age, phone, #{id,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{phone,jdbcType=VARCHAR}, selectcount(*)fromstudent updatestudent id=#{record.id,jdbcType=VARCHAR}, user_name=#{record.userName,jdbcType=VARCHAR}, age=#{record.age,jdbcType=INTEGER}, phone=#{record.phone,jdbcType=VARCHAR}, updatestudent setid=#{record.id,jdbcType=VARCHAR}, user_name=#{record.userName,jdbcType=VARCHAR}, age=#{record.age,jdbcType=INTEGER}, phone=#{record.phone,jdbcType=VARCHAR}
各种UUID生成策略,生成的UUID值进行比较。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。