mybatis學習(七)——resultType解析
resultType是sql映射文件中定義返回值類型,返回值有基本類型,對象類型,List類型,Map類型等。現總結一下再解釋html
總結:
resultType:java
一、基本類型 :resultType=基本類型sql
二、List類型: resultType=List中元素的類型apache
三、Map類型 單條記錄:resultType =mapsession
多條記錄:resultType =Map中value的類型mybatis
一、對象類型
對於對象類型resultType直接寫對象的全類名就能夠了app
實例:學習
hotelMapper接口測試
package com.pjf.mybatis.dao; import com.pjf.mybatis.po.Hotel; public interface HotelMapper { //返回值類型爲Hotel public Hotel getHotel(Integer i); }
HotelMapper.xmlui
<?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.pjf.mybatis.dao.HotelMapper"> <!--resultType直接寫對象的全類名 --> <select id="getHotel" resultType="com.pjf.mybatis.po.Hotel"> select * from hotel where id=#{id} </select> </mapper>
測試類:
package com.pjf.mybatis; import java.io.IOException; import java.io.InputStream;import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.pjf.mybatis.dao.HotelMapper; import com.pjf.mybatis.po.Hotel; public class TestHotel { public SqlSessionFactory sqlSessionFactory() throws IOException { // mybatis的配置文件 String resource = "mybatis_config.xml"; // 使用類加載器加載mybatis的配置文件(它也加載關聯的映射文件)TestHotel.class.getClassLoader() InputStream is = Resources.getResourceAsStream(resource); // 構建sqlSession的工廠 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); return sessionFactory; } // 查 @Test public void getHotel() throws IOException { SqlSessionFactory sessionFactory = sqlSessionFactory(); SqlSession session = sessionFactory.openSession(); HotelMapper hotelMapper = session.getMapper(HotelMapper.class); System.out.println(hotelMapper.getClass()); //直接返回Hotel對象,打印出來 Hotel hotel = hotelMapper.getHotel(1001); System.out.println(hotel); session.close(); } }
二、List類型
返回值爲List類型,resultType爲List中對象的類型,如List<Hotel>,resultType爲Hotel
實例:
hotelMapper接口
package com.pjf.mybatis.dao; import java.util.List; import com.pjf.mybatis.po.Hotel; public interface HotelMapper { // 返回值爲List public List<Hotel> getHotel(Integer i); }
hotelMapper.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.pjf.mybatis.dao.HotelMapper"> <!-- 返回值爲List,resultType爲List中元素的全類名 --> <select id="getHotel" resultType="com.pjf.mybatis.po.Hotel"> select * from hotel where price>#{price} </select> </mapper>
測試類
package com.pjf.mybatis; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.pjf.mybatis.dao.HotelMapper; import com.pjf.mybatis.po.Hotel; public class TestHotel { public SqlSessionFactory sqlSessionFactory() throws IOException { // mybatis的配置文件 String resource = "mybatis_config.xml"; // 使用類加載器加載mybatis的配置文件(它也加載關聯的映射文件)TestHotel.class.getClassLoader() InputStream is = Resources.getResourceAsStream(resource); // 構建sqlSession的工廠 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); return sessionFactory; } // 查 @Test public void getHotel() throws IOException { SqlSessionFactory sessionFactory = sqlSessionFactory(); SqlSession session = sessionFactory.openSession(); HotelMapper hotelMapper = session.getMapper(HotelMapper.class); System.out.println(hotelMapper.getClass()); // 返回值爲List List<Hotel> list = hotelMapper.getHotel(1000); for (Hotel hotel : list) { System.out.println(hotel); } session.close(); } }
三、Map類型
a、返回單條記錄的map,key爲屬性,值爲屬性值。resultType爲map
hotelMapper接口
package com.pjf.mybatis.dao; import java.util.Map; import com.pjf.mybatis.po.Hotel; public interface HotelMapper { // 返回值爲Map,key爲屬性名,value爲屬性值 public Map<String, Object> getHotel(Integer i); }
hotelMapper.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.pjf.mybatis.dao.HotelMapper"> <!-- 返回值爲map,resultType爲map --> <select id="getHotel" resultType="map"> select * from hotel where id=#{id} </select> </mapper>
測試類,返回id=1001的酒店
package com.pjf.mybatis; import java.io.IOException; import java.io.InputStream; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.pjf.mybatis.dao.HotelMapper; import com.pjf.mybatis.po.Hotel; public class TestHotel { public SqlSessionFactory sqlSessionFactory() throws IOException { // mybatis的配置文件 String resource = "mybatis_config.xml"; // 使用類加載器加載mybatis的配置文件(它也加載關聯的映射文件)TestHotel.class.getClassLoader() InputStream is = Resources.getResourceAsStream(resource); // 構建sqlSession的工廠 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); return sessionFactory; } // 查 @Test public void getHotel() throws IOException { SqlSessionFactory sessionFactory = sqlSessionFactory(); SqlSession session = sessionFactory.openSession(); HotelMapper hotelMapper = session.getMapper(HotelMapper.class); System.out.println(hotelMapper.getClass()); // 返回值爲map Map<String, Object> map = hotelMapper.getHotel(1001); System.out.println(map); session.close(); } }
b、返回多條記錄的map,key爲任意一屬性,值爲對象類型。如Map<String,Hotel>,resultType爲Hotel
返回多條記錄的map時,key爲任意一屬性,值爲對象類型,不過key須要經過@MapKey("hotelName")指定對象中一個屬性名爲key
實例:
hotelMapper接口
package com.pjf.mybatis.dao; import java.util.Map; import org.apache.ibatis.annotations.MapKey; import com.pjf.mybatis.po.Hotel; public interface HotelMapper { // 返回值爲Map,key須要經過@MapKey("屬性名")來指定javaBean中的一個屬性名爲key,value爲對象 @MapKey("hotelName") public Map<String, Hotel> getHotel(Integer i); }
hotelMapper.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.pjf.mybatis.dao.HotelMapper"> <!-- 返回值爲map,resultType爲對象的全類名 --> <select id="getHotel" resultType="com.pjf.mybatis.po.Hotel"> select * from hotel where price>#{price} </select> </mapper>
測試類:返回價格>1000以上的酒店
package com.pjf.mybatis; import java.io.IOException; import java.io.InputStream; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.pjf.mybatis.dao.HotelMapper; import com.pjf.mybatis.po.Hotel; public class TestHotel { public SqlSessionFactory sqlSessionFactory() throws IOException { // mybatis的配置文件 String resource = "mybatis_config.xml"; // 使用類加載器加載mybatis的配置文件(它也加載關聯的映射文件)TestHotel.class.getClassLoader() InputStream is = Resources.getResourceAsStream(resource); // 構建sqlSession的工廠 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); return sessionFactory; } // 查 @Test public void getHotel() throws IOException { SqlSessionFactory sessionFactory = sqlSessionFactory(); SqlSession session = sessionFactory.openSession(); HotelMapper hotelMapper = session.getMapper(HotelMapper.class); System.out.println(hotelMapper.getClass()); // 返回值爲map Map<String, Hotel> map = hotelMapper.getHotel(1000); System.out.println(map); session.close(); } }