Mybatis之resultType详解

       resultType是sql映射文件中定义返回值类型,返回值有基本类型,对象类型,List类型,Map类型等。

resultType:

基本类型  :resultType=基本类型

List类型:   resultType=List中元素的类型

Map类型    单条记录:resultType =map

                   多条记录:resultType =Map中value的类型

对象类型:对于对象类型resultType直接写对象的全类名就可以了


1、基本数据类型,例如int

   studentMapper接口

package com.bj58.mybatis.dao;

import com.bj58.mybatis.po.Student;

public interface StudentMapper {

//返回值类型为int

public int countStudent(long userid);

}

   studentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.bj58.mybatis.dao.StudentMapper"> <!--resultType直接写对象的全类名 --> <select id="countStudent" resultType="int"> select COUNT(*) from user where attTime = #{attTime} and userId = #{userId}; </select> </mapper>


2、List类型,返回值为List类型,resultType为List中对象的类型,如List<Student>,resultType为Student

studentMapper接口

package com.bj58.mybatis.dao;

import java.util.List;

import com.bj58.mybatis.po.Student;

public interface StudentMapper{

// 返回值为List

public List<Student> getStudent(Integer i);

}

studentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.bj58.mybatis.dao.StudentMapper">

<!-- 返回值为List,resultType为List中元素的全类名 -->

<select id="getStudent" resultType="com.bj58.mybatis.po.Student">

select * from user

where

age>#{age}

</select>

</mapper>


3、Map类型,

a、返回单条记录的map,key为属性,值为属性值。resultType为map

   studentMapper接口

package com.bj8.mybatis.dao;

import java.util.Map;

import com.bj58.mybatis.po.Student;

public interface StudentMapper {

// 返回值为Map,key为属性名,value为属性值

public Map<String, Object> getStudent(Integer i);

}

   studentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.bj58.mybatis.dao.StudentMapper">

<!-- 返回值为map,resultType为map -->

<select id="getStudentl" resultType="map">

select * from user

where

age=#{age}

</select>

</mapper>

b、返回多条记录的map,key为任意一属性,值为对象类型。如Map<String,Student>,resultType为Student。返回多条记录的map时,key为任意一属性,值为对象类型,不过key需要通过@MapKey("studentName")指定对象中一个属性名为key

studentMapper接口

package com.bj8.mybatis.dao;

import java.util.Map;

import org.apache.ibatis.annotations.MapKey;

import com.bj58.mybatis.po.Student;

public interface StudentMapper {

// 返回值为Map,key需要通过@MapKey("属性名")来指定javaBean中的一个属性名为key,value为对象

@MapKey("studentName")

public Map<String, Student> getStudent(Integer i);

}

   studentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.bj58.mybatis.dao.StudentMapper">

<!-- 返回值为map,resultType为map -->

<select id="getStudentl" resultType="com.bj58.mybatis.po.Student">

select * from user

where

age=#{age}

</select>

</mapper>


4、对象类型,对于对象类型resultType直接写对象的全类名就可以了

       studentMapper接口

package com.bj58.mybatis.dao;

import com.bj58.mybatis.po.Student;

public interface StudentMapper {

//返回值类型为Student

public Student getStudent (Integer i);

}

    studentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.bj58.mybatis.dao.StudentMapper">

<!--resultType直接写对象的全类名 -->

<select id="getStudent" resultType="com.bj58.mybatis.po.Student">

select * from user

where

age=#{age}

</select>

</mapper>

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值