MyBatis学习笔记(四)一多一关系

Student中包含Address

package com.skymr.mybatis.model;

public class Student {

	private int id;
	
	private String name;
	
	private int age;
	
	private Address address;
	
	/**
	 * 必须要有无参构造器,有参构造器可有可无(至少我测试时是这样)
	 * 如果没有无参构造器,只有有参构造器,会报错
	 */
	public Student() {
	}
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public String toString(){
		return "["+name+","+age+","+address+"]";
	}
	
}

package com.skymr.mybatis.model;

public class Address {

	//省
	private String province;
	//市
	private String city;
	//区
	private String region;
	
	private int id;

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public String toString(){
		return "[" + id+","+this.province+","+this.city+","+this.region+"]";
	}
}

package com.skymr.mybatis.mappers;

import java.util.List;

import com.skymr.mybatis.model.Student;

public interface StudentMapper {

	public Student getStudent(int id);
	
	public List<Student> getAllStudents();
	//取得所有学生,带地址
	public List<Student> getAllStudentsWithAddr();
	//取得学生,带地址
	public Student getStudentWithAddr(int id);
}

方式一:对象集联

	<!-- 一对一关系 -->
	<select id="getAllStudentsWithAddr" resultMap="stuMapWidhAddr">
		select * from mybatis_Student t1, mybatis_address t2 where t1.address_id=t2.id
	</select>
	<resultMap type="Student" id="stuMapWidhAddr">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		<result property="address.id" column="address_id"/>
		<result property="address.province" column="province"/>
		<result property="address.city" column="city"/>
		<result property="address.region" column="region"/>
	</resultMap>
	
	<select id="getStudentWithAddr" resultMap="stuMapWidhAddr" parameterType="int">
		select * from mybatis_Student t1 left join mybatis_address t2 on t1.address_id=t2.id and t1.id=#{id}
	</select>

这种方式不太好,重用性差.

方式二:

	<!-- 一对一关系 -->
	<select id="getAllStudentsWithAddr" resultMap="stuMapWidhAddr">
		select * from mybatis_Student t1, mybatis_address t2 where t1.address_id=t2.id
	</select>
	<resultMap type="Student" id="stuMapWidhAddr">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		<association property="address" resultMap="addressMap"></association>
	</resultMap>
	<!-- 将Address独立出来 -->
	<resultMap type="Address" id="addressMap">
		<id property="id" column="id"/>
		<result property="province" column="province"/>
		<result property="city" column="city"/>
		<result property="region" column="region"/>
	</resultMap>
	<select id="getStudentWithAddr" resultMap="stuMapWidhAddr" parameterType="int">
		select * from mybatis_Student t1 left join mybatis_address t2 on t1.address_id=t2.id and t1.id=#{id}
	</select>

将Address独立出来,然后使用association关联到Address的resultMap

方式三:

	<resultMap type="Student" id="stuMapWidhAddr">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		<association property="address" javaType="Address">
			<result property="id" column="id"/>
			<result property="province" column="province"/>
			<result property="city" column="city"/>
			<result property="region" column="region"/>
		</association>
	</resultMap>

方式四:推荐的方式

这种方式还是主要是应用association标签

首先,为Address类加入AddressMapper类与AddressMapper.xml

package com.skymr.mybatis.mappers;

import com.skymr.mybatis.model.Address;

public interface AddressMapper {

	public Address getAddress(int id);
}

<?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.skymr.mybatis.mappers.AddressMapper">
	<select id="getAddress" resultType="Address" parameterType="int">
		select * from mybatis_address where id=#{id}
	</select>
</mapper> 

其次,修改StudentMapper.xml

	<!-- 一对一关系 -->
	<select id="getAllStudentsWithAddr" resultMap="stuMapWidhAddr">
		select * from mybatis_Student
	</select>
	<resultMap type="Student" id="stuMapWidhAddr">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		<association property="address" column="address_id" select="com.skymr.mybatis.mappers.AddressMapper.getAddress">
		</association>
	</resultMap>
	<select id="getStudentWithAddr" resultMap="stuMapWidhAddr" parameterType="int">
		select * from mybatis_Student where id=#{id}
	</select>

使用了association标签,定义column与select属性

column: 传入mybatis_Student表的外键address_id.

select: 调用AddressMapper的getAddress方法

推演过程:查询一条student数据,再根据address_id查询address表的一条数据.

思考:不知道可不可以这样想,如果查询1000条student,再分别查address表的1000条数据,这样查询次数就是1001次了,不影响性能吗?




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值