Mybatis返回值封装在Map集合中

数据库表:

CREATE TABLE `products` (
  `prod_id` char(10) COLLATE utf8_unicode_ci NOT NULL,
  `vend_id` char(10) COLLATE utf8_unicode_ci NOT NULL,
  `prod_name` char(255) COLLATE utf8_unicode_ci NOT NULL,
  `prod_price` decimal(8,2) NOT NULL,
  `prod_desc` text COLLATE utf8_unicode_ci,
  PRIMARY KEY (`prod_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

映射文件:



<mapper namespace="com.heiketu.testpackage.mapper.ProductsMapper">

<select id="getResultUseMap" resultType="map">
    SELECT * FROM Products WHERE prod_id = #{prod_id}
select>
mapper>
/**
引入了lombok插件
*/
@Setter
@Getter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Products {
    private String prodId;
    private String vendId;
    private String prodName;
    private BigDecimal prodPrice;
    private String prodDesc;
}

映射接口:

public interface ProductsMapper {
    //结果集以Map形式返回
    public Map<String,Object> getResultUseMap(@Param("prod_id") String productId);
}

测试代码:

SqlSession sqlSession = null;

try {
    String configFile = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(configFile);
    SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
    sqlSession = build.openSession();

    //以Map形式返回
    ProductsMapper mapper = sqlSession.getMapper(ProductsMapper.class);
    Map<String, Object> useMap = mapper.getResultUseMap("BR03");

    //返回形式是key=字段名,value=字段的值
    System.out.println(useMap);

} catch (IOException e) {
    e.printStackTrace();
}finally {
    sqlSession.close();
}

结果:

{vend_id=BRS01, prod_desc=18 inch teddy bear, comes with cap and jacket, prod_price=11.99, prod_id=BR03, prod_name=18 inch teddy bear}

总结:
mybatis映射返回值封装成Map需要在映射文件中的resultType属性值设置为map(map为mybatis默认设置的Map集合类别名).
封装的结果结构为: {key=字段名,value=字段的值,key=字段名,value=字段的值...}

你可能感兴趣的:(mybaits)