1. 程式人生 > >mybatis Mapper 中resultType使用方法及返回值為Map的寫法 mybatis學習(七)——resultType解析

mybatis Mapper 中resultType使用方法及返回值為Map的寫法 mybatis學習(七)——resultType解析

 

mybatis學習(七)——resultType解析

resultType是sql對映檔案中定義返回值型別,返回值有基本型別,物件型別,List型別,Map型別等。現總結一下再解釋

總結:

resultType:

1、基本型別  :resultType=基本型別

2、List型別:   resultType=List中元素的型別

3、Map型別    單條記錄:resultType =map

                          多條記錄:resultType =Map中value的型別

1、物件型別

對於物件型別resultType直接寫物件的全類名就可以了

例項:

hotelMapper介面

複製程式碼
package com.pjf.mybatis.dao;

import com.pjf.mybatis.po.Hotel;

public interface HotelMapper { //返回值型別為Hotel public 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"> <!--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(); } }
複製程式碼

2、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(); } }
複製程式碼

3、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(); } }
複製程式碼