数据库
create database student charset utf8;
use student;CREATE TABLE student (
sno INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR(12) NOT NULL,
sage INT NOT NULL,
addressID INT,
FOREIGN KEY (addressID)
REFERENCES address (id)
)
》》》》》》》》》》》》》
use student;
CREATE TABLE address (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
sheng VARCHAR(12) NOT NULL,
shi VARCHAR(12) NOT NULL,
qu VARCHAR(12) NOT NULL
)
》》》》》》》》》
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.imis.mappers.StudentMapper">
<!-- 第一种 -->
<!-- <resultMap type="Student" id="StudentResult">
<id property="sno" column="sno"/>
<result property="sname" column="sname"/>
<result property="sage" column="sage"/>
<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> -->
<!-- 第二种 -->
<!-- <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="sno" column="sno"/>
<result property="sname" column="sname"/>
<result property="sage" column="sage"/>
<association property="address" resultMap="AddressResult"></association>
</resultMap> -->
<!-- 第三种 -->
<!-- <resultMap type="Student" id="StudentResult">
<id property="sno" column="sno"/>
<result property="sname" column="sname"/>
<result property="sage" column="sage"/>
<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> -->
<!-- 第四种 -->
<resultMap type="Student" id="StudentResult">
<id property="sno" column="sno"/>
<result property="sname" column="sname"/>
<result property="sage" column="sage"/>
<association property="address" column="addressID" select="com.imis.mappers.AddressMapper.findByID"></association>
</resultMap>
<select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
select * from student t1,address t2 where t1.addressId=t2.id and t1.sno=#{sno}
</select>
<insert id="add" parameterType="Student" >
insert into student values(null,#{sname},#{sage},#{addressID})
</insert>
<update id="update" parameterType="Student">
update student set sname=#{sname},sage=#{sage},addresssID=#{addressID} where sno=#{sno}
</update>
<delete id="delete" parameterType="Integer">
delete from student where sno=#{sno}
</delete>
<select id="findById" parameterType="Integer" resultType="Student">
select * from student where sno=#{sno}
</select>
<select id="find" resultMap="StudentResult">
select * from student
</select>
</mapper>
》》》》》》》》》
StudentMapper.java文件
import java.util.List;
import com.imis.model.Student;
public interface StudentMapper {
public int add(Student student) ;
public int update(Student student);
public int delete(int sno);
public Student findById(int sno);
public List<Student> find();
public Student findStudentWithAddress(int sno);
}
》》》》》》》
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.imis.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>
》》》》》》》
AddressMapper.java
import com.imis.model.Address;
public interface AddressMapper {
public Address findByID(int id);
}
》》》》》》》》
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.imis.mappers.StudentMapper;
import com.imis.model.Student;
import com.imis.util.SqlSessionFactoryUtil;
public class StudentTest3 {
private static Logger logger=Logger.getLogger(StudentTest2.class);
private SqlSession sqlSession=null;
private StudentMapper studentMapper=null;
@Before
public void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
studentMapper=sqlSession.getMapper(StudentMapper.class);
}
@After
public void tearDown() throws Exception {
sqlSession.close();
}
@Test
public void testFindStudentWithAdress() {
logger.info("查询学生(带地址)");
Student student=studentMapper.findStudentWithAddress(1);
System.out.println(student);
System.out.println("1");
}
}
》》》》》》》》》
Model
public class Student {
private int sno;
private String sname;
private int sage;
private Address address;
public Student() {
super();
}
public Student(int sno, String sname, int sage) {
super();
this.sno = sno;
this.sname = sname;
this.sage = sage;
}
public Student(String sname, int sage) {
super();
this.sname = sname;
this.sage = sage;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
@Override
public String toString() {
return "Student [sno=" + sno + ", sname=" + sname + ", sage=" + sage + ", address=" + address + "]";
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
》》》》》》》》》》》
public class Address {
private int id;
private String sheng;
private String shi;
private String qu;
public Address() {
super();
}
public int getId() {
return id;
}
public void setId(int 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;
}
@Override
public String toString() {
return "Address [id=" + id + ", sheng=" + sheng + ", shi=" + shi + ", qu=" + qu + "]";
}
}
》》》》》》》》
测试
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.imis.mappers.StudentMapper;
import com.imis.model.Student;
import com.imis.util.SqlSessionFactoryUtil;
public class StudentTest3 {
private static Logger logger=Logger.getLogger(StudentTest2.class);
private SqlSession sqlSession=null;
private StudentMapper studentMapper=null;
@Before
public void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
studentMapper=sqlSession.getMapper(StudentMapper.class);
}
@After
public void tearDown() throws Exception {
sqlSession.close();
}
@Test
public void testFindStudentWithAdress() {
logger.info("查询学生(带地址)");
Student student=studentMapper.findStudentWithAddress(1);
System.out.println(student);
}
}