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>