Mybatis 操作续集(结合上文)

2023-12-04 07:28

本文主要是介绍Mybatis 操作续集(结合上文),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

当我们增加一个数据之后,如果我们想要获取它的 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 

 

这篇关于Mybatis 操作续集(结合上文)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/452633

相关文章

MyBatis与其使用方法示例详解

《MyBatis与其使用方法示例详解》MyBatis是一个支持自定义SQL的持久层框架,通过XML文件实现SQL配置和数据映射,简化了JDBC代码的编写,本文给大家介绍MyBatis与其使用方法讲解,... 目录ORM缺优分析MyBATisMyBatis的工作流程MyBatis的基本使用环境准备MyBati

C#中的 Dictionary常用操作

《C#中的Dictionary常用操作》C#中的DictionaryTKey,TValue是用于存储键值对集合的泛型类,允许通过键快速检索值,并且具有唯一键、动态大小和无序集合的特性,常用操作包括添... 目录基本概念Dictionary的基本结构Dictionary的主要特性Dictionary的常用操作

C# winform操作CSV格式文件

《C#winform操作CSV格式文件》这篇文章主要为大家详细介绍了C#在winform中的表格操作CSV格式文件的相关实例,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录实例一实例效果实现代码效果展示实例二实例效果完整代码实例一实例效果当在winform界面中点击读取按钮时 将csv中

springboot3.4和mybatis plus的版本问题的解决

《springboot3.4和mybatisplus的版本问题的解决》本文主要介绍了springboot3.4和mybatisplus的版本问题的解决,主要由于SpringBoot3.4与MyBat... 报错1:spring-boot-starter/3.4.0/spring-boot-starter-

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤

《SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤》本文主要介绍了SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤,文中通过示例代码介绍的非常详... 目录 目标 步骤 1:确保 ProxySQL 和 mysql 主从同步已正确配置ProxySQL 的

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec