mybatis中resultType返回类型

mybatis的resultType属性支持多种返回类型,包括基础类型、包装类、自定义实体类、Map以及List集合,允许在查询时灵活指定返回数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis中resultType可选类型:

1,java的基础类型及其包装类int,double和java.lang.Integer,java.lang.Double等

2,实体类,自己定义的实体类

3,map类型,如果使用resultMap可以使用自定义map;

4,集合,即返回的是一个List集合,其中该集合的类型可以为1,2,3中提到的类型

Map的demo:Dao层的返回类型为Map<String, Object>key是对应的column值,value是数据中的数据
<mapper namespace="com.xx.xx.dao.PersonMapper">
    <!-- 返回值为Map<String,Object>,resultType为map-->
    <select id="getPerson" resultType="map">
        select name,age,sex from Person where id=#{id}
    </select>
</mapper>

Map的demo2:Dao层的返回类型为personInfoMap里的数据(适合于返回自定义所需数据)
<select id="findByIdCard" resultMap="personInfo">
<mapper namespace="com.xx.xx.dao.PersonMapper">
    <select id="getPersonInfo" resultType="personInfoMap">
        select name,age,sex from Person where id=#{id}
    </select>
</mapper>
<!-- 如下resultMap的id与如上查询的resultType对应 -->
<resultMap id="personInfoMap" type="com.xx.xx.Person">
        <result property="name" column="name"/>
        <result property="age" column="age"/>
</resultMap>

List集合demo:Dao层的返回类型为List<Person>,集合类型就是resultType中的值
<mapper namespace="com.xx.xx.dao.PersonMapper">
    <select id="getPersonList" resultType="Person">
        select * from Person
    </select>
</mapper>

基础类型demo:Dao层的返回类型为int或者Integer
<mapper namespace="com.xx.xx.dao.PersonMapper">
    <!-- 返回值为int,resultType为int ,java.lang.Integer也可以-->
    <select id="countPerson" resultType="int">
        select count(1) from Person
    </select>
</mapper>

实体类demo:Dao层的返回类型为(实体类)Person
<mapper namespace="com.xx.xx.dao.PersonMapper">
    <select id="getPerson" resultType="Person">
        select * from Person where id=#{id}
    </select>
</mapper>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值