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

Mybatis之常用动态Sql语句

数据库结构、实体类

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语句中进行执行

在接口文件中写入

List<Youth> getYouthBywhereif(Youth youth);

在映射文件中写入

<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>

测试类中写入

@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多用于选择性修改操作上 它会自动去掉条件语句结尾多余的 ,

在映射文件中写入

<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>

在测试类中写入

@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 的格式


http://www.kler.cn/a/534181.html

相关文章:

  • 【现代深度学习技术】深度学习计算 | 延后初始化自定义层
  • MySQL表的CURD
  • 说一下JVM管理的常见参数
  • 基于springboot的体质测试数据分析及可视化设计
  • 怀旧经典:1200+款红白机游戏合集,Windows版一键畅玩
  • C# List 列表综合运用实例⁓Hypak原始数据处理编程小结
  • 云原生周刊:K8s引领潮流
  • Android 中APK 体积优化的几种方法
  • 【科研】 -- 医学图像处理方向,常用期刊链接
  • Python:温度转化
  • pandas习题 070:将数据库中大数据分块读取计算
  • 2.4学习
  • TCP三次握手、四次挥手过程及原理
  • swift 专题三 swift 规范一
  • 5.6 Mybatis代码生成器Mybatis Generator (MBG)实战详解
  • 在 Debian/Ubuntu 系统上,永久固定网口的IP地址
  • 基于 SpringBoot3 的 SpringSecurity6 + OAuth2 自定义框架模板
  • DeepSeek R1技术报告关键解析(8/10):DeepSeek-R1 的“aha 时刻”,AI 自主学习的新突破
  • Linux zcat 命令使用详解
  • labview通过时间计数器来设定采集频率
  • vue2-v-if和v-for的优先级
  • Ubuntu添加硬盘踩坑日志:SMB无权限的问题
  • 【前端构建】Rollup: 下一代 JavaScript 模块打包器
  • 【Elasticsearch】文本分类聚合Categorize Text Aggregation
  • 路由器及工作原理与常用路由配置命令详解
  • Redis主从模式与哨兵模式详解及案例分析