MyBatis——MyBatis查询语句

一、返回Car

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

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

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 mapper
        PUBLIC "-//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>
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 {
    @Test
    public void testSelectById(){
        CarMapper mapper = SqlSessionUtil.openSession().getMapper(CarMapper.class);
        Car car = mapper.selectById(35L);
        System.out.println(car);
    }
}

二、返回 List<Car> 

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

/**
* 查询所有的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>
@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 集合即可。

/**
 * 通过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>
@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>  

/**
 * 查询所有的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>
@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 集合时更方便  

/**
 * 获取所有的 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>
@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 进行结果映射

/**
 * 查询所有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>
@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>

七、返回总记录条数 

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

一  叶  知  秋,奥  妙  玄  心

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qx_java_1024

祝老板生意兴隆,财源广进!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值