正文
mybatis学习(八)——resultMap之association&&collection解析
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
一、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();
}
}
查看结果
mybatis学习(八)——resultMap之association&&collection解析的更多相关文章- mybatis学习 十三 resultMap标签 一对一
1 .<resultMap>标签 写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系. 在写<select>标签中,有一个resultType属性,此时s ...
- Mybatis学习笔记-ResultMap结果集映射
解决属性名与字段名不一致的问题 新建项目 --> 测试实体类字段不一致的情况 数据库字段:id,name,pwd 实体类属性:id,name,password 输出结果 User{id=1, n ...
- mybatis学习八 事物
1.事物的定义: 是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行. 事务处理可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的资源. 2,事物的特性: ...
- Mybatis学习笔记导航
Mybatis小白快速入门 简介 本人是一个Java学习者,最近才开始在博客园上分享自己的学习经验,同时帮助那些想要学习的uu们,相关学习视频在小破站的狂神说,狂神真的是我学习到现在觉得最GAN的老师 ...
- mybatis collection解析以及和association的区别
1.collection标签 说到mybatis的collection标签,我们肯定不陌生,可以通过它解决一对多的映射问题,举个例子一个用户对应多个系统权限,通过对用户表和权限表的关联查询我们可以得到 ...
- 解决 Mybatis 元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id*,result*,association*,collection*,discriminat
在配置 mybatis mapper.xml文件时, 一不小心就会报如下类似的异常: Caused by: org.springframework.beans.factory.BeanCreation ...
- mybatis项目启动报错 The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)".
启动项目报错 2018-02-26 17:09:51,535 ERROR [org.springframework.web.context.ContextLoader] - Context initi ...
- MyBatis学习 之 二、SQL语句映射文件(1)resultMap
目录(?)[-] 二SQL语句映射文件1resultMap resultMap idresult constructor association联合 使用select实现联合 使用resultMap实 ...
- 深入浅出Mybatis系列八-mapper映射文件配置之select、resultMap
注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之inse ...
随机推荐- hd - MFM/IDE 硬盘设备
描述 DESCRIPTION hd* 开头的设备是以裸模式(raw mode)访问MFM/IDE类型硬盘的块设备. 第一个IDE驱动控制器上的主盘(主设备号3)是 hda ;从盘是 hdb. 第二个I ...
- 2018.4.6 java交易记录系统
题目 ###1.交易明细文件内容如下例: 客户号 姓名 所述机构号 性别 帐号 发生时间 发生额 000001|刘德华|0000|1|4155990188888888|20060720200005|3 ...
- GIT在团队中的最佳实践
我们已经从SVN 切换到Git很多年了,现在几乎所有的项目都在使用Github管理, 本篇文章讲一下为什么使用Git, 以及如何在团队中正确使用. Git的优点 Git的优点很多,但是这里只列出我认为 ...
- 字符串 -----JavaScript
本文摘要:http://www.liaoxuefeng.com/ JavaScript的字符串就是用''或""括起来的字符表示. 如果'本身也是一个字符,那就可以用"&q ...
- linux文件或文件夹常见操作
创建文件夹 mkdir [-p] DirName 在工作目录下,建立一个名为 A 新的子目录 : mkdir A 在工作目录下的 B目录中,建立一个名为 T 的子目录: 若 B 目录不存在, ...
- ubuntu 16.04 +anaconda3.6 +Nvidia DRIVER 390.77 +CUDA9.0 +cudnn7.0.4+tensorflow1.5.0+neural-style
这是我第一个人工智能实验.虽然原理不是很懂,但是觉得深度学习真的很有趣.教程如下. Table of Contents 配置 时间轴 前期准备工作 anaconda3 安装 bug 1:conda:未 ...
- ANSI C 与 K&R C
C语言由Dennis M.Ritchie在1973年设计和实现.从那以后使用者逐渐增加.到1978年Ritchie和Bell实验室的另一位程序专家Kernighan合写了著名的<TheC Pro ...
- Unity基础-脚本的基本使用
脚本的基本使用 定义与挂载monobehaviour 1.新建一个场景 2.新建脚本 using System.Collections; using System.Collections.Generi ...
- w3resource_MySQL练习:Basic_select_statement
w3resource_MySQL练习题:Basic_select_statement 1. Write a query to display the names (first_name, last_n ...
- 杭电 1155 Bungee Jumping(物理题)
Problem Description Once again, James Bond is fleeing from some evil people who want to see him dead ...
1 .<resultMap>标签 写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系. 在写<select>标签中,有一个resultType属性,此时s ...
解决属性名与字段名不一致的问题 新建项目 --> 测试实体类字段不一致的情况 数据库字段:id,name,pwd 实体类属性:id,name,password 输出结果 User{id=1, n ...
1.事物的定义: 是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行. 事务处理可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的资源. 2,事物的特性: ...
Mybatis小白快速入门 简介 本人是一个Java学习者,最近才开始在博客园上分享自己的学习经验,同时帮助那些想要学习的uu们,相关学习视频在小破站的狂神说,狂神真的是我学习到现在觉得最GAN的老师 ...
1.collection标签 说到mybatis的collection标签,我们肯定不陌生,可以通过它解决一对多的映射问题,举个例子一个用户对应多个系统权限,通过对用户表和权限表的关联查询我们可以得到 ...
在配置 mybatis mapper.xml文件时, 一不小心就会报如下类似的异常: Caused by: org.springframework.beans.factory.BeanCreation ...
启动项目报错 2018-02-26 17:09:51,535 ERROR [org.springframework.web.context.ContextLoader] - Context initi ...
目录(?)[-] 二SQL语句映射文件1resultMap resultMap idresult constructor association联合 使用select实现联合 使用resultMap实 ...
注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之inse ...
- hd - MFM/IDE 硬盘设备
描述 DESCRIPTION hd* 开头的设备是以裸模式(raw mode)访问MFM/IDE类型硬盘的块设备. 第一个IDE驱动控制器上的主盘(主设备号3)是 hda ;从盘是 hdb. 第二个I ...
- 2018.4.6 java交易记录系统
题目 ###1.交易明细文件内容如下例: 客户号 姓名 所述机构号 性别 帐号 发生时间 发生额 000001|刘德华|0000|1|4155990188888888|20060720200005|3 ...
- GIT在团队中的最佳实践
我们已经从SVN 切换到Git很多年了,现在几乎所有的项目都在使用Github管理, 本篇文章讲一下为什么使用Git, 以及如何在团队中正确使用. Git的优点 Git的优点很多,但是这里只列出我认为 ...
- 字符串 -----JavaScript
本文摘要:http://www.liaoxuefeng.com/ JavaScript的字符串就是用''或""括起来的字符表示. 如果'本身也是一个字符,那就可以用"&q ...
- linux文件或文件夹常见操作
创建文件夹 mkdir [-p] DirName 在工作目录下,建立一个名为 A 新的子目录 : mkdir A 在工作目录下的 B目录中,建立一个名为 T 的子目录: 若 B 目录不存在, ...
- ubuntu 16.04 +anaconda3.6 +Nvidia DRIVER 390.77 +CUDA9.0 +cudnn7.0.4+tensorflow1.5.0+neural-style
这是我第一个人工智能实验.虽然原理不是很懂,但是觉得深度学习真的很有趣.教程如下. Table of Contents 配置 时间轴 前期准备工作 anaconda3 安装 bug 1:conda:未 ...
- ANSI C 与 K&R C
C语言由Dennis M.Ritchie在1973年设计和实现.从那以后使用者逐渐增加.到1978年Ritchie和Bell实验室的另一位程序专家Kernighan合写了著名的<TheC Pro ...
- Unity基础-脚本的基本使用
脚本的基本使用 定义与挂载monobehaviour 1.新建一个场景 2.新建脚本 using System.Collections; using System.Collections.Generi ...
- w3resource_MySQL练习:Basic_select_statement
w3resource_MySQL练习题:Basic_select_statement 1. Write a query to display the names (first_name, last_n ...
- 杭电 1155 Bungee Jumping(物理题)
Problem Description Once again, James Bond is fleeing from some evil people who want to see him dead ...