Mybatis之常用动态Sql语句

数据库结构、实体类

java 复制代码
public class Youth {

    private Integer id;
    private String username;
    private Date birthday;
    private Character sex;
    private String address;
    private Integer age;


    public Youth(Integer id, String username, Date birthday, Character sex, String address) {
        this.id = id;
        this.username = username;
        this.birthday = birthday;
        this.sex = sex;
        this.address = address;
    }

    public Youth() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Character getSex() {
        return sex;
    }

    public void setSex(Character sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Youth{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", sex=" + sex +
                ", address='" + address + '\'' +
                ", age=" + age +
                '}';
    }
}

where-if

where-if多用于查询含有某个特征语句上 如果 where标签内的条件成立 它会自动去掉条件语句开头多余的 and 或者 or

它会通过判断是否符合test中的属性来进行选择性执行 会将满足此条件的内置语句进行嵌入到sql语句中进行执行

在接口文件中写入

java 复制代码
List<Youth> getYouthBywhereif(Youth youth);

在映射文件中写入

java 复制代码
<select id="getYouthBywhereif" resultType="youth">
        select * from youth
        <where>
            <if test=" username != null and username !='' ">
                and username = #{username}
            </if>
            <if test="birthday != null">
                and birthday = #{birthday}
            </if>
            <if test="sex!=null and sex!=''"  >
                and sex= #{sex}
            </if>
            <if test="address!=null and address!=''">
                and address = #{address}
            </if>
            <if test="age!=null">
                and age = #{age}
            </if>
        </where>
    </select>

测试类中写入

java 复制代码
@Test
    public void getYouthBywhereifTest(){
        Youth youth = new Youth();
        youth.setSex('男');
        youth.setAddress("北京");
        List<Youth> youths = youthMapper.getYouthBywhereif(youth);
        for(Youth y:youths){
            System.out.println(y);
        }
    }

执行结果为

set-if

set-if多用于选择性修改操作上 它会自动去掉条件语句结尾多余的 ,

在映射文件中写入

java 复制代码
<update id="updateYouthBysetif" parameterType="youth">
        update youth
        <set>
            <if test="username!=null and username!=''">
                username = #{username},
            </if>
            <if test="birthday!=null">
                birthday =#{birthday},
            </if>
            <if test="sex!=null and sex!=''">
                sex = #{sex},
            </if>
            <if test="address!=null and address!=''">
                address=#{address},
            </if>
            <if test="age!=null">
                age = #{age}
            </if>
        </set>
        <where>
            id=#{id}
        </where>
    </update>

在测试类中写入

java 复制代码
@Test
    public void updateYouthBysetifTest(){
        Youth youth = new Youth(4,"张三", new Date(2019 - 1900,10 - 1,9),'男',"北京");
        int i = youthMapper.updateYouthBysetif(youth);
        if(i>0)
            System.out.println("修改成功");
        else
            System.out.println("修改失败");
    }

执行结果为

特别须知

  1. 在test中非字符串类型 不可使用 属性 != ' '
  2. 建议将实体类的属性使用包装类进行修饰 因为非包装类不能使用 类似于age != null 的格式
相关推荐
明月_清风7 分钟前
🚀 Flyway 存量数据库迁移:50张表一键导出清洗实战(附完整脚本)
数据库·后端
羊小蜜.15 分钟前
Mysql 08: 数据表基本操作——从创建到约束
数据库·mysql·数据表
程序员小郭8316 分钟前
MySQL分库分表策略全解析(实战版)
数据库·mysql·架构
卓怡学长18 分钟前
m320基于Java的网络音乐系统的设计与实现
java·数据库·spring·tomcat·maven
爱浦路 IPLOOK42 分钟前
选对UPF网元供应商,解锁5G-A时代行业赋能新可能
运维·网络·数据库
Elastic 中国社区官方博客1 小时前
将 Logstash 管道从 Azure Event Hubs 迁移到 Kafka 输入插件
大数据·数据库·elasticsearch·microsoft·搜索引擎·kafka·azure
草莓熊Lotso1 小时前
MySQL 事务管理全解:从 ACID 特性、隔离级别到 MVCC 底层原理
linux·运维·服务器·c语言·数据库·c++·mysql
鸽芷咕1 小时前
Oracle 替代工程实践深度解析:金仓全链路工程实践 —— 从评估决策到平滑上线的深度技术攻坚
数据库·oracle
gushinghsjj1 小时前
元数据管理包含哪些?元数据管理如何支持数据分析?
数据库·oracle·数据分析
PawSQL1 小时前
PawSQL平台功能月度更新 | 2026年2月
sql·pawsql·sql审核