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

embedded/2024/10/18 22:35:09/

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 增:

    @Testpublic 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种方式):

    @Testpublic 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方法返回一个当前对象。

    @Testpublic 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 查(两种方式):

    @Testpublic 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 {@Beanpublic PaginationInterceptor paginationInterceptor(){return new PaginationInterceptor().setCountSqlParser(new JsqlParserCountOptimize(true));//可优化1对1连接查询效率}
}

        3.1内置分页查询

    @Testpublic 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*FROMuser</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一致。

        添加测试:

    @Testpublic 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>

        添加拦截器:

    //两个分页插件不冲突@Beanpublic 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);

        测试:

    @Testpublic 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.ppmy.cn/embedded/16927.html

相关文章

【Pytorch】(十四)C++ 加载TorchScript 模型

文章目录 &#xff08;十四&#xff09;C 加载TorchScript 模型Step 1: 将PyTorch模型转换为TorchScriptStep 2: 将TorchScript序列化为文件Step 3: C程序中加载TorchScript模型Step 4: C程序中运行TorchScript模型 【Pytorch】&#xff08;十三&#xff09;PyTorch模型部署: T…

软考137-上午题-【软件工程】-软件质量

一、软件质量 软件质量是指反映软件系统或软件产品满足规定或隐含需求的能力的特征和特性全体。软件质量管理是指对软件开发过程进行独立的检查活动&#xff0c;由质量保证、质量规划和质量控制3个主要活动构成。软件质量保证是指为保证软件系统或软件产品充分满足用户要求的质…

图论基础知识 深度优先(Depth First Search, 简称DFS),广度优先(Breathe First Search, 简称DFS)

图论基础知识 学习记录自代码随想录 dfs 与 bfs 区别 dfs是沿着一个方向去搜&#xff0c;不到黄河不回头&#xff0c;直到搜不下去了&#xff0c;再换方向&#xff08;换方向的过程就涉及到了回溯&#xff09;。 bfs是先把本节点所连接的所有节点遍历一遍&#xff0c;走到下…

智能穿戴终端设备安卓主板方案_MTK平台智能手表PCBA定制开发

新移科技智能手表方案兼容WiFi、BLE、2~5G等多种通信能力。支持多个功能模块&#xff0c;包括&#xff1a;通话、计步、定位、睡眠监测、心率监测、血氧监测等。智能手表通过滑动与功能性按键提供高度直观的体验感受&#xff0c;从腕间即可掌控日常生活。形态支持定制包括&…

专项技能训练五《云计算网络技术与应用》实训6-2:ryu控制器安装

文章目录 ryu控制器安装1. 使用VMware安装CentOS 7虚拟机&#xff0c;安装时需添加多一张网卡&#xff0c;该网卡为自定义-VMnet1.并且记得开启CPU虚拟化&#xff0c;将其命名为“OVS1”。2. 安装完虚拟机后&#xff0c;进入虚拟机&#xff0c;修改网络配置&#xff08;onboot改…

Vim学习笔记01~04

第01章&#xff1a; 遁入空门&#xff0c;模式当道 1.什么是vim Vim是一个高效的文本编辑工具&#xff0c;并且可以在编程开发过程中发挥越来越重要的作用。 事实上&#xff0c;有不少编程高手使用他们来进行代码的开发&#xff0c;并且对此赞不绝口。 2.本系列目的 但是让…

使用selenium时出现element click intercepted报错的解决办法

win10&#xff0c;python3.8.10。 selenium版本如下&#xff08;用pip38 show selenium查看&#xff09;&#xff1a; 在定位中&#xff0c;定位了一个按钮&#xff08;特点&#xff1a;button下还有span然后才是文本&#xff09;&#xff0c;代码如下&#xff1a; from sele…

✪✪✪宁波国家实验室认可✪✪✪

&#x1f60d;哇塞&#xff01;&#x1f389;大新闻来啦&#xff01;&#x1f97c;宁波国家实验室&#x1f618;终于获得了CNAS的认可&#xff01;&#x1f50d;&#x1f52c; &#x1f468;‍&#x1f3a8;对于我们这些热爱科学、&#x1f469;‍&#x1f52c;关注实验室动态…