一、resultMap的使用
resultMap 也是定義返回值類型,返回值為用戶自定義的類型,可用於解決JavaBean中的屬性名和數據庫中的列名不一致的情況
之前對於JavaBean中屬性名和數據庫中的列名不一致的情況,通過有兩種辦法,1、通過在sql中使用別名 2、如果正好符合駝峰命名,需要在settings中配置,現在可以通過resultMap來解決
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"> <!-- resultMap定義 type:javaBean的全類名, id為該resultMap的唯一標識 --> <resultMap type="com.pjf.mybatis.po.Hotel" id="myHotel"> <!--id 指定主鍵的封裝規則 column:數據庫中列名 property:javaBean的屬性名 --> <id column="id" property="id" jdbcType="INTEGER" /> <!--result 指定非主鍵的封裝規則 column:數據庫中列名 property:javaBean的屬性名 --> <result column="hotel_name" property="hotelName" jdbcType="VARCHAR" /> <result column="hotel_address" property="hotelAddress" jdbcType="VARCHAR" /> <result column="price" property="price" jdbcType="INTEGER" /> </resultMap> <!-- resultMap使用 --> <select id="getHotel" resultMap="myHotel"> select * from hotel where id=#{id} </select> </mapper>
二、association的使用
association和collection都是用來關聯另一個表的數據,區別就是用來關聯對象的封裝的,而collection是用來關聯集合封裝的,
舉個例子,比如通過查詢酒店,查出該酒店的城市,是一個城市對應一個酒店,用association
而查詢一個城市的酒店,是一對多的,用collection,下面來具體實現下這個例子。
1、環境准備
修改hotel.java代碼,增加一種類成員變量City,通過查詢酒店,直接查出他所在的城市
package com.pjf.mybatis.po; public class Hotel { private int id; private String hotelName; private String hotelAddress; private int price; private City city; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getHotelName() { return hotelName; } public void setHotelName(String hotelName) { this.hotelName = hotelName; } public String getHotelAddress() { return hotelAddress; } public void setHotelAddress(String hotelAddress) { this.hotelAddress = hotelAddress; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public City getCity() { return city; } public void setCity(City city) { this.city = city; } @Override public String toString() { return "Hotel [id=" + id + ", hotelName=" + hotelName + ", hotelAddress=" + hotelAddress + ", price=" + price + "]"; } }
增加城市類 City.java
package com.pjf.mybatis.po; public class City { private int cityCode; private String cityName; public int getCityCode() { return cityCode; } public void setCityCode(int cityCode) { this.cityCode = cityCode; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } @Override public String toString() { return "City [cityCode=" + cityCode + ", cityName=" + cityName + "]"; } }
還有數據庫的修改,hotel表中增加一列city_code,新增一個city表,
hotel表
city表
hotelMapper接口不變
package com.pjf.mybatis.dao; import com.pjf.mybatis.po.Hotel; public interface HotelMapper { public Hotel getHotel(Integer i); }
2、association的使用
通過association來關聯city表,使用規則如下
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"> <!-- resultMap定義 type:javaBean的全類名, id為該resultMap的唯一標識 --> <resultMap type="com.pjf.mybatis.po.Hotel" id="myHotel"> <!--id 指定主鍵的封裝規則 column:數據庫中列名 property:javaBean的屬性名 --> <id column="id" property="id" jdbcType="INTEGER" /> <!--result 指定非主鍵的封裝規則 column:數據庫中列名 property:javaBean的屬性名 --> <result column="hotel_name" property="hotelName" jdbcType="VARCHAR" /> <result column="hotel_address" property="hotelAddress" jdbcType="VARCHAR" /> <result column="price" property="price" jdbcType="INTEGER" /> <!--association 關聯的表 property 指被關聯的類成員變量 javaType 指被關聯的類成員變量的全類名 --> <association property="city" javaType="com.pjf.mybatis.po.City"> <id column="city_code" property="cityCode" jdbcType="INTEGER"/> <result column="city_name" property="cityName" jdbcType="VARCHAR"/> </association> </resultMap> <!-- resultMap使用 --> <select id="getHotel" resultMap="myHotel"> select h.id,h.hotel_name,h.hotel_address,h.price,c.city_code,c.city_name from hotel h ,city c where h.city_code=c.city_code and h.id=#{id} </select> </mapper>
測試類:
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()); Hotel hotel = hotelMapper.getHotel(1004); // 打印酒店 System.out.println(hotel); // 打印城市 System.out.println(hotel.getCity()); session.close(); } }
這時候就可以看到結果了
3、association的分步查詢的使用
三、collection的使用
實例:查詢某個城市的全部酒店
修改city類
package com.pjf.mybatis.po; import java.util.List; public class City { private int cityCode; private String cityName; private List<Hotel> hotel; public List<Hotel> getHotel() { return hotel; } public void setHotel(List<Hotel> hotel) { this.hotel = hotel; } public int getCityCode() { return cityCode; } public void setCityCode(int cityCode) { this.cityCode = cityCode; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } @Override public String toString() { return "City [cityCode=" + cityCode + ", cityName=" + cityName + "]"; } }
修改hotelMapper接口(可以重新定義一個接口和mapper.xml文件)
package com.pjf.mybatis.dao; import com.pjf.mybatis.po.City; public interface HotelMapper { public City getCityHotel(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"> <resultMap type="com.pjf.mybatis.po.City" id="cityHotel"> <id column="city_code" property="cityCode"/> <result column="city_name" property="cityName"/> <!--collection被關聯的集合 ofType被關聯集合元素的全類名 --> <collection property="hotel" ofType="com.pjf.mybatis.po.Hotel"> <id column="id" property="id"/> <result column="hotel_name" property="hotelName"/> <result column="hotel_address" property="hotelAddress"/> <result column="price" property="price"/> </collection> </resultMap> <select id="getCityHotel" resultMap="cityHotel"> SELECT c.city_code,c.city_name ,h.id,h.hotel_name,h.hotel_address,h.price FROM city c LEFT JOIN hotel h ON c.city_code=h.city_code WHERE c.city_code=#{cityCode} </select> </mapper>
測試類
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.City; 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()); City city = hotelMapper.getCityHotel(1); // 打印城市 System.out.println(city); // 打印酒店 for (Hotel hotel : city.getHotel()) { System.out.println(hotel); } session.close(); } }
查看結果