JavaEE简单示例——动态SQL之更新操作<set>元素

文章介绍了在使用Mybatis时,如何通过set标签动态判断并更新学生数据,避免无用的资源浪费。示例展示了如何根据对象属性是否为空来决定是否执行SQL修改语句,提高了代码效率和运行效率。

简单介绍:

在之前我们做的学生管理系统的时候,曾经有一个环节是修改学生的数据。我们在修改的时候是必须将student对象的三个属性全部填入信息,然后全部修改才可以,这样会造成一个问题就是在我们明明只需要修改一个属性的时候却要把全部的属性都要修改,就会造成很多的资源浪费。而<set>标签就能帮助我们动态的判断某一个元素是不是为空值,是否需要修改。

使用方法:

<select id="唯一标识" resultType="结果集封装的实体类">

        update student

        <update>

                <if test="判断条件">

                        修改数据的SQL语句

                </if>

                <if test="判断条件">

                        修改数据的SQL语句

                </if>

        </set>

        where id = #{id}

</update>

代码实现:

SQL映射文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Mappers.dynamicSql">
  <select id="selectByIdOrName" parameterType="student" resultType="student">
    select * from student where 1=1
#                             当id的值不等于null并且id的值不是空字符的时候,就会拼接这个SQL语句
        <if test="id != null and id != ''">
          and id = #{id}
        </if>
#                             当name的值不等于null的时候并且name的值不是空字符串的时候,就会拼接这个SQL语句
        <if test="name != null and name != ''">
#                             注意这个地方是使用了一个concat函数将模糊匹配的百分号和参数进行拼接,在使用的时候注意这个地方不要写错
          and name like concat ('%',#{name},'%')
        </if>
  </select>
    <select id="selectAll" resultType="student">
        select * from student;
    </select>
<!--    动态SQL中的choose元素-->
<!--    当查询的条件满足第一个when的时候,就拼接第一个when里面的SQL语句-->
<!--    当查询的条件满足第二个when的时候,就拼接第二个when里面的SQL语句-->
<!--    当所有的when都不满足的时候,就拼接otherwise里面的SQL语句-->
<!--    当有多个when里面的条件都满足的时候,就拼接最靠上的一条SQL语句,并且不会执行其他when里面的语句-->
    <select id="selectStudentByIdAndName" resultType="student" >
        select * from student where 1=1
        <choose>
            <when test="name != null and name != ''">
                and name like concat('%',#{name},'%')
            </when>
            <when test="id != null and id != ''">
                and id = #{id}
            </when>
            <otherwise>
                and password is not null
            </otherwise>
        </choose>
    </select>
<!--    使用<where>来动态的处理where关键字是否添加-->
    <select id="selectByIdAndWhere" resultType="student" parameterType="student">
        select * from student
        <where>
            <if test="name != null and name !=''">
                and name like concat('%',#{name},'%')
            </if>
            <if test="id != null and id !=''">
                and id = #{id}
            </if>
        </where>
    </select>
<!--    使用set标签简化update的代码和运行效率-->
    <update id="updateBySet" parameterType="student">
        update student
            <set>
                <if test="name != null and name != ''">
                    name = #{name},
                </if>
                <if test="password != null and password != ''">
                    password = #{password},
                </if>
            </set>
        where id = #{id}
    </update>
</mapper>

接口类:

package Mappers;

import com.mybatis.POJO.student;

import java.util.List;

public interface dynamicSql {
    List<student> selectByIdOrName(student s);
    List<student> selectStudentByIdAndName(student s);
    List<student> selectByIdAndWhere(student s);
    int updateBySet(student s);
}

测试类:

package Mappers;

import com.mybatis.POJO.Tools.createSqlSession;
import com.mybatis.POJO.student;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class dynamicSqlTest {
    @Test
    public void selectByIdOrName(){
        SqlSession sqlSession = new createSqlSession().create();
        dynamicSql dynamicSql = new createSqlSession().createdynamicSql();
        student s = new student();
        s.setId(1);
        s.setName("张三");
        List<student> stu = sqlSession.selectList("Mappers.dynamicSql.selectByIdOrName", s);
        for(student student : stu){
            System.out.println(student.toString());
        }
    }
    @Test
    public void selectStudentByIdAndName(){
        dynamicSql dynamicSql = new createSqlSession().createdynamicSql();
        student s =new student();
//        s.setId(1);
//        s.setName("张三");
        for (student student : dynamicSql.selectStudentByIdAndName(s)) {
            System.out.println(student);
        }
    }
    @Test
    public void selectAll(){
        SqlSession sqlSession = new createSqlSession().create();
        dynamicSql dynamicSql = new createSqlSession().createdynamicSql();
        List<student> list = sqlSession.selectList("Mappers.dynamicSql.selectAll");
        for(student student : list){
            System.out.println(student.toString());
        }
    }
    @Test
    public void selectByIdAndWhere(){
        SqlSession sqlSession = new createSqlSession().create();
        dynamicSql dynamicSql = new createSqlSession().createdynamicSql();
        student s = new student();
//        s.setId(1);
//        s.setName("张三");
        for (student student : dynamicSql.selectByIdAndWhere(s)) {
            System.out.println(student.toString());
        }
    }
//    测试set标签插入数据的方法
    @Test
    public void updateBySet(){
        SqlSession sqlSession = new createSqlSession().create();
        dynamicSql dynamicSql = new createSqlSession().createdynamicSql();
        student s = new student();
        s.setId(4);
        s.setName("张三");
        int i = dynamicSql.updateBySet(s);
        if(i > 0){
            System.out.println("修改成功!");
        }
    }
}

运行结果:

在我们修改数据之前,我们先来记录一下修改前的数据库文件:

接下来,我们先将id等于4位置的name从“大海”修改成为“小海”并且password不做修改:

我们创建了一个student对象,并且只传递了两个参数,一个id用来定位操作的数据,一个name的值用来修改之前的值:

在执行完程序之后,数据库中的值就被顺利的修改了:

 

那么接下来我们来修改name和password两个的值:

 

只要配置正确,依然可以正确的修改 

注意点:

需要注意的就是我们传入的时候有一个id的值是必须有的,因为通过id才能定位到我们需要修改的行,然后就是SQL语句的拼接和条件的判断。

【四轴飞行器】非线性三自由度四轴飞行器模拟器研究(Matlab代码实现)内容概要:本文围绕非线性三自由度四轴飞行器模拟器的研究展开,重点介绍了基于Matlab的建模与仿真方法。通过对四轴飞行器的动力学特性进行分析,构建了非线性状态空间模型,并实现了姿态与位置的动态模拟。研究涵盖了飞行器运动方程的建立、控制系统设计及数值仿真验证等环节,突出非线性系统的精确建模与仿真优势,有助于深入理解飞行器在复杂工况下的行为特征。此外,文中还提到了多种配套技术如PID控制、状态估计与路径规划等,展示了Matlab在航空航天仿真中的综合应用能力。; 适合人群:具备一定自动控制理论基础Matlab编程能力的高校学生、科研人员及从事无人机系统开发的工程技术人员,尤其适合研究生及以上层次的研究者。; 使用场景及目标:①用于四轴飞行器控制系统的设计与验证,支持算法快速原型开发;②作为教学工具帮助理解非线性动力学系统建模与仿真过程;③支撑科研项目中对飞行器姿态控制、轨迹跟踪等问题的深入研究; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注动力学建模与控制模块的实现细节,同时可延伸学习文档中提及的PID控制、状态估计等相关技术内容,以全面提升系统仿真与分析能力。
DeepSeek系列模型是为应对不同应用场景而设计的,它们在功能、优化方向及适用领域上有所区别。以下是基于现有信息对各个版本之间差异的概述: ### DeepSeek R1 - **设计目的**:专为跨领域问题解决设计,能够在最少微调的情况下适应多种任务需求。 - **技术背景**:依赖于V3的MoE架构进行知识蒸馏,从而使得轻量化模型能够继承强大的推理能力[^1]。 ### DeepSeek V3 - **特点**:作为一款通用大型语言模型,它特别优化了指令跟随能力多步骤逻辑推理能力。 - **架构**:采用Transformer基础架构,并结合了如MLA、MoE等创新模块来提升性能[^2]。 ### DeepSeek Coder V2 - **目标应用**:专注于代码生成软件工程任务。 - **优势**:相较于其他版本,此模型更擅长理解生成编程代码,支持多种编程语言。 ### DeepSeek VL - **特色功能**:虽然具体细节未提及,但根据命名推测该模型可能针对视觉-语言任务进行了优化,比如图像描述生成或视觉问答等。 ### DeepSeek V2 - **定位**:作为早期版本之一,它奠定了后续模型发展的基础。 - **改进点**:通过引入新的架构技术实现了性能上的飞跃,例如通过MLA(多头注意力机制的一种变体)MoE框架[^2]。 ### DeepSeek Coder - **核心竞争力**:与Coder V2类似,但可能是较早发布的版本,专注于提高代码相关任务的表现。 ### DeepSeek Math - **专长领域**:专门处理数学符号处理定量分析任务。 - **应用场景**:适用于需要复杂计算数学建模的应用场景。 ### DeepSeek LLM - **通用性**:这是一个较为宽泛的概念,指代整个DeepSeek系列中的基础架构,所有特定用途的模型都是基于这个架构发展起来的。 ```python # 示例代码:展示如何使用不同模型处理各自擅长的任务 def use_model(model_name, task): if model_name == &#39;DeepSeek R1&#39;: print("使用R1模型解决跨领域问题") elif model_name == &#39;DeepSeek V3&#39;: print("利用V3进行高效指令执行复杂推理") elif model_name == &#39;DeepSeek Coder V2&#39;: print("运用Coder V2编写高质量代码") elif model_name == &#39;DeepSeek VL&#39;: print("利用VL模型处理视觉-语言交互任务") elif model_name == &#39;DeepSeek V2&#39;: print("采用V2版本的基础架构") elif model_name == &#39;DeepSeek Coder&#39;: print("使用Coder版本生成代码") elif model_name == &#39;DeepSeek Math&#39;: print("调用Math模型处理数学问题") else: print("未知模型") use_model(&#39;DeepSeek Math&#39;, &#39;solve math problem&#39;) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值