当前位置: 首页 > news >正文

MyBatisPlus实现增删改查

文章目录

  • MyBatisPlus实现增删改查
    • 基本操作
    • 分页查询
      • 配置分页插件

MyBatisPlus实现增删改查

实体类GkUser

package com.geekmice.springbootselfexercise.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.ToString;
import org.nustaq.serialization.annotations.Serialize;/*** (GkUser)实体类** @author pingmingbo* @since 2024-06-21 14:05:12*/
@TableName(value = "gk_user")
@Data
@ToString
@Serialize
public class GkUser{/*** 主键ID*/@TableId(type = IdType.AUTO)private Long id;/*** 姓名*/@TableField(value = "name")private String name;/*** 年龄*/@TableField(value = "age")private Integer age;/*** 邮箱*/@TableField(value = "email")private String email;}

数据层GkUserDao

package com.geekmice.springbootselfexercise.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.geekmice.springbootselfexercise.entity.GkUser;/*** (GkUser)表数据库访问层** @author pingmingbo* @since 2024-06-21 14:05:12*/
public interface GkUserDao extends BaseMapper<GkUser> {
}

映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.geekmice.springbootselfexercise.dao.GkUserDao"><resultMap type="com.geekmice.springbootselfexercise.entity.GkUser" id="GkUserMap"><result property="id" column="id" jdbcType="INTEGER"/><result property="name" column="name" jdbcType="VARCHAR"/><result property="age" column="age" jdbcType="INTEGER"/><result property="email" column="email" jdbcType="VARCHAR"/></resultMap></mapper>

业务层GkUserService

package com.geekmice.springbootselfexercise.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.geekmice.springbootselfexercise.entity.GkUser;/*** (GkUser)表服务接口** @author pingmingbo* @since 2024-06-21 14:05:12*/
public interface GkUserService extends IService<GkUser> {}package com.geekmice.springbootselfexercise.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geekmice.springbootselfexercise.dao.GkUserDao;
import com.geekmice.springbootselfexercise.entity.GkUser;
import com.geekmice.springbootselfexercise.service.GkUserService;
import org.springframework.stereotype.Service;/*** (GkUser)表服务实现类** @author pingmingbo* @since 2024-06-21 14:05:12*/
@Service("gkUserService")
public class GkUserServiceImpl extends ServiceImpl<GkUserDao, GkUser> implements GkUserService {
}

基本操作

package com.geekmice.springbootselfexercise.first;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.geekmice.springbootselfexercise.SpringBootSelfExerciseApplication;
import com.geekmice.springbootselfexercise.dao.GkUserDao;
import com.geekmice.springbootselfexercise.entity.GkUser;
import com.geekmice.springbootselfexercise.service.GkUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;/*** @BelongsProject: spring-boot-self-exercise* @BelongsPackage: com.geekmice.springbootselfexercise.first* @Author: pingmingbo* @CreateTime: 2024-07-09  10:12* @Description: mybatisplus实现增删改查* @Version: 1.0*/
@SpringBootTest(classes = SpringBootSelfExerciseApplication.class)
@RunWith(SpringRunner.class)
@Slf4j
public class FirstTest {@ResourceGkUserDao gkUserDao;@ResourceGkUserService gkUserService;@Testpublic void t1() {// 初始数据// id  name age email// 1  Jone 18 test1@baomidou.com// 2  Jack 20 test2@baomidou.com// 3  Tom 28 test3@baomidou.com// 4  Sandy 21 test4@baomidou.com// 5  Billie 24 test5@baomidou.com// 1、查询GkUser gkUser = gkUserDao.selectById(1);log.info("1.1、根据主键id查询 gkUser : [{}]", gkUser);List<GkUser> gkUsers = gkUserDao.selectList(null);log.info("1.2、查询所有 gkUsers : [{}]", CollectionUtils.size(gkUsers));// todo 分页查询 通过分页插件PaginationInnerInterceptor实现// 批量查询List<Integer> list = new ArrayList(16);list.add(1);list.add(2);List<GkUser> batchUsers = gkUserDao.selectBatchIds(list);log.info("1.3、批量主键查询 batchUsers : [{}]", CollectionUtils.size(batchUsers));// 根据条件批量查询QueryWrapper<GkUser> queryWrapper = new QueryWrapper<>();queryWrapper.le("id", 2);List<GkUser> gkUsers1 = gkUserDao.selectList(queryWrapper);log.info("1.4、根据条件批量查询 gkUsers1 : [{}]", CollectionUtils.size(gkUsers1));// 2、增加GkUser insertGkUser = gkUserDao.selectById(6);if (Objects.isNull(insertGkUser)) {GkUser domain = new GkUser();domain.setAge(10);domain.setEmail("test6@baomidou.com");domain.setName("Rose");gkUserDao.insert(domain);}GkUser afterInsertGkUser = gkUserDao.selectById(6);log.info("2.1 添加数据 afterInsertGkUser : [{}]", afterInsertGkUser);// 3、修改GkUser updateGkUser = gkUserDao.selectById(6);GkUser domain = new GkUser();if (Objects.nonNull(updateGkUser)) {domain.setId(6L);domain.setAge(6);domain.setEmail("test66@baomidou.com");domain.setName("rose");gkUserDao.updateById(domain);log.info("3.1 更新一条数据 domain ");}System.out.println("aaa");UpdateWrapper<GkUser> singleUpdateWrapper = new UpdateWrapper<>();singleUpdateWrapper.lambda().in(GkUser::getId,list);singleUpdateWrapper.set("name","abc");gkUserService.update(singleUpdateWrapper);log.info("3.2 定制化修改");// 4、删除// 定制化条件删除QueryWrapper<GkUser> deleteWrapper = new QueryWrapper<>();deleteWrapper.lambda().le(GkUser::getId,1);gkUserService.remove(deleteWrapper);log.info("4.1 根据条件删除");gkUserService.removeById(2);log.info("4.2 根据主键删除");}
}

分页查询

配置分页插件

package com.geekmice.springbootselfexercise.config;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @BelongsProject: spring-boot-self-exercise* @BelongsPackage: com.geekmice.springbootselfexercise.config* @Author: pingmingbo* @CreateTime: 2023-08-09  09:29* @Description: mybatis配置信息* @Version: 1.0*/
@Configuration
@MapperScan(value = "com.geekmice.springbootselfexercise.dao")
public class MybatisPlusConfig {/*** 分页插件配置*/@Bean(name = "mybatisPlusInterceptor")public MybatisPlusInterceptor mybatisPlusInterceptor (){MybatisPlusInterceptor interceeptor = new MybatisPlusInterceptor();interceeptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceeptor;}}

如何使用

    Page<GkUser> gkUserPage = new Page<>(1,3);Page<GkUser> records = gkUserDao.selectPage(gkUserPage, null);log.info("records : [{}]" , records);

> Preparing: SELECT id,name,age,email FROM gk_user LIMIT ?
> Parameters: 3(Long)
<
Columns: id, name, age, email
<
Row: 3, Tom, 28, test3@baomidou.com
<== Row: 4, Sandy, 21, test4@baomidou.com
<== Row: 5, Billie, 24, test5@baomidou.com
<== Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67376bae]
2024-07-10 01:37:14.271 INFO 9164 — [ main] c.g.s.first.FirstTest : records : [com.baomidou.mybatisplus.extension.plugins.pagination.Page@135a8808]
2024-07-10 01:37:14.790 INFO 9164 — [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated…
2024-07-10 01:37:14.873 INFO 9164 — [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.

Process finished with exit code 0

http://www.lryc.cn/news/399511.html

相关文章:

  • 【Rust】——不安全Rust
  • 使机器人在执行任务时更加稳定
  • FFmpeg学习(五)-- libswresample使用说明及函数介绍
  • 车载视频监控管理方案:无人驾驶出租车安全出行的保障
  • 05STM32EXIT外部中断中断系统
  • MetaGPT和LangGraph对比
  • 基于SpringBoot+Hadoop+python的物品租赁系统(带1w+文档)
  • 关于 RK3588刷镜像升级镜像”没有发现设备“ 的解决方法
  • docker 上传镜像到hub仓库
  • 查询(q_proj)、键(k_proj)和值(v_proj)投影具体含义
  • 超详细版阿里云控制台环境配置+数据库配置
  • Linux:Linux网络总结(附下载链接)
  • Cxx Primer-CP-2
  • OpenCV距离变换函数distanceTransform的使用
  • Service Mesh 是一种用于处理服务间通信的基础设施层
  • QML界面控件加载与显示顺序
  • C++从入门到起飞之——缺省参数/函数重载/引用全方位剖析!
  • tkinter-TinUI-xml实战(12)pip可视化管理器
  • Java中标识符和关键字
  • 电子版pdf格式标书怎么加盖公章?
  • 【开放集目标检测】Grounding DINO
  • 东莞酷得 PMS134应广8位OTP单片机
  • [终端安全]-7 后量子密码算法
  • uniapp 支付宝小程序 芝麻免押 免押金
  • Python爬虫教程第一篇
  • AI时代:探索个人潜能的新视角
  • 【Python学习笔记】Optuna + Transformer B站视频实践
  • 【自动驾驶/机器人面试C++八股精选】专栏介绍
  • Unity中一键生成具有身体感知的虚拟人物动作
  • 谷粒商城实战-25-分布式组件-SpringCloud Alibaba-Nacos配置中心-加载多配置集