Mybatis 返回值配置理解 - 返回值是浮点数 BigDecimal 或整数 Integer的配置 - 返回指定实体类格式的 List 数组

目录

  • 前提
  • 1. 返回整形数值
  • 2. 返回值 BigDecimal 浮点数 金额类型
  • 3. 返回指定实体类格式的 List 数组
  • 参考链接

前提

Mybatis报错: A query was run and no Result Maps were found for the Mapped Statement

mybatis中的所有查询标签,都必须返回resultType或者resultMap的值,否则就会报如上错误的,其实仔细看看因为报错原因的意思就好了

1. 返回整形数值

TestMapper.xml 演示:

<select id="getReturnNum" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM test_table
select>

Java 调用演示:

@Autowired
private TestMapper testMapper;

Integer mycount = testMapper.getReturnNum();

2. 返回值 BigDecimal 浮点数 金额类型

TestMapper.xml 演示:

<select id="getReturnAmount" resultType="java.math.BigDecimal">
        SELECT IFNULL(SUM(mymoney), 0.00)
        FROM test_table
select>

Java 调用演示:

@Autowired
private TestMapper testMapper;

Integer mycount = testMapper.getReturnAmount();

3. 返回指定实体类格式的 List 数组

TestMapper.xml 演示:

<select id="getReturnList" resultType="com.test.entity.MyEntity">
        SELECT *
        FROM test_table
select>

Java 调用演示:

import com.test.entity.MyEntity;

@Autowired
private TestMapper testMapper;

List<MyEntity> myList = testMapper.getReturnList();

参考链接

Mybatis报错: A query was run and no Result Maps were found for the Mapped Statement

你可能感兴趣的:(Java,mybatis)