ID:0 name:null age:24 school:西安电子科技大学 |
<?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.sjf.mapper.StudentMapper">
<resultMap id="StudentResultMap" type="com.sjf.bean.Student" >
<id property="stuID" column="ID"/>
<result property="stuName" column="name"/>
<result property="age" column="age"/>
<result property="school" column="school"/>
</resultMap>
<select id="getStudentByID" parameterType="int" resultMap="StudentResultMap">
select * from Student where ID = #{ID}
</select>
<select id="getAllStudents" resultMap="StudentResultMap">
SELECT ID,name,age,school FROM Student
</select>
</mapper>
package com.sjf.bean;
public class Address {
private int addressID;
private String country;
private String province;
private String city;
public int getAddressID() {
return addressID;
}
public void setAddressID(int addressID) {
this.addressID = addressID;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
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;
}
@Override
public String toString() {
return "[ ID:" + addressID + " country:" + country + " province:" + province + " city:" + city +" ]";
}
}
package com.sjf.bean;
public class Student {
private int stuID;
private String stuName;
private int age;
private String school;
private Address address;
public Student() {
}
public Student(int ID, String name, int age, String school) {
stuID = ID;
this.stuName = name;
this.age = age;
this.school = school;
}
public int getStuID() {
return stuID;
}
public void setStuID(int stuID) {
this.stuID = stuID;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "ID:" + stuID + " name:" + stuName + " age:" + age + " school:" + school + " address:" + (address!=null ? address.toString() : "null");
}
}
<?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.sjf.mapper.StudentMapper">
<resultMap id="StudentResultMap" type="com.sjf.bean.Student" >
<id property="stuID" column="ID"/>
<result property="stuName" column="name"/>
<result property="age" column="age"/>
<result property="school" column="school"/>
</resultMap>
<resultMap id="StudentWithAddressResultMap" type="com.sjf.bean.Student" extends="StudentResultMap">
<result property="address.addressID" column="addressID"/>
<result property="address.country" column="country"/>
<result property="address.province" column="province"/>
<result property="address.city" column="city"/>
</resultMap>
<select id="getStudentByID" parameterType="int" resultMap="StudentResultMap">
select * from Student where ID = #{ID}
</select>
<select id="getAllStudents" resultMap="StudentWithAddressResultMap">
SELECT S.ID,name,age,school,A.ID as addressID,country,province,city
FROM Student S LEFT OUTER JOIN Address A ON S.addressID = A.ID
</select>
</mapper>
<resultMap id="StudentWithAddressResultMap" type="com.sjf.bean.Student">
<id property="stuID" column="ID"/>
<result property="stuName" column="name"/>
<result property="age" column="age"/>
<result property="school" column="school"/>
<result property="address.addressID" column="addressID"/>
<result property="address.country" column="country"/>
<result property="address.province" column="province"/>
<result property="address.city" column="city"/>
</resultMap>
<select id="getStudentByID" parameterType="int" resultMap="StudentResultMap">
select * from Student where ID = #{ID}
</select>
ID:1 name:yoona age:24 school:西安电子科技大学 address:null |
<select id="getAllStudents" resultMap="StudentWithAddressResultMap">
SELECT S.ID,name,age,school,A.ID as addressID,country,province,city
FROM Student S LEFT OUTER JOIN Address A ON S.addressID = A.ID
</select>
所有学生信息如下:
ID:3 name:sunn age:20 school:山东大学 address:[ ID:1 country:中国 province:山东 city:淄博 ]
ID:4 name:sun age:21 school:山东科技大学 address:[ ID:1 country:中国 province:山东 city:淄博 ]
ID:1 name:yoona age:24 school:西安电子科技大学 address:[ ID:2 country:中国 province:陕西 city:西安 ]
ID:2 name:sunny age:20 school:西安电子科技大学 address:[ ID:2 country:中国 province:陕西 city:西安 ]
|
下载:点击打开链接