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

MyBatis简单使用

步骤

一、添加依赖: mybatis,mariadb驱动
二、配置数据库

application.yml
spring:datasource:url: jdbc:mariadb://127.0.0.1:3306/数据库username: rootpassword: 20040814driver-class-name: org.mariadb.jdbc.Driver

三、添加Mapper映射

@Mapper
public interface UserInfoMapper {@Select("select *from userinfo") //接口方法的实现List<UserInfo> getUserInfoAll();
}

参数传递

单个参数

@Select("select * from userinfo where delete_flag = #{deleteFlag}")
List<UserInfo> getUserInfoByDeleteFlag(Integer flag)

多个参数

@Select("select * from userinfo where delete_flag = #{deleteFlag} and gender = #{gender}")
List<UserInfo> getUserInfo(@Paaram("flag")Integer flag, @Param("gender")Integer gender);

结果映射

当数据库字段和Java属性不一致时
一、取别名

@Select("select id, username, password, age, gender, phone,"+"delete_flag as deleteFlag, create_time as createTime, update_time as updateTime")+"from `userinfo`")
List<UserInfo> selectUserInfos();

二、结果映射

@Select("SELECT * FROM `userinfo`")
List<UserInfo> selectUserInfos();

复用结果映射关系

@ResultMap("resultMap")
@Select("select * from userinfo")
List<UserInfo> getUserInfoAll();@Result(id="resultMap", value={@Result(column = "delete_flag", property="deleteFlag"),@Result(column = "create_time", property="createTime"),@Result(column = "update_time", property="updateTime")
})
@Select("select * from `userinfo`")
List<UserInfo> selectUserInfo();

开启驼峰自动转换

mybatis:configuration: map-underscore-to-camel-case: true

xml

<resultMap id="BaseMap" type="cn.cangli.model.UserInfo"><id column="id" property="id"/><!--主键--><!--普通字段--><result column="delete_flag" property="deleteFlag"/><result column="create_time" property="createTime"/><result column="update_time" property="updateTime"/>
</resultMap>
<select id="selectAllUser" resultMap="BaseMap">select * from userinfo
</select>
http://www.xdnf.cn/news/7823.html

相关文章:

  • 2025年度消费新潜力白皮书470+份汇总解读|附PDF下载
  • BAGEL-7B-MoT论文速读:统一多模态预训练的新特性
  • JUC高并发编程
  • 【笔记】快速安装Poetry
  • 138. Copy List with Random Pointer
  • Docker 镜像打包到本地
  • Android开发——不同布局的定位属性 与 通用属性
  • 大数据量查询优化:解锁SQL性能提升的关键
  • Node.js多版本安装工具NVM详细使用教程
  • VsCode开发环境之Node.js离线部署
  • JS 应用安全案例泄漏云配置接口调试代码逻辑框架漏洞自检
  • 华为鸿蒙电脑发布,折叠屏怎么选?
  • 实现动态增QuartzJob,通过自定义注解调用相应方法
  • OpenCV CUDA模块特征检测与描述------一种基于快速特征点检测和旋转不变的二进制描述符类cv::cuda::ORB
  • WPF核心类继承树结构
  • 学习路之uniapp--unipush2.0推送功能--服务端推送消息
  • Java安全-Servlet内存马
  • 基于多传感器融合的智能驾驶环境感知系统
  • 【java第19集】java面向对象编程详解
  • MyBatis:简化数据库操作的持久层框架
  • 高噪声下扩展边缘检测算子对检测边缘的影响
  • windows powershell 判断 进程号是否存在
  • 无人机桥梁巡检
  • linux文件重命名命令
  • MIL-C-5015航空插头2芯震动加速度传感器连接器
  • 五、【API 开发篇(下)】:使用 Django REST Framework构建测试用例模型的 CRUD API
  • 云原生安全之PaaS:从基础到实践的技术指南
  • 谈谈 Kotlin 中的构造方法,有哪些注意事项?
  • 【Django系统】Python+Django携程酒店评论情感分析系统
  • 【Java微服务组件】异步通信P2—Kafka与消息