当前位置: 首页 > news >正文

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 

 

http://www.lryc.cn/news/251773.html

相关文章:

  • JVM基础篇:垃圾回收
  • 蓝桥杯ACwing习题
  • vue发送请求携带token,拼接url地址下载文件
  • 【PTA-C语言】编程练习3 - 循环结构Ⅱ
  • Google Chrome 下载 (离线版)
  • 2023年GopherChina大会-核心PPT资料下载
  • 从源代码出发,Jenkins 任务排队时间过长问题的解决过程
  • openssl 生成CA及相关证书
  • App测试之App日志收集及adb常用命令
  • 【Java面试——并发基础、并发关键字】
  • 如何使用 Java 在Excel中创建下拉列表
  • Python安装步骤介绍
  • 学习80min快速了解大型语言模型(ChatGPT使用)笔记
  • SQL错题集1
  • uniapp运行到安卓基座app/img标签不显示
  • vscode非常好用的扩展插件
  • 一文弄懂BFS【广度优先搜索(Breadth-First Search)】
  • 深度学习记录--logistic回归函数的计算图
  • Java基本数据类型详解
  • 第十五届蓝桥杯模拟赛(第二期)
  • 命令模式-C++实现
  • 3dMax拼图生成工具Puzzle2D使用教程
  • git报错invalid object xxx和unable to read tree xxxxxx
  • 会泽一村民上山放羊吸烟引发森林火灾,AI科技急需关注
  • docker-compose部署zabbix+grafana
  • ios 逆向分分析,某业帮逆向算法(二)
  • openCv颜色矩
  • 〖大前端 - 基础入门三大核心之JS篇㊹〗- DOM事件委托
  • 正是阶段高等数学复习--函数极限的计算
  • Linux-usb触摸板去除鼠标箭头