Mybatis 返回结果集与关系映射一对一和一对多

	public List<Student> find();

resultMap 专门定义返回的类型,结果集

type为集合的每个元素student

id为下面使用的时候引用的

返回结果为集合的情况

id为主键

property 为实体类属性

column 为数据库对应的字段名

<resultMap type="Student" id="StudentResult">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
	</resultMap> -->
	
	<select id="find" resultMap="StudentResult">
		select * from t_student
	</select>

一对一关系映射

一个学生一个地址

public class Student {

	private Integer id;
	private String name;
	private Integer age;
	private Address address;
}

public class Address {

	private Integer id;
	private String sheng;
	private String shi;
	private String qu;
}

	public Student findStudentWithAddress(Integer id);

第一种方法对象级联

column 为外键的名字 addressId对应student数据库的字段名称

<resultMap type="Student" id="StudentResult">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		
		<result property="address.id" column="addressId"/>
		<result property="address.sheng" column="sheng"/>
		<result property="address.shi" column="shi"/>
		<result property="address.qu" column="qu"/>
	</resultMap>

	<select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
		select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
	</select>

第二种方式

把address独立出来

association property 为student类里面的address属性关联上面的 resultMap

<resultMap type="Address" id="AddressResult">
		<result property="id" column="id"/>
		<result property="sheng" column="sheng"/>
		<result property="shi" column="shi"/>
		<result property="qu" column="qu"/>
	</resultMap>
	
	<resultMap type="Student" id="StudentResult">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		<association property="address" resultMap="AddressResult"/>
	</resultMap>

第三种方式嵌套方式

javaType 类型为Address

<resultMap type="Student" id="StudentResult">
		<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="sheng" column="sheng"/>
			<result property="shi" column="shi"/>
			<result property="qu" column="qu"/>
		</association>
	</resultMap> 

第四种方式

通过adressId来关联查询

column ="addressId"为 student表的外键名称对应的是adress表的主键

	<resultMap type="Student" id="StudentResult">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		<association property="address" column="addressId" select="com.java.mappers.AddressMapper.findById"></association>
	</resultMap>

public interface AddressMapper {

	public Address findById(Integer id);

}

<resultMap type="Address" id="AddressResult">
		<result property="id" column="id"/>
		<result property="sheng" column="sheng"/>
		<result property="shi" column="shi"/>
		<result property="qu" column="qu"/>
	</resultMap>
	
	<select id="findById" parameterType="Integer" resultType="Address">
		select * from t_address where id=#{id}
	</select>

一对多的关系映射

一个年级里有很多的学生

查询学生吧年级查询出来一对一

association  里面的column 传的是外键 gradeId

public class Student {

	private Integer id;
	private String name;
	private Integer age;
	private Grade grade;
}

public Student findByGradeId(Integer gradeId);	


<resultMap type="Student" id="StudentResult">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
		<association property="grade" column="gradeId" select="com.java.mappers.GradeMapper.findById"></association>
	</resultMap>

	<select id="findByGradeId" resultMap="StudentResult" parameterType="Integer">
		select * from t_student where gradeId=#{gradeId}
	</select>

查询一个年级把年级下的所有学生查询出来 一对多

collection里面的column传的是主键 id



<mapper namespace="com.java1234.mappers.GradeMapper">

	<resultMap type="Grade" id="GradeResult">
		<result property="id" column="id"/>
		<result property="gradeName" column="gradeName"/>
		<collection property="students" column="id" select="com.java.mappers.StudentMapper.findByGradeId"></collection>
	</resultMap>
	
	<select id="findById" parameterType="Integer" resultMap="GradeResult">
		select * from t_grade where id=#{id}
	</select>

</mapper> 

猜你喜欢

转载自blog.csdn.net/qq_35029061/article/details/81836087
今日推荐