[SSM]MyBatis查询语句与动态SQL

news2024/10/5 12:48:25

目录

十、MyBatis查询语句专题

10.1返回Car

10.2返回List

10.3返回Map

10.4返回List

10.5返回Map,map>

10.6resultMap结果映射

使用resultMap进行结果映射

是否开启驼峰命名自动映射

10.7返回总记录条数

十一、动态SQL

11.1 if标签

11.2 where标签

11.3 trim标签

11.4 set标签

11.5choose when otherwise

11.6 foreach标签

批量删除

批量添加

11.7sql标签与include标签


十、MyBatis查询语句专题

10.1返回Car

  • 当查询的结果有对应的实体类,并且查询结果只有一条时:

  • 查询结果是一条的话可以使用List集合。

10.2返回List<Car>

  • 当查询的记录条数是多条的时候,必须使用集合接收,如果使用单个实体类接收会出现异常。

10.3返回Map

  • 当返回的数据没有合适的实体类对应的话,可以采用Map集合接收。字段名叫做key,字段值叫做value。查询如果可以保证只有一条数据,则返回一个Map集合即可。

 

CarMapper.selectByIdRetMap

/**
 * 通过id查询⼀条记录,返回Map集合
 * @param id
 * @return
 */
Map<String, Object> selectByIdRetMap(Long id);

CarMapper.xml

<select id="selectByIdRetMap" resultType="map">
 select * from t_car where id = #{id}
</select>
  • resultMap="map",这是因为mubatis内置了很多别名。

  • 使用map不需要 select as;

CarMapperTest.testSelectByIdRetMap

@Test
public void testSelectByIdRetMap(){
 CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.cla
ss);
 Map<String,Object> car = mapper.selectByIdRetMap(35L);
 System.out.println(car);
}
  • 如果返回一个Map集合,可以将Map集合放到List集合中。

  • 如果返回的不是一条记录,是多条记录的话,只采用单个Map集合接收,会出现异常:TooManyResultsException。

10.4返回List<Map>

  • 查询结果条数⼤于等于1条数据,则可以返回⼀个存储Map集合的List集合。List<Map>等同于List<Car>。

 

CarMapperTest.testSelectAllRetListMap

@Test
public void testSelectAllRetListMap(){
 CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
 List<Map<String,Object>> cars = mapper.selectAllRetListMap();
 System.out.println(cars);
}

10.5返回Map<String,Map>

  • 用Car的id做key,以后取出对应的Map集合时更方便。

 

CarMapperTest.testSelectAllRetMap

@Test
public void testSelectAllRetMap(){
 CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
 Map<Long,Map<String,Object>> cars = mapper.selectAllRetMap();
 System.out.println(cars);
}

执行结果

{
64={carType=燃油⻋, carNum=133, guidePrice=50.30, produceTime=2020-01-10, id=64, brand=丰⽥霸道},
66={carType=燃油⻋, carNum=133, guidePrice=50.30, produceTime=2020-01-10, id=66, brand=丰⽥霸道},
67={carType=燃油⻋, carNum=133, guidePrice=50.30, produceTime=2020-01-10, id=67, brand=丰⽥霸道},
69={carType=燃油⻋, carNum=133, guidePrice=50.30, produceTime=2020-01-10, id=69, brand=丰⽥霸道},
......
}

10.6resultMap结果映射

  • 查询结果的列名和java对象的属性名对应不上怎么办?

    • 第一种方式:as给列起别名

    • 第二种方式:使用resultMap进行结果映射

    • 第三种方式:是否开启驼峰命名自动映射(配置settings)

使用resultMap进行结果映射

 <!--
        专门定义一个结果映射,在这个结果映射当中指定数据库的字段名和Java类的的属性名的对应关系
        type属性:用来指定POJO类的类名
        id属性:指定resultType的唯一标识,这个id将来要在select标签中使用
    -->
    <resultMap id="carResultMap" type="Car">
        <!--如果有主键,建议这里配置一个id标签,不是必须的,但是配上可以提高mybatis的效率-->
        <!--property后面填写POJO类的属性名,column后面填写数据库表的字段名-->
        <id property="id" column="id"/>
        <result property="carNum" column="car_num" javaType="String" jdbcType="VARCHAR"/>
        <!--如果column和property是一样的,可以省略-->
        <!--<result property="brand" column="brand"/>-->
        <result property="guidePrice" column="guide_price"/>
        <result property="produceTime" column="produce_time"/>
        <result property="carType" column="car_type"/>
    </resultMap>
​
    <select id="selectAllByResultMap" resultMap="carResultMap">
        select *
        from t_car;
    </select>

是否开启驼峰命名自动映射

mybatis-config.xml

<!--放在properties标签后⾯-->
<!--开启驼峰命名自动映射-->
<settings>
 <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

CarMapper.xml

<select id="selectAllByMapUnderscoreToCamelCase" resultType="Car">
 select * from t_car
</select>

10.7返回总记录条数

  • 需求:查询总记录条数

CarMapper接口

/**
 * 获取总记录条数
 * @return
 */
Long selectTotal();

CarMapper.xml

<!--long是别名,可参考mybatis开发⼿册。-->
<select id="selectTotal" resultType="long">
 select count(*) from t_car
</select>

CarMapperTest.testSelectTota

@Test
public void testSelectTotal(){
 CarMapper carMapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
 Long total = carMapper.selectTotal();
 System.out.println(total);
}

 

十一、动态SQL

11.1 if标签

  • 需求:多条件查询

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

CarMapper接口

List<Car> selectByMultiCondition(@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType);

CarMapper.xml

<select id="selectByMultiCondition" resultType="Car">
        select * from t_car where 1 = 1
        <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>
  • if标签中test属性是必须的。

  • if标签中test属性的值是false或者true。

  • 如果test是true,则if标签中的sql语句就会拼接。反之,则不会拼接。

  • test属性中可以使用的是:

    • 当使用了@Param注解,那么test中要出现的是@Param注解指定的参数名。@Param("brand"),那么这里只能使用brand

    • 当没有使用@Param注解,那么test中要出现的是:param1 param2 param3 arg0 arg1 arg2....

    • 当使用了POJO,那么test中出现的是POJO类的属性名。

  • 在mybatis的动态SQL当中,不能使用&&,只能使用and。

CarMapperTest.testSelectByMultiCondition

  @Test
    public void testSelectByMultiCondition() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
​
        // 假设三个条件都不是空
        List<Car> cars = mapper.selectByMultiCondition("比亚迪", 2.0, "新能源");
​
        // 假设三个条件都是空
        //List<Car> cars = mapper.selectByMultiCondition("", null, "");
​
        // 假设后两个条件不为空,第一个条件为空
        //List<Car> cars = mapper.selectByMultiCondition("", 2.0, "新能源");
​
        // 假设第一个条件不是空,后两个条件是空
        //List<Car> cars = mapper.selectByMultiCondition("比亚迪", null, "");
​
        cars.forEach(car -> System.out.println(car));
        sqlSession.close();
    }

11.2 where标签

  • where标签的作用:

    • 所有的条件为空时,where标签保证不会生成where子句。

    • 自动去除某些条件前面多余的and或or。

CarMapper.xml

  <select id="selectByMultiConditionWithWhere" resultType="car">
        select * from t_car
        <!--where标签专门负责where子句动态生成-->
        <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>

11.3 trim标签

  • trim标签的属性:

    • prefix:在trim标签中的语句前添加内容

    • suffix:在trim标签中的语句后添加内容

    • prefixOverrides:前缀覆盖掉(去掉)

    • suffixOverrides:后缀覆盖掉(去掉)

CarMapper.xml

 <select id="selectByMultiConditionWithTrim" resultType="car">
        select * from t_car
            <!--prefix="where" 是在trim标签所有内容的前面添加where-->
            <!--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>

11.4 set标签

  • 主要使用在update语句当中,用来生成set关键字,同时去掉最后多余的","

  • 比如我们只更新提交的部位空的字段,如果提交的数据是空或者"",那么这个字段我们将不更新。

CarMapper.xml

 <update id="updateBySet">
        update t_car
        <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>
        where
        id = #{id}
    </update>

11.5choose when otherwise

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

<choose>
 <when></when>
 <when></when>
 <when></when>
 <otherwise></otherwise>
</choose>
  • 等同于:

if(){
 
}else if(){
 
}else if(){
 
}else if(){
 
}else{
}
  • 只会被一个分支选择!!!!

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

CarMapper.xml

<select id="selectWithChoose" resultType="car">
 select * from t_car
 <where>
 <choose>
 <when test="brand != null and brand != ''">
 brand like #{brand}"%"
 </when>
 <when test="guidePrice != null and guidePrice != ''">
 guide_price >= #{guidePrice}
 </when>
 <otherwise>
 produce_time >= #{produceTime}
 </otherwise>
 </choose>
 </where>
</select>
  • 当brand、guidePrice、produceTime均为空时

11.6 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','新能源')

批量删除

CarMapper接口

int deleteBatchByForeach(@Param("ids") Long[] ids);

CarMapper.xml

<delete id="deleteBatchByForeach">
 delete from t_car where id in
 <foreach collection="ids" item="id" separator="," open="(" close=")">
 #{id}
 </foreach>
</delete>
或
    <insert id="deleteByIds2">
        delete from t_car where
        <foreach collection="ids" item="id" separator="or">
            id=#{id}
        </foreach>
    </insert>
  • 指定集合或者数组,必须是@Param("")中的元素

  • item:代表数组或集合中的元素

  • separator:循环之间的分割符

  • open:foreach标签中所有内容的开始

  • close:foreach标签中所有内容的结束

CarMapperTest.testDeleteBatchByForeach

@Test
public void testDeleteBatchByForeach(){
 CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
 int count = mapper.deleteBatchByForeach(new Long[]{40L, 41L, 42L});
 System.out.println("删除了⼏条记录:" + count);
 SqlSessionUtil.openSession().commit();
}

批量添加

CarMapper接口

int insertBatchByForeach(@Param("cars") List<Car> cars);

CarMapper.xml

<insert id="insertBatchByForeach">
 insert into t_car values
 <foreach collection="cars" item="car" separator=",">
 (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
 </foreach>
</insert>

CarMapperTest.testInsertBatchByForeach

  @Test
    public void testInsertBatchByForeach() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car1 = new Car(null, "1111", "宝马1", 50.00, "2016-08-26", "燃油车");
        Car car2 = new Car(null, "1112", "宝马2", 50.00, "2016-08-26", "燃油车");
        Car car3 = new Car(null, "1113", "宝马3", 50.00, "2016-08-26", "燃油车");
        ArrayList<Car> cars = new ArrayList<>();
        cars.add(car1);
        cars.add(car2);
        cars.add(car3);
        mapper.insertBatch(cars);
        sqlSession.commit();
        sqlSession.close();
    }

11.7sql标签与include标签

  • sql标签用来声明sql片段

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

  • 作用:代码复用,易维护

测试

<sql id="carCols">id,car_num carNum,brand,guide_price guidePrice,produce_t
ime produceTime,car_type carType</sql>
<select id="selectAllRetMap" resultType="map">
 select <include refid="carCols"/> from t_car
</select>
<select id="selectAllRetListMap" resultType="map">
 select <include refid="carCols"/> carType from t_car
</select>
<select id="selectByIdRetMap" resultType="map">
 select <include refid="carCols"/> from t_car where id = #{id}
</select>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/730069.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Oracle表空间和用户

Oracle表空间和用户 前言 1、创建表空间 2、 删除表空间 3、创建用户 4、用户赋权限 5、Oracle三个重要的角色 1、创建表空间 ORACLE 数据库的逻辑单元。 一个表空间可以与多个数据文件&#xff08;物理结构&#xff09;关联 一个数据库下可以建立多个表空间&#xff0c;一个表…

探秘制造业数智化:揭开低代码开发平台的新篇章

前言 随着数智时代的到来&#xff0c;制造业面临着前所未有的转型机遇。在这个信息爆炸的时代&#xff0c;如何有效运用科技和数据资源&#xff0c;成为了制造业企业的当务之急。而低代码开发平台&#xff0c;如JNPF快速开发平台&#xff0c;正是这个转型过程中不可或缺的利器。…

【编程语言 · C语言 · 结构体】

【编程语言 C语言 结构体】https://mp.weixin.qq.com/s/pWI712NxhPJi37eWrE9ofw

【产生初始解利器】基于蒙特卡洛模拟产生满足固定需求和固定供给的随机供给矩阵

如何生成一个总和是定值的随机矩阵 震惊&#xff0c;如果做一个约束比较强的模型&#xff0c;解的矩阵需要满足很多等式约束&#xff0c;而且都是整数&#xff0c;随机产生初始解很困难&#xff0c;该怎么办&#xff1f; 震惊&#xff0c;如果做一个约束比较强的模型&#xff0…

多元回归预测 | Matlab粒子群算法(PSO)优化极限梯度提升树XGBoost回归预测,PSO-XGBoost回归预测模型,多变量输入模型

文章目录 效果一览文章概述部分源码参考资料效果一览 文章概述 粒子群算法(PSO)优化极限梯度提升树XGBoost回归预测,PSO-XGBoost回归预测模型,多变量输入模型,多变量输入模型,matlab代码回归预测,多变量输入模型,多变量输入模型 评价指标包括:MAE、RMSE和R2等,代码质量极高…

多元回归预测 | Matlab麻雀算法(SSA)优化极限梯度提升树XGBoost回归预测,SSA-XGBoost回归预测模型,多变量输入模型

文章目录 效果一览文章概述部分源码参考资料效果一览 文章概述 麻雀算法(SSA)优化极限梯度提升树XGBoost回归预测,SSA-XGBoost回归预测模型,多变量输入模型,多变量输入模型,matlab代码回归预测,多

windows设置右键一键打开文件的配置

在安装软件时一般通过msi或者exe安装&#xff0c;这是一般会有提示绑定到鼠标右键&#xff0c;如果没有勾选则安装后就无法通过右键打开&#xff0c;这是需要把文件拖到快捷方式上面才可以使用该软件打开。 另外如何下载的是zip的压缩包源码格式根本没有设置的选项&#xff0c…

2.3 Web应用 -- 3. HTTP 消息格式

2.3 Web应用 -- 3. HTTP 消息格式 HTTP请求消息HTTP响应消息 HTTP请求消息 HTTP协议有两类消息 请求消息(request)响应消息(response) 请求消息 ASCII&#xff1a;人直接可读 HTTP请求消息的通用格式 上传输入的方法 POST方法 网页经常需要填写表格(form)在请求消息的消息体(…

【CMU15-445 FALL 2022】Project #1 - Extendable Hashing

Reference & Thank & Related CMU15445-project1-可扩展哈希表数据库——可拓展哈希&#xff08;Extendable Hashing&#xff09;【CMU15-445数据库】bustub Project #1&#xff1a;Buffer PoolExtendible Hashing (Dynamic approach to DBMS) Extendable Hashing co…

链表问题——长整数加法运算题解【双向链表】

长整数加法运算 问题描述 假设2个任意长度的整数x、y分别用链表A和B存储&#xff0c;现要求设计一个算法&#xff0c;实现xy。计算结果存储在链表C中。 说明&#xff1a; 链表A、B、C可以是单向链表或双向链表&#xff0c;但由于A和B输出时需要从头至尾遍历&#xff0c;而做…

Git提交项目到服务器上

目录 第一步&#xff1a;git status第二步&#xff1a;git pull第三步&#xff1a;git status第四步&#xff1a;git add第五步&#xff1a;git commit第六步&#xff1a;git push 第一步&#xff1a;git status git status 看一下你这个项目里&#xff0c;修改过什么公版的东西…

性能优化(一)JMeter使用

简介&#xff1a; jmeter 是 apache 公司基于 java 开发的一款开源压力测试工具&#xff0c;体积小&#xff0c;功能全&#xff0c;使用方便&#xff0c;是一个比较轻量级的测试工具&#xff0c;使用起来非常简 单。因为 jmeter 是 java 开发的&#xff0c;所以运行的时候必须…

Django_模板(四)

目录 一、模板 创建模板文件 定义模板 视图调用模板 简写视图调用的模板 二、去除模板中的硬编码 URL 三、为 URL 名称添加命名空间 四、生成模板渲染后的静态文件 源码等资料获取方法 一、模板 如何向请求者返回一个漂亮的页面呢&#xff1f; 肯定需要用到html、css…

多元分类预测 | Matlab基于鲸鱼优化深度置信网络(WOA-DBN)的分类预测,多输入单输出模型,多特征输入模型,WOA-DBN分类预测

文章目录 效果一览文章概述部分源码参考资料效果一览 文章概述 多元分类预测 | Matlab基于鲸鱼优化深度置信网络(WOA-DBN)的分类预测,多输入单输出模型,多特征输入模型,WOA-DBN分类预测 多特征输入单输出的二分类及多分类模型。程序内注释详细,直接替换数据就可以用。程序语…

Unity3d的智力拼图小游戏

Unity3d的智力拼图小游戏 项目地址&#xff1a;https://download.csdn.net/download/Highning0007/88015674

关于async/await

async/await是什么&#xff1f; 简单来说async/await是Promise的语法糖&#xff0c;async是异步的意思&#xff0c;await是等待的意思。async function 声明一个函数里面可能有异步代码需要执行&#xff0c;await则可以认为是等待一个异步方法执行完成。 async/await的用法 …

创建台虚拟机并安装上window10系统(NETBASE 第一课)

虚拟机&#xff08;Virtual Machine&#xff09;是一种基于软件的模拟技术&#xff0c;它可以将一台物理计算机模拟成多个虚拟计算机运行不同的操作系统和应用程序&#xff0c;从而实现资源的虚拟化和隔离。在虚拟机中&#xff0c;每个虚拟计算机都拥有自己的独立的操作系统和应…

MySQL基础篇第1章(数据库概述)

文章目录 1、为什么要使用数据库2、数据库与数据库管理系统2.1 数据库的相关概念2.2 数据库与数据库管理系统的关系2.3 常见的数据库管理系统排名2.4 常见的数据库介绍 3、MySQL介绍3.1 概述3.2 MySQL发展史重大事件3.3 关于MySQL 8.03.4 Oracle VS MySQL 4、RDBMS 与 非RDBMS4…

java的void和Void

java 函数如果什么类型的值也不需要返回&#xff0c;用关键字void。 而Void是一个类&#xff0c;是一个不能实例化的占位符类&#xff0c;它持有对一个类对象的引用&#xff0c;这个类对象代表java关键字void。 如果函数返回值是Void&#xff0c;那么函数必须明确返回null。 J…

将 jar 构建成 docker 镜像实例

&#x1f388; 作者&#xff1a;Linux猿 &#x1f388; 简介&#xff1a;CSDN博客专家&#x1f3c6;&#xff0c;华为云享专家&#x1f3c6;&#xff0c;Linux、C/C、云计算、物联网、面试、刷题、算法尽管咨询我&#xff0c;关注我&#xff0c;有问题私聊&#xff01; &…