当前位置: 首页 > 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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 删除矩阵中0所在行 matlab
  • 如何用Streamlit构建病毒式数据科学应用:快速入门指南
  • vue3 开始时间与结束时间比较验证(结束时间需要大于开始时间)
  • 防火墙组网与安全策略实验
  • esp32 usb cdc串口读写
  • 信息打点web篇--详解cdn识别与绕过
  • QT实现自定义带有提示信息的透明环形进度条
  • 基于蓝牙iBeacon定位技术的商场3D楼层导视软件功能详解与实施效益
  • 底软驱动 | Linux字符设备驱动开发基础
  • Vulnhub靶场 | DC系列 - DC2
  • 计算机视觉研究方向初学习,计算机视觉都有什么方向??!到底是干什么的?!
  • Kubelet 认证
  • iOS热门面试题(三)
  • 社交App iOS审核中的4.3问题:深入分析与解决策略
  • python-28-零基础自学python-json存数据、读数据,及程序合并
  • 【391天】每日项目总结系列128(2018.03.03)
  • JavaScript中的对象个人分享
  • JS数组方法汇总
  • KMP算法及优化
  • npx命令介绍
  • ReactNative开发常用的三方模块
  • Vue UI框架库开发介绍
  • vue-router的history模式发布配置
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 解决iview多表头动态更改列元素发生的错误
  • 深入浅出Node.js
  • 手机app有了短信验证码还有没必要有图片验证码?
  • scrapy中间件源码分析及常用中间件大全
  • 移动端高清、多屏适配方案
  • # 利刃出鞘_Tomcat 核心原理解析(七)
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • #QT(TCP网络编程-服务端)
  • (Arcgis)Python编程批量将HDF5文件转换为TIFF格式并应用地理转换和投影信息
  • (八)Flink Join 连接
  • (第30天)二叉树阶段总结
  • (二十四)Flask之flask-session组件
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (四) Graphivz 颜色选择
  • (算法)硬币问题
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (转)德国人的记事本
  • (自用)learnOpenGL学习总结-高级OpenGL-抗锯齿
  • (自用)交互协议设计——protobuf序列化
  • .bat批处理(六):替换字符串中匹配的子串
  • .net 4.0发布后不能正常显示图片问题
  • .NET Core 通过 Ef Core 操作 Mysql
  • .Net mvc总结
  • .NetCore发布到IIS
  • @RequestBody与@RequestParam
  • [145] 二叉树的后序遍历 js
  • [20150321]索引空块的问题.txt
  • [AIGC codze] Kafka 的 rebalance 机制
  • [Algorithm][动态规划][简单多状态DP问题][按摩师][打家劫舍Ⅱ][删除并获得点数][粉刷房子]详细讲解
  • [BZOJ1053][HAOI2007]反素数ant
  • [ccc3.0][数字钥匙] UWB配置和使用(二)