MyBatis入門:快速搭建數據庫操作框架 + 增刪改查(CRUD)

一、創建Mybatis的項目

Mybatis 是?個持久層框架, 具體的數據存儲和數據操作還是在MySQL中操作的, 所以需要添加MySQL驅動

1.添加依賴

或者 手動添加依賴

        <!--Mybatis 依賴包--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.4</version></dependency><!--mysql驅動包--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency>

項??程創建完成后,?動在pom.xml?件中,導?Mybatis依賴和MySQL驅動依賴

版本會隨著SpringBoot 版本發?變化

SpringBoot 3.X對?MyBatis版本為3.X

對應關系參考:Introduction – mybatis-spring-boot-autoconfigure

2.配置常見的數據庫信息

這是常見關于Mybatis的配置信息,大家可以自取

csdn暫時還不支持yml文件,這是 yml 文件:

spring:datasource:                      # 配置數據庫名url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=falseusername: root  #數據庫用戶password: root  #密碼driver-class-name: com.mysql.cj.jdbc.Driver# 設置 Mybatis 的 xml 保存路徑
mybatis:  mapper-locations: classpath:mapper/*Mapper.xmlconfiguration: # 配置打印 MyBatis 執行的 SQLlog-impl: org.apache.ibatis.logging.stdout.StdOutImplmap-underscore-to-camel-case: true  #自動駝峰轉換

前提是有這樣的一個數據庫

那么這里提供一個數據庫:

-- 創建數據庫
DROP DATABASE IF EXISTS mybatis_test;CREATE DATABASE mybatis_test DEFAULT CHARACTER SET utf8mb4;-- 使用數據數據
USE mybatis_test;-- 創建表[用戶表]
DROP TABLE IF EXISTS user_info;
CREATE TABLE `user_info` (`id` INT ( 11 ) NOT NULL AUTO_INCREMENT,`username` VARCHAR ( 127 ) NOT NULL,`password` VARCHAR ( 127 ) NOT NULL,`age` TINYINT ( 4 ) NOT NULL,`gender` TINYINT ( 4 ) DEFAULT '0' COMMENT '1-男 2-女 0-默認',`phone` VARCHAR ( 15 ) DEFAULT NULL,`delete_flag` TINYINT ( 4 ) DEFAULT 0 COMMENT '0-正常, 1-刪除',`create_time` DATETIME DEFAULT now(),`update_time` DATETIME DEFAULT now() ON UPDATE now(),PRIMARY KEY ( `id` ) 
) ENGINE = INNODB DEFAULT CHARSET = utf8mb4; -- 添加用戶信息
INSERT INTO mybatis_test.user_info( username, `password`, age, gender, phone )
VALUES ( 'admin', 'admin', 18, 1, '18612340001' );
INSERT INTO mybatis_test.user_info( username, `password`, age, gender, phone )
VALUES ( 'zhangsan', 'zhangsan', 18, 1, '18612340002' );
INSERT INTO mybatis_test.user_info( username, `password`, age, gender, phone )
VALUES ( 'lisi', 'lisi', 18, 1, '18612340003' );
INSERT INTO mybatis_test.user_info( username, `password`, age, gender, phone )
VALUES ( 'wangwu', 'wangwu', 18, 1, '18612340004' );-- 創建文章表
DROP TABLE IF EXISTS article_info;CREATE TABLE article_info (id INT PRIMARY KEY auto_increment,title VARCHAR ( 100 ) NOT NULL,content TEXT NOT NULL,uid INT NOT NULL,delete_flag TINYINT ( 4 ) DEFAULT 0 COMMENT '0-正常, 1-刪除',create_time DATETIME DEFAULT now(),update_time DATETIME DEFAULT now() 
) DEFAULT charset 'utf8mb4';-- 插入測試數據
INSERT INTO article_info ( title, content, uid ) VALUES ( 'Java', 'Java正文', 1 );

這樣一個表

3.寫對應的對象

一般寫在model文件夾下

@Data
public class UserInfo {private Integer id;private String username;private String password;private Integer age;private Integer gender;private String phone;//數據庫用下劃線連接單詞,java直接用小駝峰private Integer deleteFlag;private Date createTime;private Date updateTime;}

4.寫持久層代碼

一般寫在mapper文件夾下,或者Dao

@Mapper
public interface UserInfoMapper {//查詢所有用戶的信息@Select("select * from user_info")List<UserInfo> selectAll();
}

5.單元測試

在對應的mapper接口下,右擊

@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid selectAll() {userInfoMapper.selectAll().forEach(x -> System.out.println(x));//等同于
//        List<UserInfo> userInfos = userInfoMapper.selectAll();
//        for (UserInfo userInfo : userInfos) {
//            System.out.println(userInfo);
//        }}
}

結果:

二、Mybatis的基本操作

1.日志打印

2.傳遞單個參數

通過 #{……} 傳遞 參數

@Mapper
public interface UserInfoMapper {//                 只有一個參數的時候這里寫什么名字無所謂@Select("select * from user_info where id = #{id}")UserInfo selectUserById(Integer id);
}

單元測試:

@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid selectUserById() {System.out.println(userInfoMapper.selectUserById(2));}
}

結果:

3.傳遞多個參數

3種方法

方法1:

標簽 和 方法 中的名字一樣

方法2:

它給方法的每個形參取了別名,例如第一個param1 第二個 param2

方法3:

使用@Param("……"),和 標簽中的 #{……}對應

@Mapper
public interface UserInfoMapper {//    @Select("select * from user_info where id = #{id} and gender = #{gender}")   方法1    推薦//    @Select("select * from user_info where id = #{param2} and gender = #{param1}")  方法2   不推薦//    @Select("select * from user_info where id = #{id2} and gender = #{gender2}")  錯誤//@Select("select * from user_info where id = #{id2} and gender = #{gender2}") // 方法3   推薦List<UserInfo> selectUserByIdAndGender(@Param("id2") Integer id,@Param("gender2") Integer gender);}

單元測試:

@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid selectUserByIdAndGender() {List<UserInfo> userInfos = userInfoMapper.selectUserByIdAndGender(3, 1);System.out.println(userInfos);}
}

結果:

返回的參數可以是對象,也可以是集合類:

當我們知道數據庫中對應的參數只有一個時,可以用類接收,但是最好用集合,萬一他人增加了符號相同條件的數據,一個類就裝不下。

4.查(Select)

查詢之前已經都寫到了,就不再寫了。

發現這樣的一個問題:

數據庫的規范是單詞之間用下劃線分割,java的變量規范是小駝峰命名。這就導致了屬性對應不上導致為null

解決辦法,我先講最推薦的:

4.1 開啟駝峰命名

在yml或者properties文件中設置:

# 設置 Mybatis 的 xml 保存路徑
mybatis:mapper-locations: classpath:mapper/*Mapper.xmlconfiguration: # 配置打印 MyBatis 執行的 SQLlog-impl: org.apache.ibatis.logging.stdout.StdOutImplmap-underscore-to-camel-case: true  #自動駝峰轉換

4.2 起別名

@Mapper
public interface UserInfoMapper {@Select("select id, username, password, age, gender, phone, delete_flag as deleteFlag, " +"create_time as createTime, update_time as updateTime from user_info")List<UserInfo> selectAll2();}

即便是不用起別名也不建議用 select *就應該用到哪一列寫哪一列,即便是所有的列都需要也這么寫,因為更加規范。

4.3 結構映射

4.3.1 @Results 和 @Result
@Mapper
public interface UserInfoMapper {//    @Select("select id, username, password, age, gender, phone, delete_flag as deleteFlag, " +
//            "create_time as createTime, update_time as updateTime from user_info")@Results({@Result(column = "delete_flag", property = "deleteFlag"),@Result(column = "create_time", property = "createTime"),@Result(column = "update_time", property = "updateTime")})@Select("select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info")List<UserInfo> selectAll2();
}

這樣比起別名麻煩呀,那么真實用法是這樣:

4.3.2 @ResultMap

5.增(Insert)

5.1傳遞對象

方法一:

@Mapper
public interface UserInfoMapper {@Insert("insert into user_info (username, `password`, age, gender) values (#{username},#{password},#{age},#{gender})")Integer insertUser(UserInfo userInfo);}

Mapper層

@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid insertUser() {UserInfo userInfo = new UserInfo();userInfo.setUsername("張三");userInfo.setPassword("123445");userInfo.setAge(19);userInfo.setGender(0);userInfoMapper.insertUser(userInfo);}}

成功:

方法二:

@Mapper
public interface UserInfoMapper {@Insert("insert into user_info (username, `password`, age, gender)" +"values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})")Integer insertUserByParam(@Param("userInfo") UserInfo userInfo);
}
@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid insertUserByParam() {UserInfo userInfo = new UserInfo();userInfo.setUsername("李四");userInfo.setPassword("jaba213");userInfo.setAge(19);userInfo.setGender(0);userInfoMapper.insertUser(userInfo);}
}

6.刪(Delete)

刪除的時候一般使用id刪除

假設我們現在是pdd的員工,我們下單了一個商品但是還沒有付錢,那么此時我們就需要拿到這個訂單的id,如果在10分鐘內不付錢,我們就刪除這個訂單。

那么我們就需要在插入之后拿到id,可以用這個注解:

6.1 @Options

@Mapper
public interface UserInfoMapper {@Delete("delete from user_info where id = #{id}")Integer delete(Integer id);@Options(useGeneratedKeys = true, keyProperty = "id")@Insert("insert into user_info (username, `password`, age, gender)" +"values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})")Integer insertUserByParam(@Param("userInfo") UserInfo userInfo);}

單元測試:

@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid insertUserByParam() {UserInfo userInfo = new UserInfo();userInfo.setUsername("wangmaz");userInfo.setPassword("54231");userInfo.setAge(19);userInfo.setGender(0);//返回影響的行數Integer result = userInfoMapper.insertUserByParam(userInfo);//                                                  通過getId()獲取            System.out.println("執行結果" + result + " ,id : " + userInfo.getId());}

結果:

那么拿到數據你想用這個id干什么自己處理就好了

普通的刪除:

@Mapper
public interface UserInfoMapper {@Delete("delete from user_info where id = #{id}")Integer delete(Integer id);}

單元測試:

@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid delete() {
//    刪除id為11的數據userInfoMapper.delete(11);}
}

運行前:

運行后:

7.改(Update)

@Mapper
public interface UserInfoMapper {@Update("update user_info set password = #{password} where id = #{id}")Integer update(Integer id, String password);
}
@Mapper
public interface UserInfoMapper {@Update("update user_info set password = #{password} where id = #{id}")Integer update(Integer id, String password);
}

修改前:

修改后 :

三、報錯信息

看到這樣的報錯“密碼錯誤”,直接去看配置信息

數據庫返回結構太多,方法定義的返回結果不匹配

標簽參數和方法參數不匹配

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

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

相關文章

基于Ubuntu的ros版本切換

解決在同一個虛擬機中管理兩個不同版本的ros 基于Ubuntu&#xff08;20.04&#xff09; ros版本1和版本2的切換 前期準備&#xff1a;已經在Ubuntu中安裝了兩個版本的ros&#xff0c;這里以版本1的noetic和版本2的foxy為例 在bashrc中&#xff1a; # ~/.bashrc: executed by…

vue2:橫向無限輪播

子組件 <template><div class"infinite-scroll" ref"scrollContainer"><div class"scroll-content" :style"{ transform: translateX(${scrollPosition}px) }"><div v-for"(item, index) in displayItems&q…

CVE-2021-44228源碼分析與漏洞復現

漏洞概述 漏洞名稱&#xff1a;Apache Log4j2 遠程代碼執行漏洞 漏洞編號&#xff1a;CVE-2021-44228 CVSS 評分&#xff1a;10.0 影響版本&#xff1a;Apache Log4j 2.0-beta9 至 2.14.1 修復版本&#xff1a;2.15.0、2.16.0 CVE-2021-44228 是 Apache Log4j2 日志框架中因 …

2025年Google I/O大會上,谷歌展示了一系列旨在提升開發效率與Web體驗的全新功能

每周跟蹤AI熱點新聞動向和震撼發展 想要探索生成式人工智能的前沿進展嗎&#xff1f;訂閱我們的簡報&#xff0c;深入解析最新的技術突破、實際應用案例和未來的趨勢。與全球數同行一同&#xff0c;從行業內部的深度分析和實用指南中受益。不要錯過這個機會&#xff0c;成為AI領…

【C++指南】string(三):basic_string底層原理與模擬實現詳解

. &#x1f493; 博客主頁&#xff1a;倔強的石頭的CSDN主頁 &#x1f4dd;Gitee主頁&#xff1a;倔強的石頭的gitee主頁 ? 文章專欄&#xff1a;《C指南》 期待您的關注 文章目錄 引言一、成員變量與內存管理1.1 核心成員變量1.2 內存分配策略 二、默認成員函數的實現與優化…

AWS云與第三方通信最佳實踐:安全、高效的數據交互方案

引言 在當今的云計算時代,企業經常需要在AWS云環境中存儲和處理數據,同時還需要與第三方應用或服務進行數據交互。如何安全、高效地實現這種通信是許多企業面臨的挑戰。本文將詳細探討幾種AWS云與第三方通信的方案,并分析它們的優缺點,幫助您為自己的業務場景選擇最佳解決…

AE THYRO-AX 功率控制器 THYRISTOR-LEISTUNGSSTELLER THYRISTOR POWER CONTROLLER

AE THYRO-AX 功率控制器 THYRISTOR-LEISTUNGSSTELLER THYRISTOR POWER CONTROLLER

【論文解讀】STaR:不用人類思維鏈指導,模型可以自我進化!

1st author: Eric Zelikman paper: STaR: Bootstrapping Reasoning With Reasoning | OpenReview NeurIPS 2022 code: ezelikman/STaR: Code for STaR: Bootstrapping Reasoning With Reasoning (NeurIPS 2022) 1. 當語言模型學會自我進化 Zelikman 等人提出的 STaR (Self-T…

大語言模型 19 - MCP FastAPI-MCP 實現自己的MCP服務 快速接入API

MCP 基本介紹 官方地址&#xff1a; https://modelcontextprotocol.io/introduction “MCP 是一種開放協議&#xff0c;旨在標準化應用程序向大型語言模型&#xff08;LLM&#xff09;提供上下文的方式。可以把 MCP 想象成 AI 應用程序的 USB-C 接口。就像 USB-C 提供了一種…

用Matlab對單目相機參數的標定步驟(保姆級教程)

前言 在圖像測量及機器視覺應用中&#xff0c;為確定空間物體表面某點的三維幾何位置與其在圖像中對應點之間的相互關系&#xff0c;必須建立相機成像的幾何模型&#xff0c;這些幾何模型參數就是相機參數。 ??在大多數條件下這些參數必須通過實驗與計算才能得到&#xff…

【后端高階面經:架構篇】46、分布式架構:如何應對高并發的用戶請求

一、架構設計原則:構建可擴展的系統基石 在分布式系統中,高并發場景對架構設計提出了極高要求。 分層解耦與模塊化是應對復雜業務的核心策略,通過將系統劃分為客戶端、CDN/邊緣節點、API網關、微服務集群、緩存層和數據庫層等多個層次,實現各模塊的獨立演進與維護。 1.1 …

SQL每日一題(5)

前言&#xff1a;五更&#xff01;五更琉璃&#xff01;不對&#xff01;是&#xff0c;五更佩可&#xff01; 原始數據&#xff1a; new_hires reasonother_column1other_column2校園招聘信息 11社會招聘信息 22內部推薦信息 33獵頭推薦信息 44校園招聘信息 55社會招聘信息…

Kafka Kraft模式集群 + ssl

文章目錄 啟用集群資源規劃準備證書創建相關文件夾配置文件啟動各Kafka節點 故障轉移測試spring boot集成 啟用集群 配置集群時關鍵就是提前梳理好需要的網絡資源&#xff0c;完成對應server.properties文件的配置。在執行前先把這些梳理好&#xff0c;可以方便后面的配置&…

watchEffect

在處理復雜異步邏輯時&#xff0c;Vue 3 的 watchEffect 相比傳統的 watch 具有以下優勢&#xff1a; 1. 自動追蹤依賴 watchEffect 會自動收集其回調中使用的所有響應式依賴&#xff0c;無需手動指定監聽源&#xff1a; import { ref, watchEffect } from vue;const count …

Linux系統平均負載與top、uptime命令詳解

介紹 在Linux系統運維中&#xff0c;系統平均負載是一個重要的性能指標。通過 top和 uptime命令&#xff0c;可以實時監控系統的負載情況&#xff0c;幫助運維人員及時發現并解決系統性能問題。本文將詳細介紹Linux系統平均負載的概念及其計算方法&#xff0c;并深入解析 top和…

前端配置nginx代理

一、定義靜態文件的路徑的兩種方式 1. root 指令 &#xff08;1&#xff09;作用 指定文件系統的 基礎路徑&#xff0c;location 的 URI 會 追加到該路徑后 形成完整路徑。 &#xff08;2&#xff09;語法 location /uri/ {root /path/to/files; } &#xff08;3&#xf…

語音識別技術在人工智能中的應用

姓名&#xff1a;成杰 學號&#xff1a;21021210653 學院&#xff1a;電子工程學院 【嵌牛導讀】 應用語音智能這項識別技術是為了使計算機可以聽懂人類的語言&#xff0c;并執行人類的某項操作。現階段這項技術已經成為人工智能領域的重點研究方向和實現人機語音交互的…

uniapp實現大視頻文件上傳-- 阿里云oss直傳方式 - app、H5、微信小程序

之前的項目文件上傳比較慢&#xff0c;使用預簽名方式上傳H5正常&#xff0c;微信小程序和app使用axios時出現了各種報錯&#xff0c;配置完后還是不行。所以換一種oss直傳方式。 找到一個 實現了的 參考:https://blog.csdn.net/qq_44860866/article/details/129670188

【Java學習筆記】抽象類

抽象類 引入關鍵字&#xff1a;abstract 應用場景&#xff1a;當子類中共有的部分 / 特性可以放到抽象類中 1. 通過子類的方法重寫實現不同的功能 2. 編寫一個方法把共有的部分放入其中&#xff0c;在該方法中調用抽象方法&#xff08;動態綁定機制&#xff09; 3. 便可以實…

EPT(Efficient Prompt Tuning)方法,旨在解決提示調優(Prompt Tuning)中效率與準確性平衡和跨任務一致性的問題

EPT(Efficient Prompt Tuning)方法,旨在解決提示調優(Prompt Tuning)中效率與準確性平衡和跨任務一致性的問題 一、核心原理:分解提示與多空間投影 1. 提示分解:用低秩矩陣壓縮長提示 傳統問題: 長提示(如100個token)精度高但訓練慢,短提示(如20個token)速度快但…