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

Mybatis 操作续集2(结合上文)

Mybatis 是一个持久层框架,用于简化数据库的操作,和Spring 没有任何关系,我们现在能使用它是因为 Spring Boot 把Mybatis 的依赖给引入进来了,在 pom.xml 里面

Mybatis 如何进行重命名?

看最后两行代码,这样就能重命名了

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper
public interface UserInfoMapper {@Select("select * from userInfo")List<UserInfo> selectAll();@Select("select * from userInfo where id = #{id}")UserInfo selectOne(Integer id);@Select("select * from userInfo where id= #{userId}")UserInfo selectOne2(@Param("userId")Integer id);
}

 Mybatis 的增操作

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper
public interface UserInfoMapper {@Insert(" insert into userinfo(username,password,age,gender,phone) " +"value(#{username},#{password},#{age},#{gender},#{phone})")Integer insert(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 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 方法,执行结果:{}",result);}
}

就能看到运行结果了 

然后在Mysql查查看有没有插入成功 

没毛病

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

相关文章:

  • LangChain 19 Agents Reason+Action自定义agent处理OpenAI的计算缺陷
  • 12.整数转罗马数字
  • 免费AI洗稿软件【2023最新】
  • PTA:平方回文数
  • 从“AI证件照”到“AI译制片”,爆款AIGC应用的商业化迷思
  • JAVA代码优化:Easy Excel(操作Excel文件的开源工具)
  • Linux Python ping3库使用教程(ping3命令、ping命令)
  • 分享一些基于php商城案例
  • SpringSecurity 三更草堂 学习笔记
  • 基于Java SSM仓库管理系统
  • 基于Spark对消费者行为数据进行数据分析开发案例
  • Docker镜像制作与推送
  • Pandas时序数据分析实践—基础(1)
  • 5.C转python
  • 输出SearchFacesResponse对象的JSON格式字符串回包乱码解决方案
  • P7 链表 链表头前方插入新节点
  • SCAU:主对角线上的元素之和
  • c语言——简单客户端demo
  • 日志检索场景ES->Doris迁移最佳实践:函数篇
  • 【高效开发工具系列】jackson入门使用
  • 深入理解网络非阻塞 I/O:NIO
  • Hdoop学习笔记(HDP)-Part.07 安装MySQL
  • [数据结构]HashSet与LinkedHashSet的底层原理学习心得
  • 使用unity开发Pico程序,场景中锯齿问题
  • Spring | Spring的基本应用
  • 项目开发维护技术文档(梳理总结中)
  • 【接口测试】Apifox实用技巧干货分享
  • 车联网架构设计(一)_消息平台的搭建
  • (蓝桥杯)1125 第 4 场算法双周赛题解+AC代码(c++/java)
  • 也可Adobe Animate