mybatis通过if语句增删改查

本文介绍了如何在MyBatis中利用IF语句进行高效地增删改查操作,避免为不同属性创建多个接口。通过实例展示了在DAO层、Mapper文件、Service层和Controller层的代码实现,以及测试过程,验证了通过age和sno属性进行查询和删除的可行性。
摘要由CSDN通过智能技术生成

有时候为了简化我们的代码。

1 举个例子

Student类:

@Data
public class Student {
   
    private Integer id;
    private Integer age;
    private Integer sno;
}

有时候我们想通过age这个属性获取Student对象
有时候我们也想通过sno这个属性获取Student对象

难道我们在DAO层写两个接口?
比如这样子?

Student getStudentByAge(Int age);
Student getStudentBySno(Int sno);

那么在mapper文件中要这样写?

    <select id="getStudentByAge" parameterType="int" resultMap="studentMap">
        select * from student where age=#{
   age}
    </select>
    <select id="getStudentBySno" parameterType="int" resultMap="studentMap">
        select * from student where sno=#{
   sno}
    </select>

显然,这样子是不高效的

2 上手测试 实验

实体类 Student:

@Data
public class Student {
   
    @ApiModelProperty(name = "id",example = "1",position = 1)
    private Integer id;
    @ApiModelProperty(name = "age"
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值