Mybatis returns result set and relation mapping one to one and one to many

	public List<Student> find();

resultMap specifically defines the type of return, result set

type is each element of the collection student

id refers to

Returns a collection

id is the primary key.

Property is an entity class property

column is the corresponding field name of the database

<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>

One to one relationship mapping

One student one address

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);

First method object concatenation

column is the name of the foreign key. addressId corresponds to the field name of the student database

<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>

The second way

Independent address

association property is the address property in the student class to associate the resultMap above

<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>

The third way to nest

javaType type is 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> 

The fourth way

Associate queries with adressId

column ="addressId" is the foreign key name of the student table, which corresponds to the primary key of the address table

	<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>

One to many relationship mapping

There are many students in a grade

Check out the students. Grade check out one-on-one

The column in the association transfers the foreign key 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>

Look up a grade, find out all the students under the grade one to many

The column in the collection passes the primary key 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> 

 

 

 

 

 

 

 

 

Keywords: Java Database

Added by beanfair on Mon, 06 Jan 2020 20:59:43 +0200