Mybatis 操作续集(结合上文)

news/2025/2/21 13:28:15/

当我们增加一个数据之后,如果我们想要获取它的 Id 进行别的操作,我们该如何获取 Id 呢?

用那个@Options

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface UserInfoMapper {@Options(useGeneratedKeys = true,keyProperty = "id")@Insert(" insert into userinfo(username,password,age,gender,phone) " +"value(#{username},#{password},#{age},#{gender},#{phone})")Integer insert(UserInfo userInfo);}

然后log.info 里面加上userinfo.getId 

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;import static org.junit.jupiter.api.Assertions.*;@Slf4j
@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid insert() {UserInfo userInfo = new UserInfo();userInfo.setUsername("zhaoliu");userInfo.setPassword("123");userInfo.setAge(45);userInfo.setGender(0);userInfo.setPhone("1775423");Integer result = userInfoMapper.insert(userInfo);log.info("insert 方法,执行结果:{},自增ID:{}",result,userInfo.getId());}
}

这样就能在日志中拿到这个Id了 

我们在 Mysql 中看到确实是8 

接下来,对 insert 的参数如何进行重命名呢? 

(就是username和password这些属性前面都加上重命名之后的名字,比如重命名为userinfo,就写为userinfo.username,还有@Param()括号里面写下要改的名字)

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface UserInfoMapper {@Options(useGeneratedKeys = true,keyProperty = "id")@Insert(" insert into userinfo(username,password,age,gender,phone) " +"value(#{username},#{password},#{age},#{gender},#{phone})")Integer insert(UserInfo userInfo);@Options(useGeneratedKeys = true,keyProperty = "id")@Insert(" insert into userinfo(username,password,age,gender,phone) " +"value(#{userinfo.username},#{userinfo.password},#{userinfo.age},#{userinfo.gender},#{userinfo.phone})")Integer insert2(@Param("userinfo") UserInfo userInfo);}

然后依旧右键generate,test,勾选,ok

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;import static org.junit.jupiter.api.Assertions.*;@Slf4j
@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid insert2() {UserInfo userInfo = new UserInfo();userInfo.setUsername("zhaoliu");userInfo.setPassword("123");userInfo.setAge(45);userInfo.setGender(0);userInfo.setPhone("1775423");Integer result = userInfoMapper.insert(userInfo);log.info("insert 方法,执行结果:{},自增ID:{}",result,userInfo.getId());}
}

这样就能成功了 

接下来是删除操作

 

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface UserInfoMapper {@Delete("delete from userinfo where id=#{id}")Integer delete(Integer id);//想根据什么条件删除,括号就传什么条件}

右键,generate,test,勾选,ok

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;import static org.junit.jupiter.api.Assertions.*;@Slf4j
@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid delete() {userInfoMapper.delete(9);}
}

这样就运行成功了 

我们就会发现Id为 9 的数据被删除了 

 上面的 select 是 delete之前查找的,下面的是 delete 之后查找的

接下来是"改"操作 

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface UserInfoMapper {@Update("update userinfo set age=#{age} where id=#{id}")Integer update(UserInfo userInfo);}

右键,generate,test,勾选,OK

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;import static org.junit.jupiter.api.Assertions.*;@Slf4j
@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid update() {UserInfo userInfo = new UserInfo();userInfo.setAge(78);userInfo.setId(7);Integer result=userInfoMapper.update(userInfo);//受影响的行数,也就是一般Mysql执行一段语句之后的最后一行if (result>0){//如果受影响的行数大于0,说明执行成功了log.info("数据修改成功");}}
}

 

Id 为 7 的年龄改为 78 

 


http://www.ppmy.cn/news/1256632.html

相关文章

GEE:Sobel算子卷积和Roberts算子卷积对比

作者:CSDN @ _养乐多_ 本文介绍了Sobel算子卷积和Roberts算子卷积操作的代码,并进行了图像对比,可以观察到两个算子的细微差异。 文章目录 一、Sobel算子和Roberts算子对比二、完整代码三、代码链接一、Sobel算子和Roberts算子对比 详细介绍介绍参考《遥感数字图像处理教程…

【C++】赋值运算符重载

💐 🌸 🌷 🍀 🌹 🌻 🌺 🍁 🍃 🍂 🌿 🍄🍝 🍛 🍤 📃个人主页 :阿然成长日记 …

Facebook推广工具功能科普!

随着社交媒体的普及,Facebook已经成为全球使用最广泛的社交平台之一,对于广大营销人员来说,利用Facebook推广工具进行营销已经成为不可或缺的一部分。 那么,这些推广工具到底有哪些功能呢?本文将为您揭秘Facebook推广工具的强大…

linux之tcpdump命令

在日常系统维护中,遇到需要抓包的需求,下面对linux环境使用tcpdump命令进行抓包进行分析记录。tcpdump命令可用抓取流动在网卡上的数据包。 1、tcpdump常用参数说明 -c : 抓包次数,达到指定次数后停止抓包-C : 文件大小,写入文件…

win10、11系统安装班智达藏文输入法并正常使用(完美解决)

1. 结果图 2. 先闲聊两句 班智达输入法对于藏语初学者可谓是太好用了(哈哈)特别是联想提示的功能。不禁为开发团队点个赞。 表扬完了该批评批评了。班智达输入法的安装难度真是一言难尽。也许是开发者没有继续维护的缘故吧。想当年,哪个藏语…

【USRP】5G / 6G 原型系统 5g / 6G prototype system

面向5G/6G科研应用 USRP专门用于5G/6G产品的原型开发与验证。该系统可以在实验室搭建一个真实的5G 网络,基于开源的代码,专为科研用户设计。 软件无线电架构,构建真实5G移动通信系统 X410 采用了目前流行的异构式系统,融合了FP…

Android textView 显示: STRING_TOO_LARGE

在Android中,字符串资源的长度限制是32KB,getString()方法返回的字符串资源的大小超过这个限制,就会抛出STRING_TOO_LARGE 这个错误。 我本地的临界值是:32.3 KB (33,090 字节) 小于等于33090时,能正常显示&#xff…

什么是离岸公司?有什么作用?

离岸公司是泛指在离岸法区内依据其离岸公司法规范成立的有限责任公司或股份有限公司。这些公司不能在注册地经营,而主要是在离岸法区以外的地方开展业务活动。离岸公司的主要特点包括高度保密性、无外汇管制和减免税务负担。 离岸公司的作用主要有以下几个方面&…