MyBatis——MyBatis查询语句

embedded/2024/9/24 16:11:55/

一、返回Car

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

(查询结果只有一条也可以用 List 集合接收)

java">package org.qiu.mybatis.mapper;import org.qiu.mybatis.pojo.Car;/*** @author 秋玄* @version 1.0* @package com.qiu.mybatis.mapper* @date 2022-09-30-07:32* @since 1.0*/
public interface CarMapper {/*** 根据id主键查询:结果最多只有一条* @param id* @return*/Car selectById(Long id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.qiu.mybatis.mapper.CarMapper"><select id="selectById" resultType="Car">select id,car_num carNum,brand,guide_price guidePrice,produce_time produceTime,car_type carType from t_car where id = #{id}</select>
</mapper>
java">package org.qiu.mybatis.test;import org.qiu.mybatis.mapper.CarMapper;
import org.qiu.mybatis.pojo.Car;
import org.qiu.mybatis.utils.SqlSessionUtil;
import org.junit.Test;public class CarMapperTest {@Testpublic void testSelectById(){CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);Car car = mapper.selectById(35L);System.out.println(car);}
}

二、返回 List<Car> 

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

java">/**
* 查询所有的Car
* @return
*/
List<Car> selectAll();
<select id="selectAll" resultType="Car">select id,car_num carNum,brand,guide_price guidePrice,produce_time produceTime,car_type carType from t_car
</select>
java">@Test
public void testSelectAll(){CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);List<Car> cars = mapper.selectAll();// cars.forEach(car -> System.out.println(car));cars.forEach(System.out::println);
}

如果返回多条记录采用单个实体类接收会报 TooManyResultsException 异常  

三、返回 Map 

当返回的数据,没有合适的实体类对应的话,可以采用 Map 集合接收。字段名做 key,字段值做 value。

查询如果可以保证只有一条数据,则返回一个 Map 集合即可。

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

resultMap="map",这是因为 mybatis 内置了很多别名【参见 mybatis 开发手册】  

<select id="selectByIdRetMap" resultType="map">select id,car_num carNum,brand,guide_price guidePrice,produce_time produceTime,car_type carType from t_car where id = #{id}
</select>
java">@Test
public void testSelectByIdRetMap(){CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);Map<String,Object> car = mapper.selectByIdRetMap(35L);System.out.println(car);
}

如果返回一个 Map 集合,可以将 Map 集合放到 List 集合中

反过来,如果返回的不是一条记录,是多条记录的话,只采用单个 Map 集合接收,这样同样会出现之前的异常:TooManyResultsException

四、返回 List<Map> 

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

java">/*** 查询所有的Car,返回一个List集合。List集合中存储的是Map集合。* @return*/
List<Map<String,Object>> selectAllRetListMap();
<select id="selectAllRetListMap" resultType="map">select id,car_num carNum,brand,guide_price guidePrice,produce_time produceTime,car_type carType from t_car
</select>
java">@Test
public void testSelectAllRetListMap(){CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);List<Map<String,Object>> cars = mapper.selectAllRetListMap();System.out.println(cars);
}
执行结果:
[{carType=燃油车, carNum=103, guidePrice=50.30, produceTime=2020-10-01, id=33, brand=奔驰E300L}, {carType=电车, carNum=102, guidePrice=30.23, produceTime=2018-09-10, id=34, brand=比亚迪汉}, {carType=燃油车, carNum=103, guidePrice=50.30, produceTime=2020-10-01, id=35, brand=奔驰E300L}, {carType=燃油车, carNum=103, guidePrice=33.23, produceTime=2020-10-11, id=36, brand=奔驰C200},......
]

五、返回 Map<String, Map>

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

java">/*** 获取所有的 Car,返回一个 Map 集合。* Map 集合的 key 是 Car 的 id。* Map 集合的 value 是对应 Car。* @return*/
@MapKey("id")
Map<Long,Map<String,Object>> selectAllRetMap();
<select id="selectAllRetMap" resultType="map">select id,car_num carNum,brand,guide_price guidePrice,produce_time produceTime,car_type carType from t_car
</select>
java">@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=丰田霸道},
......
}

六、resultMap 结果映射 

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

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

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

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

使用 resultMap 进行结果映射

java">/*** 查询所有Car,使用resultMap进行结果映射* @return*/
List<Car> selectAllByResultMap();
<!--resultMap:id:这个结果映射的标识,作为 select 标签的 resultMap 属性的值。type:结果集要映射的类。可以使用别名。
-->
<resultMap id="carResultMap" type="car"><!--对象的唯一标识,官方解释是:为了提高 mybatis 的性能。建议写上。--><id property="id" column="id"/><result property="carNum" column="car_num"/><!--当属性名和数据库列名一致时,可以省略。但建议都写上。--><!--javaType 用来指定属性类型。jdbcType 用来指定列类型。一般可以省略。--><result property="brand" column="brand" javaType="string" jdbcType="VARCHAR"/><result property="guidePrice" column="guide_price"/><result property="produceTime" column="produce_time"/><result property="carType" column="car_type"/>
</resultMap><!--resultMap 属性的值必须和 resultMap 标签中 id 属性值一致。-->
<select id="selectAllByResultMap" resultMap="carResultMap">select * from t_car
</select>
java">@Test
public void testSelectAllByResultMap(){CarMapper carMapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);List<Car> cars = carMapper.selectAllByResultMap();System.out.println(cars);
}

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

使用这种方式的前提是:属性名遵循 Java 的命名规范,数据库表的列名遵循 SQL 的命名规范。

Java 命名规范:首字母小写,后面每个单词首字母大写,遵循驼峰命名方式。

SQL 命名规范:全部小写,单词之间采用下划线分割。

比如以下的对应关系:

实体类中的属性名数据库表的列名
carNumcar_num
carTypecar_type
produceTimeproduce_time

如何启用该功能,在mybatis-config.xml文件中进行配置:

<!--放在 properties 标签后面-->
<settings><setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

七、返回总记录条数 

java">/*** 获取总记录条数* @return*/
Long selectTotal();
<!--long是别名,可参考mybatis开发手册。-->
<select id="selectTotal" resultType="long">select count(*) from t_car
</select>
java">@Test
public void testSelectTotal(){CarMapper carMapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);Long total = carMapper.selectTotal();System.out.println(total);
}

一  叶  知  秋,奥  妙  玄  心


http://www.ppmy.cn/embedded/41564.html

相关文章

联合新能源汽车有限公司出席2024年7月8日杭州快递物流展

参展企业介绍 青岛联合新能源汽车有限公司&#xff08;简称&#xff1a;联合汽车&#xff09;&#xff0c;是一家专注于纯电动汽车领域创新的科技公司&#xff0c;在国内率先提出车电分离&#xff0c;电池标准化并共享的方案&#xff0c;研发了包含标准电池、电池仓、可换电纯电…

VUE.JS详细的使用方法

Vue.js是一个轻量级的前端JavaScript框架&#xff0c;它用于构建用户界面和单页应用。以下是Vue.js的基本使用方法和帮助。 安装Vue.js 使用npm: npm install vue使用CDN: <script src"https://cdn.jsdelivr.net/npm/vue2.6.14/dist/vue.min.js"></script&…

计算机网络实验3:路由器安全防控配置

实验目的和要求 理解标准IP访问控制列表的原理及功能理解CHAP、DHCP配置原理了解家用式无线路由配置方法实验项目内容 标准IP访问控制列表配置 CHAP验证路由器上配置DHCP网络地址转换NAT配置无线路由实现实验环境 1. 硬件&#xff1a;PC机&#xff1b; 2. 软件&#xff1a;W…

ubuntu 22.04 安装 RTX 4090 显卡驱动 GPU Driver(PyTorch准备)

文章目录 1. 参考文章2. 检查GPU是Nvidia3. 卸载已有驱动3.1. 命令删除3.2. 老驱动包 4. 官网下载驱动5. 运行5.1. 远程安装关闭交互界面5.2. 运行5.3. 打开交互界面 6. 检测与后续安装 1. 参考文章 https://blog.csdn.net/JineD/article/details/129432308 2. 检查GPU是Nvid…

webpack优化构建速度示例-resolve.modules:

resolve.alias可以设置路径别名&#xff0c;但是对于没有设置路径别名的模块&#xff0c;而且一些特定的模块位于非标准位置时&#xff0c;可以使用 resolve.modules 来优化模块解析的性能&#xff08;webpack会按照resolve.modules的顺序查找&#xff09;&#xff0c;特别是在…

Python基本统计分析

常见的统计分析方法 import numpy as np import scipy.stats as spss import pandas as pd 鸢尾花数据集 https://github.com/mwaskom/seaborn-data df pd.read_csv("iris.csv",index_col"species") v1 df.loc["versicolor",:].petal_lengt…

交换机组网最常见的8大故障及解决方式

有朋友多次提到网络故障&#xff0c;其中在交换机组网时常见的故障比较多&#xff0c;为了便于大家排除这些故障&#xff0c;在此介绍一些常见的典型故障案例及处理思路。 故障1&#xff1a;交换机刚加电时网络无法通信 【故障现象】 交换机刚刚开启的时候无法连接至其他网络…

上海市计算机学会竞赛平台2024年1月月赛乙组序列最大公约数(二)

题目描述 给定 &#x1d45b;n 个正整数&#x1d44e;1,&#x1d44e;2,...,&#x1d44e;&#x1d45b;a1​,a2​,...,an​&#xff0c;你可以至多修改其中一个数字&#xff0c;使这 &#x1d45b;n 个数字的最大公约数尽可能的大。 请问修改后可能的最大公约数的值。 输入…