SpringBoot框架——8.MybatisPlus常见用法(常用注解+内置方法+分页查询)

1.MybatisPlus常用注解:

        1.1 当数据库、表名和字段名和实体类完全一致时无需加注解,不一致时:

        @TableName指定库名

        @TableId指定表名

        @TableField指定字段名

        1.2 自增主键:

        @TableId(type=IdType.AUTO)

        private Long id;

        1.3 实体类中属性不是表字段:

        @TableField(exist=false)

2.内置增删改查:

        这里如果加了@Data注解但无法生效,应该是没有安装Lombok插件,在plugin中添加即可

        2.1 增:

    @Test
    public void testInsert(){
        User user=new User();
        user.setName("lxj");
        user.setEmail("lxj@163.com");
        user.setAge(30);
        Assert.assertTrue(userMapper.insert(user)>0);
        userMapper.selectList(null).forEach(System.out::println);
    }

        2.1 删(3种方式):

    @Test
    public void testDelete(){
        //主键删除
//        userMapper.deleteById(1l);//长整型需添加l
//        userMapper.selectList(null).forEach(System.out::println);

        //批量删除
        //userMapper.delete(new QueryWrapper<User>().like("name","J"));
        //userMapper.delete(Wrappers.<User>query().like("name","J"));
        userMapper.delete(Wrappers.<User>query().lambda().like(User::getName,"J"));
        userMapper.selectList(null).forEach(System.out::println);

    }

        2.3 改:

        这里可以在实体类中添加@Accessors(chain=true)注解使set方法返回一个当前对象。

    @Test
    public void testUpdate(){
        //基本修改
//        userMapper.updateById(new User().setId(1l).setName("wayaya"));
//        userMapper.selectList(null).forEach(System.out::println);

        //批量修改
//        userMapper.update(null,Wrappers.<User>update().set("email","ppp@163.com").like("name","J"));
//        userMapper.selectList(null).forEach(System.out::println);
        //批量修改
        userMapper.update(new User().setEmail("ppp@163.com"),Wrappers.<User>update().like("name","J"));
        userMapper.selectList(null).forEach(System.out::println);

    }

        2.4 查(两种方式):

    @Test
    public void testSelectNew(){
        //System.out.println(userMapper.selectOne(Wrappers.<User>query().eq("name","Tom")));

        userMapper.selectList(new QueryWrapper<User>().select("id","name")).forEach(user -> {
            System.out.println(user);
        });
    }

3.分页

        原理一样都是通过分页拦截器,查询前先查询总行数,然后再查询当前页记录。

        先添加一个分页拦截器:MybatisPlusConfig

package com.lxj.quickstart.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor().setCountSqlParser(new JsqlParserCountOptimize(true));//可优化1对1连接查询效率
    }
}

        3.1内置分页查询:

    @Test
    public void testPage(){
        IPage<User> page=new Page<>(2,2);
        IPage<User> pr = userMapper.selectPage(page, Wrappers.<User>query());
        System.out.println("总行数"+pr.getTotal());
        System.out.println("总页数"+pr.getPages());
        System.out.println("每页行数"+pr.getSize());

        pr.getRecords().forEach(user -> {
            System.out.println(user);
        });
    }

        3.2自定义xml分页查询:

        添加配置项:

    
#mybatisplus
mybatis-plus:
  type-aliases-package: com.lxj.quickstart.entity   #别名搜索
  mapper-locations: classpath:/mappers/*.xml         #加载映射文件

        添加xml查询:

<?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.lxj.quickstart.mapper.UserMapper">

    <sql id="selectSql">
        SELECT
              *
        FROM
              user
    </sql>

    <select id="selectUserByPage" resultType="user">
        <include refid="selectSql"></include>
        <where>
            <if test="u.age != null">
                age = #{u.age}
            </if>
            <if test="u.email != null">
                and email like '%${u.email}%'
            </if>
        </where>
    </select>
</mapper>

        添加mapper接口:

package com.lxj.quickstart.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxj.quickstart.entity.User;
import org.apache.ibatis.annotations.Param;

public interface UserMapper extends BaseMapper<User> {


    //映射的接口中有2个参数需要@Param定义参数名,映射文件中使用p.和c.调用属性
    public IPage<User> selectUserByPage(@Param("p") IPage<User> page, @Param("u") User condition);

}

        这里注意第二个参数’u‘必须和xml中的u一致。

        添加测试:

    @Test
    public void testPage2(){

        IPage<User> page=new Page<>(1,2);

        //条件对象
        User u=new User();
        u.setAge(18);
        u.setEmail("@163.com");

        IPage<User> pr = userMapper.selectUserByPage(page, u);
        System.out.println("总行数"+pr.getTotal());
        System.out.println("总页数"+pr.getPages());
        System.out.println("每页行数"+pr.getSize());

        pr.getRecords().forEach(user -> {
            System.out.println(user);
        });
    }

        3.3 pageHelper分页

        添加依赖:

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.11</version>
        </dependency>

        添加拦截器:

    //两个分页插件不冲突
    @Bean
    public PageInterceptor pageInterceptor(){
        return  new PageInterceptor();
    }

        映射文件 :

<select id="selectUserByPage2" resultType="user">
        <include refid="selectSql"></include>
        <where>
            <if test="age != null">
                age = #{age}
            </if>
            <if test="email != null">
                and email like '%${email}%'
            </if>
        </where>
    </select>

        映射文件对呀接口:

public List<User> selectUserByPage2(User condition);

        测试:

    @Test
    public void testPageHelper(){
        //条件对象
        User u=new User();
        u.setAge(18);
        u.setEmail("@163.com");


        PageInfo<User> page=PageHelper.startPage(1,2).doSelectPageInfo(()->{
            //映射文件
            userMapper.selectUserByPage2(u);
            //内置方法
            userMapper.selectList(Wrappers.<User>query());
        });



        List<User> list = page.getList();

        page.getList().forEach(System.out :: println);

        System.out.println("总行数"+page.getTotal());
        System.out.println("总页数"+page.getPages());
        System.out.println("每页行数"+page.getPageSize());
        System.out.println("当前页数"+page.getPageNum());

        System.out.println("起始行数"+page.getStartRow());
        System.out.println("每页行数"+page.getSize());
        System.out.println("是第一页"+page.isIsFirstPage());
        System.out.println("是最后一页"+page.isIsLastPage());
        System.out.println("有上一页"+page.isHasPreviousPage());
        System.out.println("有下一页"+page.isHasNextPage());
        System.out.println("页码列表"+Arrays.toString(page.getNavigatepageNums()));
    }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/555681.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

python环境引用《解读》----- 环境隔离

首先我先讲一下Anaconda&#xff0c;因为我用的是Anaconda进行包管理。方便后面好理解一点。 大家在python中引用环境的时候都会经历下面这一步&#xff1a; 那么好多人就会出现以下问题&#xff08;我就是遇到了这个问题&#xff09;&#xff1a; 我明明下载了包&#xff0c…

编程填空题:麻烦的位运算

闲着没事干可以做做 可以看到&#xff0c;那个函数直接return了&#xff0c;也就是说&#xff0c;得找到一个表达式&#xff0c;直接求出结果 简单分析一下&#xff1a; 其第i位为1当且仅当n的右边i位均为1 也就是说&#xff0c;前i-1位有0&#xff0c;第i位就是0 也就是说…

针对springcloud gateway 跨域问题解决方案

springcloud gateway版本 <spring-boot.version>2.3.3.RELEASE</spring-boot.version> <spring-cloud.version>Hoxton.SR8</spring-cloud.version>跨域问题说明 application:1 Access to XMLHttpRequest at https://xxxxxxxxxx from origin http://l…

创新入门|用户体验设计策略:数字化成功的蓝图

今天,我们来深入探讨如何打造令人兴奋的用户体验设计策略。将数字产品和服务从“普通”提升到“太棒了”的高度,这将是我们的主题。为什么这一策略那么重要呢?在当今被各种数字产品“轰炸”的世界,一个可靠的体验策略可能决定用户是否长期使用,还是一弹而走。我们都曾深受那种…

【Canvas技法】六种环状花纹荟萃

【图例】 【代码】 <!DOCTYPE html> <html lang"utf-8"> <meta http-equiv"Content-Type" content"text/html; charsetutf-8"/> <head><title>使用HTML5/Canvas绘制六种环状花纹</title><style type&quo…

万兆以太网10G Ethernet简介

2002年6月IEEE标准协会批准了万兆&#xff08;10G&#xff09;以太网的正式标准。此标准的全名是“10Gbit/s工作的媒体接入控制参数、物理层和管理参数”。 另一个组织是10G以太网联盟(10GEA)。10GEA由网络界的著名企业创建&#xff0c;现已有一百多家企业参加&#xff0c;中国…

林草资源管理系统:构筑绿色长城,守护自然之美

在全球气候变化和生态环境恶化的背景下&#xff0c;森林和草原资源的保护、恢复和合理利用显得尤为重要。林草资源管理系统的建立&#xff0c;旨在通过现代信息技术手段&#xff0c;提升林草资源管理的效率和质量&#xff0c;确保自然资源的可持续发展。 项目背景 森林和草原…

初学python记录:力扣705. 设计哈希集合

题目&#xff1a; 不使用任何内建的哈希表库设计一个哈希集合&#xff08;HashSet&#xff09;。 实现 MyHashSet 类&#xff1a; void add(key) 向哈希集合中插入值 key 。bool contains(key) 返回哈希集合中是否存在这个值 key 。void remove(key) 将给定值 key 从哈希集合…

基于Java+SpringBoot+Vue前后端分离精简博客系统设计和实现

基于JavaSpringBootVue前后端分离精简博客系统设计和实现 &#x1f345; 作者主页 央顺技术团队 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; &#x1f345; 文末获取源码联系方式 &#x1f4dd; &#x1f345; 查看下方微信号获取联系方式 承接各种定制系…

线程互斥,线程安全和线程同步

多线程的基本代码编写步骤 1.创建线程pthread_create() 2.终止线程的三种方法。线程取消pthread_cancel(一般在主线程取消)&#xff0c; 线程终止pthread_exit(在其他线程执行)&#xff0c; 或者使用线程返回return 3.线程等待pthread_join 需要等待的原因是 1.已经退出的线程…

WordPress的全面解析:为什么它是创建博客和网站的首选

在当前的数字化时代&#xff0c;无论是个人博客还是企业网站&#xff0c;都需要一个强大而灵活的平台以支撑其内容和用户交互。WordPress作为全球最流行的内容管理系统&#xff08;CMS&#xff09;&#xff0c;以其强大的功能、灵活的定制性和广泛的用户基础&#xff0c;成为了…

交通管理在线服务系统|基于Springboot的交通管理系统设计与实现(源码+数据库+文档)

交通管理在线服务系统目录 目录 基于Springboot的交通管理系统设计与实现 一、前言 二、系统功能设计 三、系统实现 1、用户信息管理 2、驾驶证业务管理 3、机动车业务管理 4、机动车业务类型管理 四、数据库设计 1、实体ER图 五、核心代码 六、论文参考 七、最新计…

开关转换器中的噪声源对纹波测量的影响

由于输出纹波通常较小,示波器需要设置为高电压灵敏度。然而,这种设置容易受到电源产生的噪音的影响。其中一种常见的噪音源是电感的漂移磁场。许多廉价的电感采用了半屏蔽的I型磁心,其绕线周围包覆着铁氧体粉末和环氧树脂。即使如此,这些电感仍然会产生相当大的漂移磁场。附…

什么是CPU与GPU,它们之间有什么关系

什么是CPU与GPU&#xff0c;它们之间有什么关系一、CPU1. 核心功能2. 工作原理3. 组成部分4. 发展历程5. 性能指标6. 架构种类7. 发展趋势8. 应用领域 二、GPU三、CPU与GPU的关系 什么是CPU与GPU&#xff0c;它们之间有什么关系 一、CPU CPU&#xff0c;全称是“Central Proc…

profinet协议基础

文章目录 工业以太网自动化通讯金字塔工业以太网技术比较 profinet概述profinet特性 EtherNet通信EtherCAT通信EtherCat特性EtherCat过程同步 工业以太网 工业以太网是基于IEEE 802.3 (Ethernet)的强大的区域和单元网络。 自动化通讯金字塔 各个组织与工业以太网 工业以太网…

Docker操作容器打包(commit),压缩(save),挂载(load)

文章目录 前言一、容器打包二、将镜像压缩成tar包三、将tar包挂载为镜像结束 前言 将容器打包成镜像时&#xff0c;你正在将应用程序及其所有依赖项、文件和配置文件捆绑到一个可移植的、独立的单元中。这样做可以确保您的应用程序在不同环境中具有一致的运行方式&#xff0c;…

VBA技术资料MF143:将PowerPoint中幻灯片导出为图片

我给VBA的定义&#xff1a;VBA是个人小型自动化处理的有效工具。利用好了&#xff0c;可以大大提高自己的工作效率&#xff0c;而且可以提高数据的准确度。“VBA语言専攻”提供的教程一共九套&#xff0c;分为初级、中级、高级三大部分&#xff0c;教程是对VBA的系统讲解&#…

# 从浅入深 学习 SpringCloud 微服务架构(二)模拟微服务环境

从浅入深 学习 SpringCloud 微服务架构&#xff08;二&#xff09;模拟微服务环境&#xff08;1&#xff09; 段子手168 1、打开 idea 创建父工程 创建 artifactId 名为 spring_cloud_demo 的 maven 工程。 --> idea --> File --> New --> Project --> Ma…

[蓝桥杯 | 暴搜] 学会暴搜之路

虽然会调侃蓝桥杯是暴力求解的&#xff0c;但是本弱弱不会搜&#xff0c;不知道如何搜&#xff0c;于是写下这篇碎碎念&#xff0c;记录看到过的&#xff0c;惊艳自己的暴搜。 小总结 题目特征&#xff1a;很复杂的排列组合 说是暴力&#xff0c;其实就是枚举罢了&#xff0…

java项目的构建工具-Maven

黑马程序员JavaWeb开发教程 文章目录 一、概述1、介绍&#xff08;1&#xff09;介绍&#xff08;2&#xff09;Maven的作用&#xff08;3&#xff09;官网&#xff08;4&#xff09;仓库 2、安装 二、IDEA 集成 Maven1、配置Maven环境2、创建Maven项目&#xff08;1&#xff0…