MyBatis一对多 多对一小例

Student实体类

package com.java1234.model;

public class Student {

	private Integer id;
	private String name;
	private Integer age;
	private Address address;
	private Grade grade;
		
	public Student() {
		super();
	}

	public Student(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}

	public Student(Integer id, String name, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public Grade getGrade() {
		return grade;
	}
	public void setGrade(Grade grade) {
		this.grade = grade;
	}	
}

Address实体类

package com.java1234.model;
public class Address {

	private Integer id;
	private String sheng;
	private String shi;
	private String qu;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getSheng() {
		return sheng;
	}
	public void setSheng(String sheng) {
		this.sheng = sheng;
	}
	public String getShi() {
		return shi;
	}
	public void setShi(String shi) {
		this.shi = shi;
	}
	public String getQu() {
		return qu;
	}
	public void setQu(String qu) {
		this.qu = qu;
	}	
}

Grade实体类

package com.java1234.model;
import java.util.List;
public class Grade {

	private Integer id;
	private String gradeName;
	private List<Student> students;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getGradeName() {
		return gradeName;
	}
	public void setGradeName(String gradeName) {
		this.gradeName = gradeName;
	}
	public List<Student> getStudents() {
		return students;
	}
	public void setStudents(List<Student> students) {
		this.students = students;
	}		
}

StudentMapper接口

package com.java1234.mappers;

import java.util.List;
import com.java1234.model.Student;

public interface StudentMapper {
	public Student findStudentWithAddress(Integer id);	
	public Student findByGradeId(Integer gradeId);
}

StudentMapper.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.java1234.mappers.StudentMapper">
	
	<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.java1234.mappers.AddressMapper.findById"></association>
		<association property="grade" column="gradeId"                 select="com.java1234.mappers.GradeMapper.findById"></association>
	</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>
	
	<select id="findByGradeId" resultMap="StudentResult" parameterType="Integer">
		select * from t_student where gradeId=#{gradeId}
	</select>
</mapper> 

AddressMapper接口

package com.java1234.mappers;
import com.java1234.model.Address;
public interface AddressMapper {

	public Address findById(Integer id);

}

AddressMapper.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.java1234.mappers.AddressMapper">

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

</mapper> 

GradeMapper接口

package com.java1234.mappers;
import com.java1234.model.Grade;
public interface GradeMapper {

	public Grade findById(Integer id);

}

GradeMapper.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.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.java1234.mappers.StudentMapper.findByGradeId"></collection>
	</resultMap>
	
	<select id="findById" parameterType="Integer" resultMap="GradeResult">
		select * from t_grade where id=#{id}
	</select>
</mapper> 




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值