Mybatis查询

news/2024/10/18 9:25:56/
  1. 返回实体类,必须指定返回类型, resultType不能省略,并且数据库字段名与实体类不一致会填充NULL,实体类我们一般都是驼峰,数据库字段一般都是下划线,所以在查询的时候可以起别名解决,属性填充本质上调用的是实体类的set方法,例如

    例如car_num就会变成 setCar_num实体类并没有这个方法,所以实体类这个变量就会为NULL

    <select id="selectCarById" resultType="com.powernode.mybatis.pojo.Car">
    select id, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carTypefrom t_car where id = #{id}
    </select>
    
  2. 查询多条数据,例如List

    <!--虽然结果是List集合,但是resultType属性需要指定的是List集合中元素的类型。-->
    <select id="selectCarById" resultType="com.powernode.mybatis.pojo.Car">
    select id, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carTypefrom t_car where id = #{id}
    </select>
    
  3. 用Map接受返回结果

    Map<String, Object> getUser(String account);
    
    <select id="getUser" resultType="map">select *from userwhere account = '${account}' or 1 = 1;
    </select>
    

    数据库为NULL的列不会查出来
    在这里插入图片描述

  4. 用Map来接受对象

    	@MapKey里面填写一个列名作为Map的key,value为User实体类,为空也会被查出来@MapKey("id")Map<String,Object> getUser();
    
        <select id="getUser" resultType="user">select *from user</select>
    

    在这里插入图片描述

  5. ResultMap结果映射
    查询结果的列名和java对象的属性名对应不上怎么办?
    第一种方式:as 给列起别名
    第二种方式:使用resultMap进行结果映射
    第三种方式:是否开启驼峰命名自动映射(配置settings),前提命名要规范,实体类全部使用驼峰命名,数据库字段用下划线命名

    mybatis:configuration:map-underscore-to-camel-case: true #开启驼峰映射
    
    /**
    * 查询所有Car,使用resultMap进行结果映射
    * @return
    */
    List<Car> selectAllByResultMap();
    
    <!--
    resultMap:
    id:这个结果映射的标识,作为select标签的resultMap属性的值。
    type:结果集要映射的类。可以使用别名。
    -->
    <resultMap id="carResultMap" type="car">
    <id property="id" column="id"/>
    <result property="carNum" column="car_num"/>
    <!--当属性名和数据库列名一致时,可以省略。但建议都写上。-->
    <!--javaType用来指定属性类型。jdbcType用来指定列类型。一般可以省略。-->
    <result property="brand" column="brand" javaType="string" jdbcType="VARC
    HAR"/>
    <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>
    

http://www.ppmy.cn/news/1023593.html

相关文章

centos7安装 juter notebook

下载安装脚本并执行 wget https://repo.anaconda.com/archive/Anaconda3-2023.03-0-Linux-x86_64.sh chmod x Anaconda3-2023.03-0-Linux-x86_64.sh bash Anaconda3-2023.03-0-Linux-x86_64.sh配置环境变量 cat > /etc/profile.d/anaconda.sh << EOF export PATH$PA…

AD域机器KMS自动激活

1、打开AD域控&#xff0c;点击DNS管理 2、创建其它记录 3、选择服务位置 SRV 4、输入相关信息 服务&#xff1a;_VLMCS协议&#xff1a;_TCP权重&#xff1a;100端口号&#xff1a;1688KMS服务器地址&#xff1a;10.3.0.211 5、成功&#xff0c;这时域内主机重启后&#xff0…

SpringBoot项目启动失败:共三处错误,都是依赖的问题┭┮﹏┭┮

文章目录 项目启动报错1&#xff1a;Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway项目启动报错2&#xff1a;Failed to determine a suitable driver class项目启动报错3&#xff1a;Failed to start bean documentationPluginsBootstrapp…

25 | 葡萄酒质量数据分析

基于kaggle提供的公开数据集,对全球葡萄酒分布情况和质量情况进行数据探索和分析 from kaggle: https://www.kaggle.com/zynicide/wine-reviews 分析思路: 0、数据准备 1、葡萄酒的种类 2、葡萄酒质量 3、葡萄酒价格 4、葡萄酒描述词库 5、品鉴师信息 6、总结 0、数据准备 …

MySQL中基础查询语句

用户表user数据如下&#xff1a; iddevice_idgenderageuniversityprovince12138male21北京大学Beijing23214male复旦大学Shanghai36543famale20北京大学Deijing42315female 23 浙江大学ZheJiang55432male25山东大学Shandong 1&#xff0c;写出ddl语句创建如上表&#xff0c;…

【Codeforces】 CF1734E Rectangular Congruence

题目链接 CF方向 Luogu方向 题目解法 暂时不考虑 b i b_i bi​ 的限制 考虑构造 a i , j i j a_{i,j}ij ai,j​ij&#xff0c; 那么 a r 1 , c 1 a r 2 , c 2 r 1 c 1 r 2 c 2 , a r 1 , c 2 a r 1 , c 2 r 1 c 2 r 2 c 1 a_{r1,c1}a_{r2,c2}r1c1r2c2,\;a_{r1,c2}…

基于IMU和超声的3D手势识别笔

随着科技的发展&#xff0c;人机交互在商业中有了越来越多的应用。面对日益复杂的交互场景&#xff0c;手势识别逐渐成为虚拟现实等相关应用的主要交互手段。 3D手势识别是一个具有挑战性的问题&#xff0c;常用的手势传感器有三种基本类型&#xff1a;多点触摸屏传感器、基于视…

winform 使用CommonOpenFileDialog选择文件夹或文件

选择文件夹 /// <summary> /// 选择文件夹 /// </summary> public void SelectFolder() {CommonOpenFileDialog dialog new CommonOpenFileDialog("请选择一个文件夹");dialog.IsFolderPicker true; //选择文件还是文件夹&#xff08;true:选择文件夹…