MyBatis实例更新

本文详细介绍了如何在MyBatis中进行SQL预编译,提供不同字段别名处理方案,包括手动映射和驼峰命名规则。还涵盖了条件查询、XML映射文件以及动态SQL的使用实例,如if和foreach标签的应用。
摘要由CSDN通过智能技术生成

MyBatis具体

准备工作

image-20240412104642165

image-20240412111039722

image-20240412122804352

预编译SQL

image-20240412144835363

image-20240412145535703

image-20240412145616796

image-20240412151403387

image-20240412151906188

image-20240412152122710

新增

image-20240412152308214

image-20240412154220712

image-20240412155044983

image-20240412155356002

image-20240412155720156

更新

image-20240412161805589

查询

image-20240412162433393

image-20240412162634561

image-20240412162742454

//查询员工
    @Select("select * from emp where id = #{id}")
    public Emp getById(Integer id);

    //方案一:给字段起别名,让别名与实体类属性一致
    @Select("select id,username,password,name,gender,image,job,entrydate,"
            +" dept_id deptId,create_time createTime,update_time updateTime from emp where id = #{id}")
    public Emp getById(Integer id);

    //方案二:通过@Results @Result注解手动映射封装方案(繁琐,不用)
    @Results({
            @Result(column = "dept_id",property = "deptId"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select * from emp where id = #{id}")
    public Emp getById(Integer id);

    //方案三:开启mybatis的驼峰命名自动映射开关 a column ---> aColumn 在application.properties增加语句
    //mybatis.configuration.map-underscore-to-camel-case=true

image-20240412164521828

条件查询

image-20240412165646758

image-20240412165814437

image-20240412171030093

image-20240412171438556

image-20240412171800744

xml映射文件

image-20240412191556776

image-20240412194132759

image-20240412194720057

image-20240412194854401

image-20240412194938434

Mybatis动态SQL

image-20240412195740201

image-20240412195831316

image-20240412203453838

if

image-20240412205201792

if例子

image-20240412210648020

image-20240412213826110

image-20240412213848040

foreach

image-20240412215239221

sql include

image-20240412215535668

image-20240412215616337

image-20240412220058473

image-20240412220339095

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值