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

MyBatis 动态 SQL 的详细内容讲解

1. MyBatis 动态 SQL 的详细内容讲解

文章目录

  • 1. MyBatis 动态 SQL 的详细内容讲解
  • 2. 准备工作
  • 3. if 标签
  • 4. where 标签
  • 5. trim 标签
  • 6. set 标签
  • 7. choose when otherwise 标签
  • 8. foreach 标签
    • 8.1 批量删除
    • 8.2 批量添加
  • 9. SQL 标签与 include 标签
  • 10. 总结:
  • 11. 最后:


有的业务场景,也需要SQL语句进行动态拼接,例如:

在这里插入图片描述

delete from t_car where id in(1,2,3,4,5,6,......这里的值是动态的,根据用户选择的id不同,值是不同的);

多条件查询:

在这里插入图片描述

select * from t_car where brand like '丰田%' and guide_price > 30 and .....;

2. 准备工作

数据表结构的设计,数据表名为:t_car

在这里插入图片描述

t_car 表中的数据信息:

在这里插入图片描述

pom.xml 文件当中配置相关的依赖的 jar 包如下:

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.rainbowsea</groupId><artifactId>mybatis-005-crud-blog</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target></properties><dependencies><!--        mybatis 的依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.10</version></dependency><!--        mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.30</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><!--        引入 logback的依赖,这个日志框架实现了slf4j 规范--><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.11</version></dependency></dependencies></project>

配置 logback 的配置文件,用于打印显示,我们的日志信息,方便我们查看我们的运行过程,效果。

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?><configuration debug="false"><!-- 控制台输出 --><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"><!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern></encoder></appender><!--mybatis log configure--><logger name="com.apache.ibatis" level="TRACE"/><logger name="java.sql.Connection" level="DEBUG"/><logger name="java.sql.Statement" level="DEBUG"/><logger name="java.sql.PreparedStatement" level="DEBUG"/><!-- 日志输出级别,logback日志级别包括五个:TRACE < DEBUG < INFO < WARN < ERROR --><root level="DEBUG"><appender-ref ref="STDOUT"/><appender-ref ref="FILE"/></root></configuration>

配置 MyBatis 的核心配置文件,

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--  使用 <package>	还可以将这个包下的所有的类的全部自动起别名,别名就是简名,不区分大小写 --><package name="com.rainbowsea.mybatis.pojo"/></typeAliases><environments default="mybatis"><environment id="mybatis"><!--            MANAGED 没有用第三框架管理的话,都是会被提交的,没有事务上的管理了 --><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis"/><property name="username" value="root"/><property name="password" value="MySQL123"/></dataSource></environment></environments><mappers><!-- 这里也是可以使用 package 包名扫描,但是同样的:对应接口路径要一致,接口名一致--><package name="com.rainbowsea.mybatis.mapper"></package></mappers>
</configuration>

对照 t_car 创建的ORM 映射的 Car 类

注意:在MyBatis 当中对应的ORM ,一般在框架里对应的 Bean实体类,一定要实现该 set 和 get 方法以及无参数构造方法,无法框架无法使用反射机制,进行操作

建议用包装类,这样可以防止 Null的问题,因为(简单类型 int num = null ,是不可以赋值为 null)的编译无法通过

在这里插入图片描述

package com.rainbowsea.mybatis.pojo;public class Car {// 数据库表当中的字段应该和pojo类的属性一一对应// 建议使用包装类,这样可以防止null的问题private Long id;private String carNum;private String brand;private Double guidePrice;private String produceTime;private String carType;public Car() {}public Car(Long id, String carNum, String brand, Double guidePrice, String produceTime, String carType) {this.id = id;this.carNum = carNum;this.brand = brand;this.guidePrice = guidePrice;this.produceTime = produceTime;this.carType = carType;}@Overridepublic String toString() {return "Car{" +"id=" + id +", carNum='" + carNum + '\'' +", brand='" + brand + '\'' +", guidePrice=" + guidePrice +", produceTime='" + produceTime + '\'' +", catType='" + carType + '\'' +'}';}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getCarNum() {return carNum;}public void setCarNum(String carNum) {this.carNum = carNum;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public Double getGuidePrice() {return guidePrice;}public void setGuidePrice(Double guidePrice) {this.guidePrice = guidePrice;}public String getProduceTime() {return produceTime;}public void setProduceTime(String produceTime) {this.produceTime = produceTime;}public String getcarType() {return carType;}public void setcarType(String catType) {this.carType = catType;}
}

3. if 标签

需求:多条件查询。

可能的条件包括:品牌(brand)、指导价格(guide_price)、汽车类型(car_type)

多条件查询
不提供查询:0 条件 select * from t_product;
当选择一个条件: select * from t_product where brand=#{brand}
当用户选了多个条件:select * from t_product where 条件1=...and 条件2=..and条件3 =...
 <if test="表达式运行之后的结果是false,不会拼接"></if>

if 标签的使用的注意事项:

  1. if 标签中的 test 属性是必须的
  2. if 标签中 test 属性的值是false 或者 true
  3. 如果test是true ,则if标签中的sql语句就会拼接,反之则部分拼接、
  4. test属性中可以使用的是:
    1. 当使用了@Param注解,那么 test 中给你要出现的是 @Param 注解指定的参数名,@Param(“brand”)
    2. 当没有使用@Param注解,那么test中要出现的是:param1,param2,param3 ; arg0,arg1…
    3. 当使用了POJO,那么test中出现的是POJO类的属性名,
    4. 5.在mybatis的动态SQL当中,不能使用&&,只能使用 and ,or
  5. 注意单引号双引号交替使用

在这里插入图片描述

package com.rainbowsea.mybatis.mapper;import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface CarMapper {/*** 多条件查询** @param brand       品牌* @param guiderPrice 指导价* @param carType     汽车类型* @return*/List<Car> selectByMultiCondition(@Param("brand") String brand, @Param("guidePrice") Double guiderPrice,@Param("carType") String carType);
}

在这里插入图片描述

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace 一定要是:对应的接口的全限定类名-->
<mapper namespace="com.rainbowsea.mybatis.mapper.CarMapper"><!--	id 要是 namespace 对应接口上的方法名: --><select id="selectByMultiCondition" resultType="Car">SELECT id, car_num, brand, guide_price, produce_time, car_typeFROM `t_car`where 1=1<!--1.if标签中的test属性是必须的2.if标签中test属性的值是false 或者 true3.如果test是true ,则if标签中的sql语句就会拼接,反之则部分拼接4.test属性中可以使用的是:当使用了@Param注解,那么test中i给你要出现的是@Param注解指定的参数名,@Param("brand")当没有使用@Param注解,那么test中要出现的是:param1,param2,param3 ; arg0,arg1...当使用了POJO,那么test中出现的是POJO类的属性名5.在mybatis的动态SQL当中,不能使用&&,只能使用 and ,or6.注意单引号双引号交替使用--><!--        <if test="表达式运行之后的结果是false,不会拼接"></if>--><if test="brand != null and brand != ''"><!--数据库表中的字段名-->and brand like "%"#{brand}"%"</if><if test="guidePrice != null and guidePrice !=''">and guide_price >= #{guidePrice}</if><if test="carType != null and carType != ''">and car_type = #{carType}</if></select></mapper>

运行测试:

在这里插入图片描述

在这里插入图片描述

package com.rainbowsea.mybatis.test;import com.rainbowsea.mybatis.mapper.CarMapper;
import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;import java.io.IOException;
import java.util.List;public class CarMapperTest {@Testpublic void testSelectByMultiCondition() throws IOException {SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"), "mybatis");SqlSession sqlSession = sqlSessionFactory.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);// 假设三个条件都不为null//List<Car> cars = mapper.selectByMultiCondition("小米",21.00,"新能源");// 假设三个条件都是空//List<Car> cars = mapper.selectByMultiCondition("",null,"");// 假设后两个条件不为空,第一个条件为空//List<Car> cars = mapper.selectByMultiCondition("",21.00,"新能源");// 假设第一个条件不是空,后两个条件为空List<Car> cars = mapper.selectByMultiCondition("小米",null,"");cars.forEach(car -> {System.out.println(car);});sqlSession.close();}
}

补充:

如果第一个条件为空,剩下两个条件不为空,会是怎样呢?

List<Car> cars = mapper.selectByMultiCondition("", 20.0, "燃油车");

在这里插入图片描述

报错了,SQL语法有问题,where后面出现了and。这该怎么解决呢?

可以where后面添加一个恒成立的条件。

在这里插入图片描述

在这里插入图片描述

4. where 标签

where标签的作用:让 where 子句更加动态智能。

  • 所有条件都为空时,where 标签不会生成where子句。同时满足条件 会自动生成 where 关键字,不需要我们手动添加。
  • 自动去除某些条件前面多余的 and 或 or。

继续使用 if 标签中的需求。

需求:多条件查询。

可能的条件包括:品牌(brand)、指导价格(guide_price)、汽车类型(car_type)

在这里插入图片描述

import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface CarMapper {/*** 使用where标签,让where 子句更加灵活,更加智能** @param brand* @param guidePrice* @param carType* @return*/List<Car> selectByMultiConditionWithWhere(@Param("brand") String brand, @Param("guidePrice") Double guidePrice,@Param("carType") String carType);}

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace 一定要是:对应的接口的全限定类名-->
<mapper namespace="com.rainbowsea.mybatis.mapper.CarMapper"><select id="selectByMultiConditionWithWhere" resultType="Car">SELECT id, car_num, brand, guide_price, produce_time, car_typeFROM `t_car`<!--where 标签是专门负责 where 子句动态生成的,不用我们自己再写 where 关键字了,自动生成--><!--自动去除某些条件前面多余的and或or。--><where><if test="brand != null and brand != ''"><!--数据库表中的字段名-->and brand like "%"#{brand}"%"</if><if test="guidePrice != null and guidePrice !=''">and guide_price >= #{guidePrice}</if><if test="carType != null and carType != ''">and car_type = #{carType}</if></where></select></mapper>

运行测试:

在这里插入图片描述


import com.rainbowsea.mybatis.mapper.CarMapper;
import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;import java.io.IOException;
import java.util.List;public class CarMapperTest {@Testpublic void testSelectByMultiConditionWithWhere() throws IOException {SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"), "mybatis");SqlSession sqlSession = sqlSessionFactory.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);// 假设三个条件都不为nullList<Car> cars = mapper.selectByMultiConditionWithWhere("小米",21.00,"新能源");// 如果第一个条件是空,其他的不为空List<Car> cars2 = mapper.selectByMultiConditionWithWhere("",21.00,"新能源");// 假设第一个条件不是空,后两个条件为空List<Car> cars3 = mapper.selectByMultiConditionWithWhere("小米",null,"");cars3.forEach(car -> {System.out.println(car);});sqlSession.close();}
}

5. trim 标签

trim标签的属性:

  • prefix:在trim标签所有内容的最 前面添加内容
  • suffix:在trim标签中所有内容的最 后面添加 内容
  • prefixOverrides:trim 标签中所有内容当中前缀覆盖掉(去掉)
  • suffixOverrides:trim 标签中所有内容当中后缀覆盖掉(去掉)

在这里插入图片描述

import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface CarMapper {/*** 使用 trim 标签,添加删除** @param brand* @param guidePrice* @param carType* @return*/List<Car> selectByMultiConditionWithTrim(@Param("brand") String brand, @Param("guidePrice") Double guidePrice,@Param("carType") String carType);
}

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace 一定要是:对应的接口的全限定类名-->
<mapper namespace="com.rainbowsea.mybatis.mapper.CarMapper"><select id="selectByMultiConditionWithTrim" resultType="Car" >SELECT id, car_num, brand, guide_price, produce_time, car_typeFROM `t_car`<!--prefix 加前缀suffix: 加后缀prefixOverriders: 删除前缀suffixOverrides : 删除后缀prefix="where" 表示是在 trim 标签所有内容的最前面添加 where ,suffix 同理suffixOverrides="and|or" 把 trim 标签中所有内容的后缀 and 或 or 去掉--><trim prefix="where" suffixOverrides="and|or"><if test="brand != null and brand != ''">brand like "%"#{brand}"%" and</if><if test="guidePrice != null and guidePrice !=''">guide_price > #{guidePrice} and</if><if test="carType != null and carType !=''">car_type = #{carType}</if></trim></select></mapper>

运行测试:

在这里插入图片描述


import com.rainbowsea.mybatis.mapper.CarMapper;
import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;import java.io.IOException;
import java.util.List;public class CarMapperTest {@Testpublic void testSelectByMultiConditionWithTrim() throws IOException {SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"), "mybatis");SqlSession sqlSession = sqlSessionFactory.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);List<Car> cars = mapper.selectByMultiConditionWithTrim("", null, "");List<Car> cars2 = mapper.selectByMultiConditionWithTrim("小米", null, "");cars.forEach(car -> {System.out.println(car);});sqlSession.close();}
}

6. set 标签

主要使用在 update 语句当中,用来生成 set 关键字,同时去掉最后多余的“,”。set 标签会自动添加 set 关键字,不用我们自己再写了-

比如我们只更新提交的不为空的字段,如果提交的数据是空或者 “”,那么这个字段我们将不更新

在这里插入图片描述


import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface CarMapper {/*** 使用set 标签** @param car* @return*/int updateSet(Car car);}

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace 一定要是:对应的接口的全限定类名-->
<mapper namespace="com.rainbowsea.mybatis.mapper.CarMapper"><update id="updateSet">update t_car<!--set 标签会自动添加 set 关键字,不用我们自己再写了--><set><if test="carNum != null and carNum != ''">Car_num = #{carNum},</if><if test="brand != null and brand != ''">brand = #{brand},</if><if test="guidePrice != null and guidePrice != ''">guide_price = #{guidePrice},</if><if test="produceTime != null and produceTime != ''">produce_time = #{produceTime},</if><if test="carType != null and carType != ''">car_type = #{carType},</if></set>whereid = #{id}</update>
</mapper>

运行测试:

将 id 为 128的 brand 改为:丰田霸道 ,car_type 改为 燃油车,其他的为 null (这里使用了 set ,为null / 空字段值,不会被修改)

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


import com.rainbowsea.mybatis.mapper.CarMapper;
import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;import java.io.IOException;
import java.util.List;public class CarMapperTest {/** 主要使用在update语句当中,用来生成set关键字,同时去掉最后多余的“,”比如我们只更新提交的不为空的字段,如果提交的数据是空或者"",那么这个字段我们将不更新。*/@Testpublic void testUpdateSet() throws IOException {SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"), "mybatis");SqlSession sqlSession = sqlSessionFactory.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Car car = new Car(128L, null, "丰田霸道", null, null, "燃油车");mapper.updateSet(car);sqlSession.commit();sqlSession.close();}
}

7. choose when otherwise 标签

这三个标签是在一起使用的:

<choose><when></when><when></when><when></when><otherwise></otherwise>  // 上面的 when 都没满足时,执行这个
</choose>

等同于

if(){}else if(){}else if(){}else if(){}else{}

只有一个分支会被选择(其中一个满足了,后面的就不会进去了),被执行 SQL拼接 !!!!

需求:先根据品牌查询,如果没有提供品牌,再根据指导价格查询,如果没有提供指导价格,就根据生产日期查询。

在这里插入图片描述

import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface CarMapper {/*** 使用choose when otherwise 标签** @param brand* @param guiderPrice* @param carType* @return*/List<Car> selectByChoose(@Param("brand") String brand, @Param("guidePrice") Double guiderPrice,@Param("carType") String carType);}

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace 一定要是:对应的接口的全限定类名-->
<mapper namespace="com.rainbowsea.mybatis.mapper.CarMapper"><select id="selectByChoose" resultType="Car">SELECT id, car_num, brand, guide_price, produce_time, car_typeFROM `t_car`<!--只会执行其中的 一个满足条件的分支当中。--><where><choose><!-- 当其中的某个 when 满足条件,进入该 when 的SQL语句(拼接),并执行,后面其他的when 就不会进入了--><when test="brand != null and brand != ''">brand like "%"#{brand}"%"</when><when test="guidePrice != null and guidePrice !=''">guide_price > #{guidePrice}</when><otherwise><!-- 当上面所有的 when 都没有满足条件的,则会进入这里的 otherwise的SQL语句(拼接),并执行-->car_type = #{carType}</otherwise></choose></where></select></mapper>

运行测试:

在这里插入图片描述

在这里插入图片描述


import com.rainbowsea.mybatis.mapper.CarMapper;
import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;import java.io.IOException;
import java.util.List;public class CarMapperTest {/*** 只有一个分支会被选择!!!!*/@Testpublic void testSelectByChoose() throws IOException {SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"), "mybatis");SqlSession sqlSession = sqlSessionFactory.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);// 三个条件不为空List<Car> cars = mapper.selectByChoose("丰田霸道",3.00,"燃油车");// 第一条件为空List<Car> cars2 = mapper.selectByChoose("",3.00,"燃油车");// 两个条件为NullList<Car> cars3 = mapper.selectByChoose("",null,"燃油车");// 全部为空,执行选择 otherwise 标签当中的信息List<Car> cars4 = mapper.selectByChoose("",null,"");cars4.forEach(car ->{System.out.println(car);});sqlSession.close();}
}

8. foreach 标签

利用循环数组或集合,动态生成sql,比如这样的SQL:

批量删除

delete from t_car where id in(1,2,3);
delete from t_car where id = 1 or id = 2 or id = 3;

批量添加:

insert into t_car values(null,'1001','凯美瑞',35.0,'2010-10-11','燃油车'),(null,'1002','比亚迪唐',31.0,'2020-11-11','新能源'),(null,'1003','比亚迪宋',32.0,'2020-10-11','新能源')

8.1 批量删除

方式一:

delete from t_car where id in(1,2,3);

在这里插入图片描述

import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface CarMapper {/*** 批量删除,foreach 标签** @param ids* @return*/int deleteByIds(@Param("ids") Long[] ids);
}

foreach 标签的属性:

 collection :指定数组或者集合,在对应接口方法当中参数中定义的参数名item:代表数组或集合中的元素的一个临时变量,命名随意,但是最好见名知意separator:循环之间的分隔符open: foreach 循环拼接的所有sql语句的最前面以什么开始close: foreach 循环拼接的所有sql语句的最前面以什么结束collection="ids" 第一次写这个的时候报错了,错误信息是:{array,arg0}什么意思?map.put("array",数组)map.put("arg0",数组)
 <!--  delete from t_car where id in(1,2,3)--><!-- delete from t_car where id in ( -->
<foreach collection="ids" item="变量" separator=",">
</foreach><foreach collection="ids" item="aaa" separator=",">#{aaa}</foreach>

在这里插入图片描述

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace 一定要是:对应的接口的全限定类名-->
<mapper namespace="com.rainbowsea.mybatis.mapper.CarMapper"><!--foreach 标签的属性:collection :指定数组或者集合,在对应接口方法当中参数中定义的参数名item:代表数组或集合中的元素separator:循环之间的分隔符open: foreach 循环拼接的所有sql语句的最前面以什么开始close: foreach 循环拼接的所有sql语句的最前面以什么结束collection="ids" 第一次写这个的时候报错了,错误西悉尼是:{array,arg0}什么意思?map.put("array",数组)map.put("arg0",数组)--><delete id="deleteByIds"><!--  delete from t_car where id in(1,2,3)--><!-- delete from t_car where id in ( --><!--<foreach collection="ids" item="变量" separator=","></foreach>--><!-- <foreach collection="ids" item="aaa" separator=",">#{aaa}</foreach>--><!-- ) -->delete from t_car where id in<foreach collection="ids" item="id" separator="," open="(" close=")">#{id}</foreach></delete></mapper>

运行测试:

在这里插入图片描述

方式二:

delete from t_car where id = 1 or id = 2 or id = 3;

在这里插入图片描述

import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface CarMapper {/*** 批量删除第二种方式 or** @param ids* @return*/int deleteByIds2(@Param("ids") Long[] ids);}

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace 一定要是:对应的接口的全限定类名-->
<mapper namespace="com.rainbowsea.mybatis.mapper.CarMapper"><delete id="deleteByIds2">delete from t_car where<foreach collection="ids" separator="or" item="id">id=#{id}</foreach></delete>
</mapper>

运行测试:

删除id为 136 ,137 两条记录

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


import com.rainbowsea.mybatis.mapper.CarMapper;
import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class CarMapperTest {@Testpublic void testDeleteByIds2() throws IOException {SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"), "mybatis");SqlSession sqlSession = sqlSessionFactory.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Long[] ids = {136L, 137L};mapper.deleteByIds2(ids);sqlSession.commit();sqlSession.close();}
}

8.2 批量添加

insert into t_car values(null,'1001','凯美瑞',35.0,'2010-10-11','燃油车'),(null,'1002','比亚迪唐',31.0,'2020-11-11','新能源'),(null,'1003','比亚迪宋',32.0,'2020-10-11','新能源')

在这里插入图片描述

import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface CarMapper {/*** 批量插入,一次插入多个Car信息** @param cars* @return*/int insertBath(@Param("cars") List<Car> cars);}

需要注意的是:这里是封装到POJO实体类当中的,所定义的 item的变量后,所有POJO实体类的属性名,用 "." 的方式,比如:car.carNum,变量名.POJO实体类的属性名

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace 一定要是:对应的接口的全限定类名-->
<mapper namespace="com.rainbowsea.mybatis.mapper.CarMapper"><insert id="insertBath">insert into t_car(id,car_num,brand,guide_price,produce_time,car_type) values<foreach collection="cars" item="car" separator=","><!--需要注意的是:这里是封装到POJO实体类当中的,所定义的 item的变量后,所有POJO实体类的属性名,用 "."的方式-->(null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})</foreach></insert>
</mapper>

运行测试:

在这里插入图片描述

在这里插入图片描述


import com.rainbowsea.mybatis.mapper.CarMapper;
import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class CarMapperTest {@Testpublic void testInsertBath() throws IOException {SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"), "mybatis");SqlSession sqlSession = sqlSessionFactory.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Car car1 = new Car(null, "1201", "玛莎拉蒂1", 30.0, "2020-12", "燃油车");Car car2 = new Car(null, "1202", "玛莎拉蒂2", 30.0, "2020-12", "燃油车");Car car3 = new Car(null, "1203", "玛莎拉蒂3", 30.0, "2020-12", "燃油车");List<Car> cars = new ArrayList<>();cars.add(car1);cars.add(car2);cars.add(car3);mapper.insertBath(cars);sqlSession.commit();sqlSession.close();}}

9. SQL 标签与 include 标签

sql标签用来声明sql片段

include 标签用来将声明的 sql 片段包含到某个 sql 语句当中

作用:代码复用。易维护。

首先就是通过 <SQL> 标签定义/声明一个SQL片段,再通过 <include> 标签根据所声明的 SQL语句片段的 id ,引用相关SQL片段的内容。

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace 一定要是:对应的接口的全限定类名-->
<mapper namespace="com.rainbowsea.mybatis.mapper.CarMapper"><!--    声明一个sql片段--><sql id="carColumnNameSql">id,car_num      as carNum,brand,guide_price  as guidePrice,produce_time as produceTime,car_type     as carType</sql><select id="selectById2" resultType="Car">SELECT<!--将声明的sql片段包含进来--><include refid="carColumnNameSql"></include>FROM `t_car`where id=#{id}</select></mapper>

运行测试:

在这里插入图片描述

import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface CarMapper {/*** 测试 sql标签,代码片段的运用** @param id* @return*/Car selectById2(@Param("id") Long id);}

在这里插入图片描述

在这里插入图片描述

import com.rainbowsea.mybatis.mapper.CarMapper;
import com.rainbowsea.mybatis.pojo.Car;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class CarMapperTest {@Testpublic void testSelectById2() throws IOException {SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"), "mybatis");SqlSession sqlSession = sqlSessionFactory.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Car car = mapper.selectById2(118L);System.out.println(car);sqlSession.close();}
}

10. 总结:

  1. if 标签: if 标签中的 test 属性是必须的,if 标签中 test 属性的值是false 或者 true,如果test是true ,则if标签中的sql语句就会拼接,反之则部分拼接、
  2. Where标签:所有条件都为空时,where 标签不会生成where子句。同时满足条件 会自动生成 where 关键字,不需要我们手动添加。自动去除某些条件前面多余的 and 或 or。
  3. trim 标签:
- prefix:在trim标签所有内容的最 前面添加内容
- suffix:在trim标签中所有内容的最  后面添加 内容
- prefixOverrides:trim 标签中所有内容当中前缀覆盖掉(去掉)
- suffixOverrides:trim 标签中所有内容当中后缀覆盖掉(去掉
  1. set 标签:主要使用在 update 语句当中,用来生成 set 关键字,同时去掉最后多余的“,”。set 标签会自动添加 set 关键字,不用我们自己再写了-

    比如我们只更新提交的不为空的字段,如果提交的数据是空或者 “”,那么这个字段我们将不更新

  2. choose when otherwise 标签:这三个标签是在一起使用的:

    <choose><when></when><when></when><when></when><otherwise></otherwise>  // 上面的 when 都没满足时,执行这个
    </choose>
    

    等同于

    if(){}else if(){}else if(){}else if(){}else{}
    

    只有一个分支会被选择(其中一个满足了,后面的就不会进去了),被执行 SQL拼接 !!!!

  3. SQL标签与 include 标签:提高代码的复用性。首先就是通过 <SQL> 标签定义/声明一个SQL片段,再通过 <include> 标签根据所声明的 SQL语句片段的 id ,引用相关SQL片段的内容。

11. 最后:

“在这个最后的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。”

在这里插入图片描述

相关文章:

MyBatis 动态 SQL 的详细内容讲解

1. MyBatis 动态 SQL 的详细内容讲解 文章目录 1. MyBatis 动态 SQL 的详细内容讲解2. 准备工作3. if 标签4. where 标签5. trim 标签6. set 标签7. choose when otherwise 标签8. foreach 标签8.1 批量删除8.2 批量添加 9. SQL 标签与 include 标签10. 总结&#xff1a;11. 最…...

「前端+鸿蒙」鸿蒙应用开发-ArkTS-声明式UI组件化

鸿蒙应用开发中&#xff0c;ArkTS 是一个基于 TypeScript 的开发框架&#xff0c;它允许开发者使用声明式 UI 和组件化的方式来构建用户界面。声明式 UI 意味着你通过描述 UI 的状态和状态的变化来更新界面&#xff0c;而不是通过命令式地操作 DOM。组件化则是将 UI 拆分成独立…...

线程有规律循环打印输出,线程拷贝图片运用

1&#xff1a;线程打印循环&#xff08;保证循环顺序输出&#xff09; 使用互斥锁和条件变量实现&#xff1a; #include <stdio.h>#include <pthread.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <semaphore.h>…...

C++中整型的取值范围

C中整型的取值范围 计算机内存由一些叫做位(bit)的单元组成(参见本章后面的旁注“位与字节”)。C的short、int、long 和 longlong 类型通过使用不同数目的位来存储值,最多能够表示4种不同的整数宽度。如果在所有的系统中&#xff0c;每种类型的宽度都相同&#xff0c;则使用起…...

【研发日记】Matlab/Simulink软件优化(三)——利用NaNFlag为数据处理算法降阶

文章目录 前言 背景介绍 初始算法 优化算法 分析和应用 总结 前言 见《【研发日记】Matlab/Simulink软件优化(一)——动态内存负荷压缩》 见《【研发日记】Matlab/Simulink软件优化(二)——通信负载柔性均衡算法》 背景介绍 在一个嵌入式软件开发项目中&#xff0c;需要开…...

云原生应用开发培训,开启云计算时代的新征程

在云计算时代&#xff0c;云原生应用开发技术已经成为IT领域的热门话题。如果您想要转型至云原生领域&#xff0c;我们的云原生应用开发培训将帮助您开启新征程。 我们的课程内容涵盖了云原生技术的基础概念、容器技术、微服务架构、持续集成与持续发布&#xff08;CI/CD&#…...

C++ | Leetcode C++题解之第150题逆波兰表达式求值

题目&#xff1a; 题解&#xff1a; class Solution { public:int evalRPN(vector<string>& tokens) {int n tokens.size();vector<int> stk((n 1) / 2);int index -1;for (int i 0; i < n; i) {string& token tokens[i];if (token.length() >…...

Spring boot开启跨域配置

Spring boot开启跨域配置 背景 跨域&#xff08;Cross-Origin&#xff09;是指在互联网上的一个域下的文档或脚本尝试请求另一个域下的资源时&#xff0c;域名、协议或端口不同的这种情况。具体来说&#xff0c;如果一个网页试图通过脚本&#xff08;如JavaScript&#xff09…...

RK3568技术笔记六 新建 Ubuntu Linux 虚拟机

VMware 安装完成后&#xff0c;启动 VMware 软件。启动后在 VMware 主界面点击“创建新的虚拟机”。如下图所示&#xff1a; 开始对新建的虚拟机进行设置。选择“自定义”&#xff0c;然后点击“下一步”。如下图所示&#xff1a; 使用默认配置&#xff0c;单击“下一步”。如下…...

Docker面试整理-什么是多阶段构建?它的好处是什么?

多阶段构建是 Docker 在 Dockerfile 中引入的一个功能,允许你在单个 Dockerfile 中使用多个构建阶段,但最终只生成一个轻量级的镜像。这是通过在一个 Dockerfile 中定义多个 FROM 指令来实现的,每个 FROM 指令都可以使用不同的基础镜像,并开始一个新的构建阶段。 多阶段构建…...

TCP/IP协议分析实验:通过一次下载任务抓包分析

TCP/IP协议分析 一、实验简介 本实验主要讲解TCP/IP协议的应用&#xff0c;通过一次下载任务&#xff0c;抓取TCP/IP数据报文&#xff0c;对TCP连接和断开的过程进行分析&#xff0c;查看TCP“三次握手”和“四次挥手”的数据报文&#xff0c;并对其进行简单的分析。 二、实…...

【ArcGIS微课1000例】0119:TIFF与grid格式互相转换

文章目录 一、任务描述二、tiff转grid三、grid转tif四、注意事项一、任务描述 地理栅格数据常用TIFF格式和GRID格式进行存储。TIFF格式的栅格数据常以单文件形式存储,不仅存储有R、G、B三波段的像素值,还保存有地理坐标信息。GRID格式的栅格数据常以多文件的形式进行存储,且…...

laravel8使用中间件实现xss处理

1、创建中间件 php artisan make:middleware XSSClean 2、编辑app/Http/Middleware/XSSClean.php文件 <?phpnamespace App\Http\Middleware;use Closure; use Illuminate\Http\Request;class XSSClean {/*** Handle an incoming request.** param \Illuminate\Http\Requ…...

Spring Boot整合hibernate-validator实现数据校验

文章目录 概念基本概念常用校验注解 前置内容整合Hibernate Validator快速入门优雅处理参数校验异常其余注解校验自定义校验注解 参考来源 概念 基本概念 Hibernate Validator 是一个参数校验框架&#xff0c;可以非常方便地帮助我们校验应用程序的入参&#xff0c;实现了参数…...

Interview preparation--案例加密后数据的模糊查询

加密数据的模糊查询实现方案 我们知道加密后的数据对模糊查询不是很友好&#xff0c;本篇就针对加密数据模糊查询这个问题来展开讲一讲实现的思路&#xff0c;希望对大家有所启发。为了数据安全我们在开发过程中经常会对重要的数据进行加密存储&#xff0c;常见的有&#xff1…...

git clone 项目报“鉴权失败”的解决办法

#问题展示# git clone https://gitee.com/soaringsoft/.....git 正克隆到...... Username for https://gitee.com:...... Password for https://.....gitee.com:...... remote: [session-1440f183] Unauthorized fatal: git clone https://gitee.com/soaringsoft/.....gi…...

韩顺平0基础学java——第22天

p441-459 异常exception 选中代码块&#xff0c;快捷键ctraltt6&#xff0c;即trt-catch 如果进行了异常处理&#xff0c;那么即使出现了异常&#xff0c;但是会继续执行 程序过程中发生的异常事件分为两大类&#xff1a; 异常体系图※ 常见的运行异常&#xff1a;类型转换…...

长沙干洗服务,打造您的专属衣橱

长沙干洗服务&#xff0c;用心呵护您的每一件衣物&#xff01;致力于为您打造专属的衣橱&#xff0c;让您的每一件衣物都焕发出独特的魅力。 我们深知每一件衣物都承载着您的故事和情感&#xff0c;因此我们会以更加细心的态度对待每一件衣物。无论是您心爱的牛仔裤&#xff0c…...

cuda 架构设置

import torch torch.cuda.get_device_capability(0) 添加cmake options&#xff1a; -DCMAKE_CUDA_ARCHITECTURES86 -DCMAKE_CUDA_COMPILER/usr/local/cuda-11.8/bin/nvcc cmake工程出现“CMAKE_CUDA_ARCHITECTURES must be non-empty if set.“的解决方法_failed to detec…...

从源码分析 vllm + Ray 的分布式推理流程

一、前言 随着 LLM 模型越来越大&#xff0c;单 GPU 已经无法加载一个模型。以 Qwen-14B-Chat 模型为例&#xff0c;模型权重大概 28GB&#xff0c;但是单个 NVIDIA A10 仅有 24GB 显存。如果想要在 A10 上部署 Qwen-14B-Chat 模型&#xff0c;我们需要将模型切分后部署到 2 个…...

快捷键专栏 IDEA、Navicat、电脑、Excle、Word等

标题 电脑篇windowsR 配合以下常用命令连上公司网线WiFi速度变慢问题解决Windows10 设置鼠标右键在此处打开cmd和Powershell窗口、关机打开电脑诊断工具系统设置常用设置查看电脑出场日期 systeminfo删除文件显示已在另一个程序打开&#xff1f;找回回收站删除的文件WindowsR输…...

用h()给渲染的组件传递参数

项目的一个下载悬浮提示框是使用antv的notification组件结合自定义的进度条实现的。 效果&#xff1a; 由于进度条需要完整显示&#xff0c;所以取消了组件自带的自动关闭效果。 查看官方文档&#xff0c;可以通过notification.close(key)来关闭提示框窗口。其中key是notifica…...

.NET C# ‘string‘ 类型思考与解析

目录 .NET C# string 类型思考与解析1 string 是值类型还是引用类型&#xff1f;2 为什么字符串要设计成引用类型&#xff0c;且相同字符串会用一个地址的字符串实例&#xff0c;这样解决了什么问题&#xff0c;有什么好处&#xff1f; .NET C# ‘string’ 类型思考与解析 1 ‘…...

从客户端WebAPI视角下解读前端学习

API 应用程序接口&#xff08;API&#xff0c;Application Programming Interface&#xff09;是基于编程语言构建的结构&#xff0c;使开发人员更容易地创建复杂的功能。它们抽象了复杂的代码&#xff0c;并提供一些简单的接口规则直接使用。 JavaScript VS 客户端 API VS 客…...

Python记忆组合透明度语言模型

&#x1f3af;要点 &#x1f3af;浏览器语言推理识别神经网络 | &#x1f3af;不同语言秽语训练识别数据集 | &#x1f3af;交互式语言处理解释 Transformer 语言模型 | &#x1f3af;可视化Transformer 语言模型 | &#x1f3af;语言模型生成优质歌词 | &#x1f3af;模型不确…...

qt仿制qq登录界面

#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) {// 设置窗口大小this->resize(window_width, window_heigth);// 固定窗口大小this->setFixedSize(window_width, window_heigth);// 设置窗口图标this->se…...

Ubuntu server 24 (Linux) Zabbix 7.0 LTS 配置mail邮件报警

1 告警--媒介 选择右边默认模板修改 2 用户设置--配置--报警媒介 3 告警--动作--触发器动作 #测试 sudo systemctl stop zabbix-agent 本文使用postfix自建邮件服务器&#xff0c;如有需要请看...

安鸾学院靶场——安全基础

文章目录 1、Burp抓包2、指纹识别3、压缩包解密4、Nginx整数溢出漏洞5、PHP代码基础6、linux基础命令7、Mysql数据库基础8、目录扫描9、端口扫描10、docker容器基础11、文件类型 1、Burp抓包 抓取http://47.100.220.113:8007/的返回包&#xff0c;可以拿到包含flag的txt文件。…...

Windows 10 找不到Microsoft Edge 浏览器

下载链接 了解 Microsoft Edge 手动下载浏览器 问题说明 一般来说&#xff0c;windows10系统应该是自带浏览器edge的&#xff0c;但有的电脑就是没有找到edge浏览器&#xff0c;可能系统是精简过的&#xff0c;可能是被卸载了。如下&#xff0c;控制面板确实没找到程序。 ​ …...

实践中ES常用命令总结

一.集群状况查看命令 1.1集群健康度 curl http://localhost:9200/_cat/health?v 1.2 集群节点 curl http://localhost:9200/_cat/nodes?v 1.3 集群索引 curl http://localhost:9200/_cat/indices?v 1.4 查看某个索引段 curl http://localhost:9200/_cat/segments/or…...

【算法——动态规划(从dfs回溯开始推导dp)】

基础理论 递归&#xff1a; 递&#xff1a;大问题分解子问题的过程 &#xff1b; 归&#xff1a;产生答案 dp&#xff1a;只进行归&#xff1b;用已知的最底层的&#xff08;递归的边界&#xff0c;搜索树的底&#xff09;&#xff0c;推出未知 《视频索引》 一句话&…...

如何将ai集成到radsystems项目中,在项目中引入ai

AI可以自动化重复性和低价值的任务&#xff0c;例如数据输入、文档处理、信息检索等&#xff0c;让员工能够专注于更具战略性和创造性的工作。通过引入AI驱动的聊天机器人或虚拟助手&#xff0c;可以提供24/7的客户支持&#xff0c;快速响应用户的问题&#xff0c;提高客户满意…...

微服务之远程调用

常见的远程调用方式 RPC&#xff1a;Remote Produce Call远程过程调用&#xff0c;类似的还有 。自定义数据格式&#xff0c;基于原生TCP通信&#xff0c;速度快&#xff0c;效率高。早期的webservice&#xff0c;现在热门的dubbo &#xff08;12不再维护、17年维护权交给apac…...

2024蓝桥杯初赛决赛pwn题全解

蓝桥杯初赛决赛pwn题解 初赛第一题第二题 决赛getting_startedbabyheap 初赛 第一题 有system函数&#xff0c;并且能在bss上读入字符 而且存在栈溢出&#xff0c;只要过掉check函数即可 check函数中&#xff0c;主要是对system常规获取权限的参数&#xff0c;进行了过滤&…...

Spark参数配置不合理的情况

1.1 内存设置 &#x1f4be; 常见的内存设置有两类&#xff1a;堆内和堆外 &#x1f4a1; 我们作业中大量的设置 driver 和 executor 的堆外内存为 4g&#xff0c;造成资源浪费 &#x1f4c9;。 通常 executor 堆外内存在 executor.cores1 的时候&#xff0c;1g 足够了&…...

13600KF+3060Ti,虚拟机安装macOS 14,2024年6月

距离上次装macOS虚拟机已经有一段时间了&#xff0c;macOS系统现在大版本升级的速度也是越来越快了&#xff0c;由于Office只支持最新三个版本的macOS&#xff0c;所以现在保底也得安装macOS 12了&#xff0c;我这次是用macOS 14做实验&#xff0c;13和12的安装方式和macOS 14一…...

用python+vue实现一个计算页面

要实现一个计算器页面&#xff0c;我们需要分别创建前端和后端部分。前端使用 Vue.js 框架&#xff0c;后端使用 Python 的 Flask 框架。 安装依赖 pip install flask npm install -g vue npm install -g vue/cli vue create calculator-frontend cd calculator-frontend npm…...

大语言模型 (LLM) 红队测试:提前解决模型漏洞

大型语言模型 (LLM) 的兴起具有变革性&#xff0c;以其在自然语言处理和生成方面具有与人类相似的卓越能力&#xff0c;展现出巨大的潜力。然而&#xff0c;LLM 也被发现存在偏见、提供错误信息或幻觉、生成有害内容&#xff0c;甚至进行欺骗行为的情况。一些备受关注的事件包括…...

【HarmonyOS】遇见的问题汇总

一、当前编辑的页面&#xff0c;预览打不开 1、问题说明 当前编辑的页面&#xff0c;预览打不开&#xff0c;日志提示如下&#xff1a; Route information is not configured for the current page. To avoid possible redirection issues, configure route information for…...

【C语言】11.指针基础概念

一、内存和地址 我们知道计算上CPU&#xff08;中央处理器&#xff09;在处理数据的时候&#xff0c;需要的数据是在内存中读取的&#xff0c;处理后的数据也会放回内存中&#xff0c;那我们买电脑的时候&#xff0c;电脑上内存是8GB/16GB/32GB等&#xff0c;那这些内存空间如…...

数据挖掘丨轻松应用RapidMiner机器学习内置数据分析案例模板详解(下篇)

RapidMiner 案例模板 RapidMiner 机器学习平台提供了一个可视化的操作界面&#xff0c;允许用户通过拖放的方式构建数据分析流程。RapidMiner目前内置了 13 种案例模板&#xff0c;这些模板是预定义的数据分析流程&#xff0c;可以帮助用户快速启动和执行常见的数据分析任务。 …...

2024酒店IPTV云桌面系统建设方案

Hello大家好&#xff0c;我是点量小芹&#xff0c;这一年多的时间一直在分享实时云渲染像素流相关的内容&#xff0c;今天和大家聊聊酒店IPTV云桌面电视系统解决方案&#xff0c;或者有的朋友也会称之为IPTV服务器。熟悉小芹的朋友知道&#xff0c;IPTV软件系统是我们一直在推的…...

设置systemctl start kibana启动kibana

1、编辑kibana.service vi /etc/systemd/system/kibana.service [Unit] DescriptionKibana Server Manager [Service] Typesimple Useres ExecStart/home/es/kibana-7.10.2-linux-x86_64/bin/kibana PrivateTmptrue [Install] WantedBymulti-user.target 2、启动kibana # 刷…...

学习分享-FutureTask

前言 今天再改简历的时候回顾了之前实习用到的FutureTask&#xff0c;借此来回顾一下相关知识。 FutureTask 介绍 FutureTask 是 Java 并发包&#xff08;java.util.concurrent&#xff09;中的一个类&#xff0c;用于封装异步任务。它实现了 RunnableFuture 接口&#xff0…...

表 达式树

》》》可以借助 LINQPad工具 using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using System.Transactions;namespace EFDemo {public cla…...

Mysql的null值处理

开源项目SDK&#xff1a;https://github.com/mingyang66/spring-parent 个人文档&#xff1a;https://mingyang66.github.io/raccoon-docs/#/ 在Mysql中&#xff0c;null用于标识缺失的或未知的数据&#xff0c;处理null值需要特别小心&#xff0c;因为在数据库中塔可能会导致不…...

1004.最大连续1的个数

给定一个二进制数组 nums 和一个整数 k&#xff0c;如果可以翻转最多 k 个 0 &#xff0c;则返回 数组中连续 1 的最大个数 。 示例 1&#xff1a; 输入&#xff1a;nums [1,1,1,0,0,0,1,1,1,1,0], K 2 输出&#xff1a;6 解释&#xff1a;[1,1,1,0,0,1,1,1,1,1,1] 粗体数字…...

【轮询负载均衡规则算法设计题】

一、题目描述 给定n台主机&#xff08;编号1~n&#xff09;和某批数据包&#xff0c;数据包格式为&#xff08;抵达主机时刻&#xff0c;负载量&#xff09;。这里数据每个时刻最多只有1条数据到达。负载量表示该主机处理此数据包总耗时。请计算轮询负载均衡规则下&#xff0c…...

IDEA创建简单web(servlet)项目(server为tomcat)

引言 鉴于网上很少有关于IDEA开发servlet项目的教程&#xff08;24版idea&#xff0c;并且servlet技术十分复古&#xff0c;很少有人用到&#xff0c;能够理解&#xff0c;该文章旨在为在校的学生提供一个参考&#xff0c;项目技术简单&#xff09;本人在此总结从头开始到项目…...

OV5647与树莓派bullseye 64bit的兼容性问题

OV5647与树莓派bullseye 64bit的兼容性问题 1. 源由2. 问题3. 分析方法一&#xff1a;使用 pyudev 库方法二&#xff1a;使用 v4l2-ctl 工具方法三&#xff1a;直接读取 /dev 目录方法四&#xff1a;使用 OpenCV方法五&#xff1a;使用 Picamera方法六&#xff1a;使用 libcame…...

福特CEO称开了半年小米SU7舍不得换 同行高管赞叹不已

福特CEO称开了半年小米SU7舍不得换小米SU7作为小米进军汽车行业的首款车型,其出色的完成度赢得了广泛赞誉。不仅普通用户和大V对其赞不绝口,就连福特汽车CEO吉姆法利也对该车表示了高度赞赏。在“Everything Electric Show”的一档专访中,吉姆法利提到自己已经驾驶小米SU7长…...

华为正式发布原生鸿蒙系统 实现全面自研突破

10月22日,华为发布了首个国产移动操作系统——原生鸿蒙操作系统,成为继苹果iOS和安卓系统之后的全球第三大移动操作系统。此前发布的鸿蒙系统因为使用了部分AOSP开放源代码而不得不兼容一些安卓应用软件,但此次发布的原生鸿蒙实现了系统底座的全部自研,提升了系统的流畅度、…...

五人委员会都是谁?为什么由他们一起执掌哈马斯

辛瓦尔遇袭身亡,哈马斯领导人之位悬空,10月17日,哈马斯消息人士称五人委员会将暂时承担领导职责。副手、&ldquo;CEO&rdquo;、兄弟&hellip;&hellip;外界对于辛瓦尔继任者人选的猜测仍未停止。总台记者获悉,据法新社当地时间21日援引巴勒斯坦伊斯兰抵抗运动(…...

清江游:美国正在掀起更大的风浪?

近来,世界出现了一件匪夷所思的事情,在俄乌冲突没看到终点时,在中东局势可能出现大规模战争时,朝鲜半岛局势突变,对抗升级。朝韩双方剑拔弩张,给人的感觉似乎作为世界上曾经的火药桶即将再次点燃,也有人预测第二次朝鲜战争一触即发,甚至有人说必然爆发,今年还真的是多…...

秦岭遇险获救者发声 讲述惊魂一夜

10月21日,陕西西安秦岭冰晶顶发生了一起登山遇险事件。共有三拨人从野路夜爬冰晶顶,计划次日早上看云海,但途中天气突变,导致多人被困。救援人员经过十多个小时的努力,成功救出部分被困人员,但一对年轻情侣不幸遇难。获救的登山者小郑回忆说,当时山顶气温过低,天气恶劣…...

今年第20号台风潭美形成 强度逐渐增强逼近吕宋岛

中央气象台消息显示,菲律宾以东的热带低压已于凌晨加强为今年第20号台风“潭美”。早晨5点时,其中心位于菲律宾马尼拉偏东方向约720公里处,即北纬13.6度、东经127.6度。中心附近最大风力达到8级(18米/秒),中心最低气压为998百帕,七级风圈半径在120至260公里之间。预计,…...