参考资料:
搭建教程:
- SpringBoot/SpringCloud的搭建参照上述链接,这里就不多赘述,搭建成品见上述参考demo
- ①、创建数据库
CREATE TABLE `guest_info` (
`id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '主键',
`guest_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
`guest_pwd` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
`sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性别',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`gx_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
- ②、导入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
- ③、配置实体,这个是数据库中表对应和字段对应的关键,重要的配置如下:
注:名字一致,除完全一致外,实体中的驼峰写法,对应表名或者字段名的"_"连接写法,如
createTime =====> create_time 一致
guestInfo =====> guest_info 一致
name =====> name 一致
name =====> Name 不一致
@TableName("") | 声明该实体对应的表名,当表名与实体类名一致时,可以不使用 |
@TableId | 声明该属性为表中的主键,当属性名称为默认id时,可以不使用 |
@TableId(type = IdType.ASSIGN_UUID) | 声明该属性为表中的主键,并且自动用uuid进行填充 |
@TableId(type = IdType.AUTO) | 声明该属性为表中的主键,并且自动进行自增 |
@TableField("") | 声明该属性对应的数据库字段,当属性名和字段名一致时,可以不使用 |
@TableField(fill = FieldFill.INSERT) | 该属性在插入时进行自动填充操作,当属性名和字段名不一致时,可以使用value属性声明对应的数据库字段名,加这个字段必须进行MetaObjectHandler的配置 |
@TableField(value = "",fill = FieldFill.INSERT_UPDATE) | 该属性在更新时进行自动填充操作,当属性名和字段名不一致时,可以使用value属性声明对应的数据库字段名,加这个字段必须进行MetaObjectHandler的配置 |
package com.example.springbootmybatisplusdemo.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
* @program: springboot-mybatis-plus-demo
* @description:
* @author: wjl
* @create: 2023-07-08 21:43
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("guest_info")//当表名与实体类名不一致时,可以在实体类上加入@TableName()声明
public class Guest {
@TableId(type = IdType.ASSIGN_UUID) //声明属性为表中的主键(若属性名称不为默认id) 自动生成UUID
//@TableId(type = IdType.AUTO) //主键自增,数据库字段也务必设置为自增长
private String id;
@TableField("guest_name")//当实体类属性与表字段不一致时,可以用来声明
private String name;
@TableField("guest_pwd")//当实体类属性与表字段不一致时,可以用来声明
private String pwd;
private String sex; //当实体类属性与表字段一致时,可以不声明
// 默认驼峰法表示_
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(value = "gx_time",fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
- ④、声明完实体和表的对应关系后,上述配置了自动填充,所以需要进行MetaObjectHandler接口的编写,如果没有设置自动填充,这步可以省略
package com.example.springbootmybatisplusdemo.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @program: springboot-mybatis-plus-demo
* @description:
* @author: wjl
* @create: 2023-07-08 22:21
**/
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
/**
* 插入时的填充策略
*
* @param metaObject
*/
@Override
public void insertFill(MetaObject metaObject) {
log.info("start insert fill ....");
metaObject.setValue("createTime", new Date());
}
/**
* 更新时的填充策略
*
* @param metaObject
*/
@Override
public void updateFill(MetaObject metaObject) {
log.info("start update fill ....");
metaObject.setValue("updateTime", new Date());
}
}
- ⑤、dao层的编写
package com.example.springbootmybatisplusdemo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springbootmybatisplusdemo.entity.Guest;
public interface GuestMapper extends BaseMapper<Guest> {
}
- ⑥、service层接口
package com.example.springbootmybatisplusdemo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.springbootmybatisplusdemo.entity.Guest;
public interface GuestService extends IService<Guest> {
}
- ⑦、service层接口实现
package com.example.springbootmybatisplusdemo.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springbootmybatisplusdemo.dao.GuestMapper;
import com.example.springbootmybatisplusdemo.entity.Guest;
import org.springframework.stereotype.Service;
@Service
public class GuestServiceImpl extends ServiceImpl<GuestMapper, Guest> implements GuestService{
}
- ⑧、controller层实现
package com.example.springbootmybatisplusdemo.controller;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.springbootmybatisplusdemo.entity.Guest;
import com.example.springbootmybatisplusdemo.service.GuestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@Slf4j
public class GuestController {
@Autowired
private GuestService guestService;
//增
@RequestMapping("add")
public String add(){
Guest guest = new Guest();
guest.setName("张三");
guest.setPwd("123");
guest.setSex("男");
guestService.save(guest);
return "finish";
}
//删
@RequestMapping("delete")
public String delete(String id){
guestService.removeById(id);
return "finish";
}
//改
@RequestMapping("update")
public String update(){
UpdateWrapper<Guest> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("guest_name","张三");
Guest guest = new Guest();
guest.setPwd("123456789");
boolean update = guestService.update(guest, updateWrapper);
return "finish";
}
//查
@RequestMapping("query")
public List<Guest> add(@RequestParam("name") String name){
UpdateWrapper<Guest> updateWrapper = new UpdateWrapper<>();
updateWrapper.like("guest_name",name);
List<Guest> list = guestService.list(updateWrapper);
return list;
}
}
- ⑨、最后一定要记得在启动类中添加扫描
@MapperScan("com.example.springbootmybatisplusdemo.dao") //扫描mapper文件夹
- ⑩、项目结构如下:具体见上述参考demo