mybatis中只查询部分字段的处理方式

时间:2025-03-16 08:40:08

mybatis中如果返回对象集合的话,会把对象中的所有字段都返回,如果表中字段很多而我只需要部分字段,有几种解决方案

1、重新定义类,里面存放要返回的字段属性

2、将结果定义为List<Map<String, Object>>类型,如下:

文件中定义如下:

<!-- Book全部字段 -->
<resultMap  type="">
    <id column="book_id" property="bookId" jdbcType="BIGINT" />
    <result column="book_name" property="bookName" jdbcType="VARCHAR" />
    <result column="press" property="press" jdbcType="VARCHAR" />
    <result column="author" property="author" jdbcType="VARCHAR" />
    <result column="translator" property="translator" jdbcType="VARCHAR" />
    <result column="isbn" property="isbn" jdbcType="CHAR" />
</resultMap>

<!-- 定义resultMap,type为HashMap -->
<resultMap  type="">
    <id column="book_id" property="bookId" jdbcType="BIGINT" />
    <result column="book_name" property="bookName" jdbcType="VARCHAR" />
    <result column="author" property="author" jdbcType="VARCHAR" />
</resultMap>

<!-- 查询语句 -->
<select  resultMap="PartBookMap">
    select book_id, book_name, author from book
</select>

文件中定义如下:

List<Map<String, Object>> selectPartBook();
  •  

用 List< Map< String, Object > > 来接收

List<Map<String, Object>> map = ();

本文来自 siwuai 的**** 博客 ,全文地址请点击:/u014710520/article/details/73569257?utm_source=copy