Mybatis实体类对象入参查询

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Mybatis实体类对象入参查询

走路的猫头鹰   2022-06-28 我要评论

Mybatis实体类对象入参查询

测试实体类对象结构如下

/**
使用lobmok插件
*/
@Getter
@Setter
@NoArgsConstructor
@ToString
@EqualsAndHashCode
public class Vendor {
    private String vend_id;
    private String vend_name;
    private String vend_address;
    private String vend_city;
    private String vend_state;
    private String vend_zip;
    private String vend_country;
}

XML映射文件

<?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.mybatis.mapper.VendorMapper">  
  <select id="findByVendorEntity" parameterType="vendor" resultType="vendor">
      select * from Vendors where vend_id = #{vend_id} and vend_name = #{vend_name}
  </select>
</mapper>

接口文件

public interface VendorMapper {
    //通过Vendor对象查询
    Vendors findByVendorEntity(Vendor vendor);
}

测试文件内容

try {
            String resource = "mybatis-config.xml";
            InputStream resourceAsStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream,"development2");
            //获取SQLSession
            SqlSession openSession = build.openSession();
            
            VendorMapper mapper = openSession.getMapper(VendorMapper.class);
            Vendor vendor = new Vendor();
            vendor.setId("BRE02");
            vendor.setName("Bear Emporium");
            
            Vendor findByVendorEntity = mapper.findByVendorEntity(vendor);
            System.out.println(findByVendorEntity);
        } catch (IOException e) {
            System.out.println("加载配置文件失败");
            e.printStackTrace();
        }

笔记:

当对象作为参数传入查询时(不一定指定parameterType属性值为实体对象的别名或全路径名,typeHandler貌似会自动识别),SQL查询语句的#{}中内容需要与实体类的字段属性一一对应(并非实体类的属性一定是数据库表中的字段,只要填入的值对应即可。

mybatis查询条件是看sql语句的where后的查询条件),如果表达式中的值没有对应,则会报错。

错误示例如下:

....Cause:org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'ids' in 'class com.mybatis.beans.Vendors' ....Cause:org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'ids' in 'class com.mybatis.beans.Vendors'...

Mybatis中的参数深入

一、mybatis的参数

parameterType参数

该参数表示的是输入类型

1、传递简单类型

像int、string这种属于简单类型

2、传递pojo对象

pojo对象就是我们所说的JavaBean,mybatis使用ognl表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称

什么是ognl表达式?

3、传递pojo包装对象

开发中通过实体类的包装对象(pojo包装对象)传递查询条件,查询条件是总和的查询条件,不仅宝库偶用户的查询条件,还包括其它的查询条件,这时候使用包装对象传递输入参数

举例说明,我们的QueryVo类中可以有多个对象,这里我们只存放一个user类

因为通过ognl表达式可以使用属性名打点调用的方式来将user中的username当作参数,同时也可以将别的类的属性当作参数传递进来

二、mybatis的输出结果的封装

resultType(输出类型)

mysql在windows系统下不区分列名的大小写,linux中严格区分大小写

当我们的实体类封装的时候,如果属性名和数据库中的列名,我们在使用mybatis查询的时候是查询不到的,那么我们如何解决这样的问题

我们只需要使封装的属性和表中的列名对应上就可以,

第一种解决方式:

查询数据库的时候起别名的方式

第二种解决方式,使用mybatis配置:

解释说明标签中的属性

  • 如果哪一行sql语句想使用resutMap中的对应关系就需要设置resultMap属性和resuletMap标签中的id属性相同
  • resultMap中的type属性表示的是封装的实体类
  • id标签和property标签中的property表示的是封装的实体类中的属性
  • id标签和property标签中的column属性表示的是映射的关系,也就是数据库表中的列名

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们