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

MyBatis 基本操作 - 注解版

目录

一,查询 - @select

1.1 全列查询

1.2 指定列查询

1.3 赋值问题

方法一:起别名

方法二:结果映射

方法三:添加配置

二,新增 - @Insert

2.1 使用对象插入

2.2 获取主键

三,删除 - @Delete

四,修改 - @Update 

补:MyBatis 的配置


一,查询 - @select

1.1 全列查询

@Mapper
public interface UserInfoMapper {@Select("select * from userInfo")public UserInfo queryAll();
}

1.2 指定列查询

@Mapper
public interface UserInfoMapper {//注意:MyBatis中方法名不允许重复!!!!//当只有一个参数时,sql参数与方法参数可以不同@Select("select * from userInfo where id = #{id}")public UserInfo queryUserInfoById(Integer id);//当需要对参数进行重命名时需要使用@Param注释//如果不使用@Param,那么sql参数与方法参数必须相同//注:如果使用阿里云创建的springboot项目,必须使用@Param注解@Select("select * from userInfo where id = #{id} and username = #{username}")public UserInfo queryUserInfo(@Param("id") Integer i, @Param("username") String name);
}

 注:加了@Param注解后,sql语句中的参数名只能与@Param()的参数名匹配!!!

1.3 赋值问题

可以发现,在赋值时,deleteFlag,createTime,updateTime 并没有被赋值,这是为什么?原因分析:

  • 当⾃动映射查询结果时,MyBatis 会获取结果中返回的列名并在 Java 类中查找相同名字的属性(忽略⼤⼩写)。 这意味着如果发现了 ID 列和 id 属性,MyBatis 会将列 ID 的值赋给 id 属性
  • 所以没有赋值的原因就是MySQL中的字段名与Java对象中的属性名不相同

 

方法一:起别名

    @Select("select id, username, password, age, gender, phone," +"delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from userInfo " +"where id = #{id} and username = #{username}")public UserInfo queryUserInfo(@Param("id") Integer i, @Param("username") String name);

方法二:结果映射

    @Select("select * from userInfo where id = #{id}")@Results(id = "resultMap",value = {@Result(column = "delete_flag", property = "deleteFlag"),@Result(column = "create_time", property = "createTime"),@Result(column = "update_time", property = "updateTime")})public UserInfo queryUserInfo(@Param("id") Integer i, @Param("username") String name);@Select("select * from userInfo where id = #{id}")@ResultMap(value = "resultMap")//使用过@Results注解后,其他方法可以直接使用@ResultMap注解获得相同效果public UserInfo queryUserInfoById(Integer id);

方法三:添加配置

mybatis:configuration:map-underscore-to-camel-case: true #驼峰自动转换

二,新增 - @Insert

2.1 使用对象插入

使用多个参数插入数据与上述查询的使用方法一样,这里不过多赘述,这里讲一下如何使用对象插入数据:

@Mapper
public interface UserInfoMapper {//这里演示一下使用对象插入@Insert("insert into userInfo (username, password, age, gender, phone) " +"values (#{username}, #{password}, #{age}, #{gender}, #{phone})")public Integer insert(UserInfo userInfo);//使用@Param注解@Insert("insert into userInfo (username, password, age, gender, phone) " +"values (#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender}, #{userInfo.phone})")public Integer insertByParam(@Param("userInfo") UserInfo userInfo);//这里的返回值是成功修改了几行数据,也可以返回void
}

2.2 获取主键

在有些情景中,我们需要获取到新插入数据的 id,如果想要拿到自增 id,需要在Mapper接口方法上添加一个@Option注解:

    @Options(useGeneratedKeys = true, keyProperty = "id")@Insert("insert into userInfo (username, password, age, gender, phone) " +"values (#{username}, #{password}, #{age}, #{gender}, #{phone})")public Integer insert(UserInfo userInfo);
  • useGeneratedKeys = true:表示告诉MyBatis使用JDBC的getGeneratedKeys方法来检索数据库内部生成的主键(如果数据库支持的话)
  • keyProperty = ''id'':这个属性指定了userInfo对象中的 id 属性接收数据库生成的主键

三,删除 - @Delete

    @Delete("delete from userInfo where id = #{id}")public Integer deleteById(Integer id);

四,修改 - @Update 

    @Update("update userInfo set username = #{username} where id = #{id}")public Integer updateById(@Param("username") String username, @Param("id") Integer id);

补:MyBatis 的配置

mybatis:configuration: # 配置打印 MyBatis⽇志map-underscore-to-camel-case: true #驼峰自动转换log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句mapper-locations: classpath:mapper/**Mapper.xml #使用xml操作数据库时会用到

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

相关文章:

  • 专业比例阀放大器配套选型
  • Springboot 多数据源整合的三种方式
  • 【科研笔记】中国知网高级检索与专业检索针对同一检索内容返回的结果对比
  • C#-了解IOC控制反转及相关框架的使用
  • CSDN机器人与僵shi粉测试(真人看看)
  • 【C/C++ 多态中的虚函数的虚函数表】详细的了解一下吧(要先知道有虚函数表
  • 基于树莓派4B设计的智能家居控制系统(阿里云IOT)(203)
  • 太阳能光伏气象站的功能优势
  • LVS(Linux Virtual Server)负载均衡详解
  • C语言 | Leetcode C语言题解之第329题矩阵中的最长递增路径
  • rabbitmq学习记录
  • MySQL数据库基础:约束
  • Java设计模式和AOP编程
  • 【扒代码】data.py
  • 【时时三省】unity test 测试框架 介绍(适用于C语言进行测试的)
  • 那些你应该掌握的linux命令
  • 系统出现高CPU可能风险因素整理
  • 前端技术 -- 动画效果之GSAP作用与使用示例
  • C口一拖二数据线:解锁数字生活的便捷新篇章LDR6020
  • CH07_数据绑定
  • 24.python基础(8.8)
  • 【论文阅读】MobileNetV4 - Universal Models for the Mobile Ecosystem
  • 大模型日报 2024-08-07
  • 区块链ddos防护怎么做
  • 在Linux中认识pthread库
  • LVS 负载均衡
  • 在Excel中启用宏 (~ ̄▽ ̄)~
  • 连接投影仪/显示器只能扩展不能复制的解决方案
  • 数据库基础知识
  • java JVM 锁消除