mybatisplus查詢練習代碼

mybatisplus查詢練習代碼

package com.yase;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yase.entity.Student;
import com.yase.entity.Teacher;
import com.yase.mapper.StudentMapper;
import com.yase.mapper.TeacherMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@SpringBootTest
public class TestSelect {@Resourceprivate TeacherMapper mapper;@Resourceprivate StudentMapper studentMapper;@Testpublic void selectById(){Teacher teacher = mapper.selectById(105);System.out.println(teacher);}@Testpublic void selectByIds(){List<Teacher> list = mapper.selectBatchIds(Arrays.asList(105, 106, 107));list.forEach(System.out::println);}@Testpublic void selectByMap(){Map<String,Object> map = new HashMap<>();map.put("teacher_name","李四");map.put("gender","女");List<Teacher> list = mapper.selectByMap(map);list.forEach(System.out::println);}@Testpublic void exists(){QueryWrapper<Teacher> wrapper = new QueryWrapper<>();wrapper.eq("teacher","張三");boolean exists = mapper.exists(wrapper);System.out.println(exists);}@Testpublic void count(){QueryWrapper<Teacher> wrapper =new QueryWrapper<>();wrapper.like("teacher_name","張");Long aLong = mapper.selectCount(wrapper);System.out.println(aLong);}@Testpublic void selectMaps(){QueryWrapper<Teacher> wrapper = new QueryWrapper<>();wrapper.like("teacher","張");List<Map<String, Object>> list = mapper.selectMaps(wrapper);list.forEach(map->map.forEach((key,value) -> System.out.println(key+" "+value)));}//只返回第一個字段的值@Testpublic void selectObjs(){QueryWrapper<Teacher> wrapper = new QueryWrapper<>();wrapper.like("teacher_name","張");List<Object> objects = mapper.selectObjs(wrapper);objects.forEach(System.out::println);}@Testpublic void selectList() {//查詢全部數據,返回list<teacher>List<Teacher> list = mapper.selectList(null);list.forEach(System.out::println);}//and查詢方式@Testpublic void selectList1() {QueryWrapper<Teacher> wrapper = new QueryWrapper<>();wrapper.like("teacher_name", "張").eq("gender", "女");List<Teacher> list = mapper.selectList(wrapper);list.forEach(System.out::println);}//or查詢方式@Testpublic void selectList2() {QueryWrapper<Teacher> wrapper = new QueryWrapper<>();wrapper.like("teacher_name", "張").or().eq("gender", "女");List<Teacher> list = mapper.selectList(wrapper);list.forEach(System.out::println);}//查詢性別是女,并且年齡大于23的女性@Testpublic void selectList3() {QueryWrapper<Teacher> wrapper = new QueryWrapper<>();wrapper.like("teacher_name", "張").eq("gender", "女").or().gt("age", 23);List<Teacher> list = mapper.selectList(wrapper);list.forEach(System.out::println);}//查詢性張,并且(性別是女年齡大于23的女性)用and包括@Testpublic void selectList4() {QueryWrapper<Teacher> wrapper = new QueryWrapper<>();wrapper.likeRight("teacher_name", "張").and(i -> i.eq("gender", "女").or().gt("age", 23));List<Teacher> list = mapper.selectList(wrapper);list.forEach(System.out::println);}//按照年齡排序,相同用id降序排序@Testpublic void selectListOrderBy() {QueryWrapper<Teacher> wrapper = new QueryWrapper<>();wrapper.orderByDesc("age").orderByDesc("id");List<Teacher> list = mapper.selectList(wrapper);list.forEach(System.out::println);}//子查詢@Testpublic void selectListSub() {QueryWrapper<Student> wrapper = new QueryWrapper<>();wrapper.inSql("tid", " select id from teacher where teacher_name = '張老師'");List<Student> students = studentMapper.selectList(wrapper);students.forEach(System.out::println);}//分頁插件@Testpublic void selectPage() {//需要在構造方法里傳入2個參數,一個頁碼,一個每頁數量Page<Student> page = new Page<>(1, 5);QueryWrapper<Student> wrapper = new QueryWrapper<>();wrapper.likeRight("student_name", "李");//page里真正的數據List<Student> records = page.getRecords();records.forEach(System.out::println);studentMapper.selectPage(page, wrapper);System.out.println("總記錄數:" + page.getTotal() +"總頁數" + page.getPages() +"當前頁數" + page.getCurrent() +"每頁顯示" + page.getSize() + "條數據" +"是否有上一頁" + page.hasPrevious() +    //bollean"是否有下一頁" + page.hasNext()         //bollean);}@Testpublic void selectMapsPage(){Page<Map<String,Object>> page = new Page<>(1, 5);QueryWrapper<Student> wrapper = new QueryWrapper<>();wrapper.likeRight("student_name", "李");studentMapper.selectMapsPage(page, wrapper);//page里真正的數據List<Map<String,Object>> records = page.getRecords();records.forEach(map->map.forEach((key,value)-> System.out.println(key+" "+value)));System.out.println("總記錄數:" + page.getTotal() +"總頁數" + page.getPages() +"當前頁數" + page.getCurrent() +"每頁顯示" + page.getSize() + "條數據" +"是否有上一頁" + page.hasPrevious() +    //bollean"是否有下一頁" + page.hasNext()         //bollean);}//自定義查詢@Testpublic void selectCustomer(){List<Student> list = studentMapper.selectCustomer();list.forEach(System.out::println);}}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/12409.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/12409.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/12409.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

什么是CCRC?做什么用的?

CCRC&#xff08;中國網絡安全審查認證和市場監管大數據中心&#xff09;原名為中國網絡安全審查技術與認證中心&#xff0c;也被稱為中國信息安全認證中心&#xff08;ISCCC&#xff09;。 該中心是經中央機構編制委員會辦公室批準成立的&#xff0c;其主要職責是依據國家法律…

kafka集群傳統部署(raft模式)—— 筑夢之路

kafka二進制包&#xff1a;https://dlcdn.apache.org/kafka/3.7.0/kafka_2.13-3.7.0.tgz 集群規劃 主機名IP地址節點ID角色分配kafka1192.168.100.1001broker,controllerkafka2192.168.100.1012broker,controllerkafka3192.168.100.1023broker,controller 編輯配置文件 con…

代碼隨想錄算法訓練營第36天|● 738.單調遞增的數字 ● 968.監控二叉樹

738. 單調遞增的數字 發現第一位變小了其他的迅速變9 class Solution:def monotoneIncreasingDigits(self, n: int) -> int:strnlist(str(n))for i in range(len(strn)-1,0,-1):if strn[i-1]>strn[i]:strn[i-1]str(int(strn[i-1])-1)for j in range(i,len(strn)):strn[…

超級簡單的地圖操作工具開發可疑應急,地圖畫點,畫線,畫區域,獲取地圖經緯度等

使用echars的地圖畫點,畫線,畫區域,獲取地圖經緯度等 解壓密碼:10086007 地圖也是用臨時的bmap.js和china.js純離線二選一 一共就這么多文件 畫點,畫線,畫區域 點擊地圖獲取經緯度-打印到控制臺,這樣就能渲染航跡,多變形,結合其他算法算圓等等操作 下載資源:https://download…

JSON-server 服務的搭建

1、全局安裝&#xff1a; pnpm i -g json-server2、創建db.json文件 {"posts": [{"id": 1,"title": "json-server","author": "typicode"}],"comments":[{"id": 1,"body": "…

什么情況下會造成索引失效?

2.3.4. 索引失效 對索引使用左或者左右模糊匹配 使用左或者左右模糊匹配的時候&#xff0c;也就是 like %xx 或者 like %xx% 這兩種方式都會造成索引失效。但是如果前綴是確定的那么就可以使用到索引&#xff0c;例如 name like 許%。 因為索引 B 樹是按照「索引值」有序排列…

SpringBoot 中 zip 文件解壓工具類

SpringBoot 中 zip 文件解壓工具類 zip 文件解壓&#xff08;不支持密碼&#xff09; 相關 Maven 依賴 <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.6</version>…

練習題(2024/5/14)

1四數相加 II 給你四個整數數組 nums1、nums2、nums3 和 nums4 &#xff0c;數組長度都是 n &#xff0c;請你計算有多少個元組 (i, j, k, l) 能滿足&#xff1a; 0 < i, j, k, l < nnums1[i] nums2[j] nums3[k] nums4[l] 0 示例 1&#xff1a; 輸入&#xff1a;n…

代碼隨想錄訓練營Day28:貪心算法06

1.738單調遞增的數字 貪心策略&#xff1a;如果strNum[i]<strNum[i-1]那么strNum[i] 9,strNum[i-1]--;//比如87對應的最大的單調遞增的就是79. 具體實現&#xff1a; 對于遇到小于的情況&#xff1a;如果strNum[i]<strNum[i-1]那么strNum[i] 9,strNum[i-1]--;遍歷順…

linux phpstudy 重啟命令

[rootLinuxWeb phpstudy]# ./system/phpstudyctl restart 查看命令 1) phpstudy -start 啟動小皮面板 2) phpstudy -stop 停止小皮面板 3) phpstudy -restart 重啟小皮面板 4) phpstudy -status 查詢面板狀態 5) phpstudy -in…

OFDM802.11a的FPGA實現(十五)短訓練序列:STS(含Matlab和verilog代碼)

原文鏈接&#xff08;相關文章合集&#xff09;&#xff1a;OFDM 802.11a的xilinx FPGA實現 1.前言 在之前已經完成了data域數據的處理&#xff0c;在構建整個802.11a OFDM數據幀的時候&#xff0c;還剩下前導碼和signal域的數據幀&#xff0c;這兩部分的內容。 PLCP的前導部分…

Nodejs筆記2

模塊化 模塊化初體驗 模塊暴露數據 導入模塊 fs 寫絕對路徑 require寫相對路徑不會受到影響 ./../不能省略 js 和json文件后綴可以省略 如果存在 命名相同的js和json文件&#xff0c;優先導入js文件 導入文件夾時的情況 require導入模塊的基本流程 commonJS模塊…

其它高階數據結構①_并查集(概念+代碼+兩道OJ)

目錄 1. 并查集的概念 2. 并查集的實現 3. 并查集的應用 3.1 力扣LCR 116. 省份數量 解析代碼1 解析代碼2 3.2 力扣990. 等式方程的可滿足性 解析代碼 本篇完。 寫在前面&#xff1a; 此高階數據結構系列&#xff0c;雖然放在⑤數據結構與算法專欄&#xff0c;但還是作…

【數據可視化01】matplotlib實例介紹4之六邊形分箱圖

目錄 一、引言二、實例介紹 一、引言 hexbin是一個二維直方圖&#xff0c;其中箱子是六邊形&#xff0c;顏色表示每個箱子內的數據點數。 二、實例介紹 import matplotlib.pyplot as plt import numpy as np# Fixing random state for reproducibility np.random.seed(19680…

服務器利用率的神器腳本

在服務器管理的過程中&#xff0c;了解服務器的各項性能指標是至關重要的。無論是CPU的負載情況&#xff0c;內存使用情況&#xff0c;還是硬盤的存儲空間以及TCP連接狀態&#xff0c;這些都是我們判斷服務器健康狀態和性能的重要依據。然而&#xff0c;手動一項項去檢查這些指…

【MySQL】Mysql——安裝指南(Linux)

MySQL8.0.26-Linux版安裝 1. 準備一臺Linux服務器 云服務器或者虛擬機都可以; Linux的版本為 CentOS7; 2. 下載Linux版MySQL安裝包 3. 上傳MySQL安裝包 4. 創建目錄,并解壓 mkdir mysqltar -xvf mysql-8.0.26-1.el7.x86_64.rpm-bundle.tar -C mysql5. 安裝mysql的安裝包 …

pip鏡像源

1.1 清華大學 https://pypi.tuna.tsinghua.edu.cn/simple 1.2 阿里云 https://mirrors.aliyun.com/pypi/simple/ 1.3 網易 https://mirrors.163.com/pypi/simple/ 1.4 豆瓣 https://pypi.douban.com/simple/ 1.5 百度云 https://mirror.baidu.com/pypi/simple/ 1.6 中科大 ht…

uniapp vue 獲取天氣數據

獲取當前地址&#xff0c;通過高德天氣數據&#xff0c;來展示天氣溫度風度等數據 //獲取天氣 getWeather(){// 獲取天氣預報uni.request({url: https://restapi.amap.com/v3/weather/weatherInfo, data: {city: 長沙,// extensions:all,key: xxxxxxxxxx//自己的高德密鑰key},…

2024OD機試卷-轉盤壽司 (java\python\c++)

題目:轉盤壽司 題目描述 壽司店周年慶,正在舉辦 優惠活動 回饋新老客戶。 壽司轉盤上總共有 n 盤壽司,prices[i] 是第 i 盤壽司的價格, 如果客戶選擇了第 i 盤壽司,壽司店免費贈送客戶距離第 i 盤壽司最近的下一盤壽司 j,前提是 prices[j] < prices[i],如果沒有滿足…

RAG 面向 LLM: 基于檢索增強的大語言模型調研

摘要 作為 AI 領域最先進的技術之一,檢索增強生成(RAG)技術可以提供可靠和最新的外部知識,為眾多任務提供巨大的便利。特別是在 AI 生成內容(AIGC)時代,RAG 中檢索強大的提供額外知識的能力使得檢索增強生成能夠輔助現有生成式 AI 生產高質量輸出。最近,大語言模型(LLM)在語言…