4、MyBatis一对一与一对多

准备数据库表

--user_info表
create table user_info(
      id int(2) primary key,
      user_name varchar(12) unique,
      password varchar(15) not null,
      real_name varchar(8) not null,
      age int(3)
);

--address表
create table address(
      id int(2) primary key,
      user_id int(2) not null,
      real_name varchar(8),
      mobile char(11),
      address varchar(150)
);

insert into user_info values(1,'浅唱灬幸福','8912@321','王晓明',12);
insert into address values(1,1,'王小明','15516472282','山西太原');
insert into address values(2,1,'王鑫','18404905139','山西大同');
insert into address values(3,1,'任建','15333021730','山西晋城');

insert into user_info values(2,'ぅ浮生若梦〤','56701wz','王楠',36);
insert into address values(4,2,'王楠','15010303314','北京海淀');
insert into address values(5,2,'赵婕','18435224278','山西长治');

insert into user_info values(3,'街角の风铃','27w4921','李晓飞',9);
insert into address values(6,6,'刘倩','13159775555','吉林长春');

在这里插入图片描述

Ⅰ、一对多

在这里插入图片描述

<?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.jd.userinfo.dao.IUserInfoDao">
	<resultMap type="com.jd.vo.UserInfo" id="ui">
		<result column="user_name" property="userName"/>
		<result column="ui_real_name" property="realName"/>
		<collection property="list" ofType="com.jd.vo.Address">
			<result column="addr_id" property="id"/>
			<result column="addr_real_name" property="realName"/>
		</collection>
	</resultMap>
	<select id="get" resultMap="ui">
		select 
		ui.id, user_name,password, ui.real_name ui_real_name, age,
		ui.id addr_id, addr.real_name addr_real_name, mobile,address
		from user_info ui 
		LEFT JOIN address addr on addr.user_id=ui.id
		where ui.id = #{id}
	</select>
</mapper>

或者这样写sql语句

		select 
		ui.id, user_name, password, ui.real_name ui_real_name, age,
		ui.id addr_id, addr.real_name addr_real_name, mobile,address
		from user_info ui , address addr
		where addr.user_id=ui.id

在这里插入图片描述
id=1对应三个地址,结果正确
在这里插入图片描述

Ⅱ、一对一

在这里插入图片描述

<?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.jd.userinfo.dao.IUserInfoDao">
	<resultMap type="com.jd.vo.UserInfo" id="ui">
		<result column="user_name" property="userName"/>
		<result column="ui_real_name" property="realName"/>
		<association property="address" javaType="com.jd.vo.Address">
			<result column="addr_id" property="id"/>
			<result column="addr_real_name" property="realName"/>
		</association>
	</resultMap>
	<select id="get" resultMap="ui">
		select 
		ui.id, user_name,password, ui.real_name ui_real_name, age,
		ui.id addr_id, addr.real_name addr_real_name, mobile,address
		from user_info ui 
		LEFT JOIN address addr on addr.user_id=ui.id
		where ui.id = #{id}
	</select>
</mapper>

将地址减少到1
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值