mybatis实现关联表查询(一对一)

1.创建数据库表

mybatis实现关联表查询(一对一)_第1张图片

2.创建实体类

package com.rjxy.select.connect;

public class Classes {
@Override
	public String toString() {
		return "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher + "]";
	}
private int id;
private String name;
private Teacher teacher;
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public Teacher getTeacher() {
	return teacher;
}
public void setTeacher(Teacher teacher) {
	this.teacher = teacher;
}
public Classes(int id, String name, Teacher teacher) {
	super();
	this.id = id;
	this.name = name;
	this.teacher = teacher;
}
public Classes() {
	super();
}

}



package com.rjxy.select.connect;

public class Teacher {
@Override
	public String toString() {
		return "Teacher [id=" + id + ", name=" + name + "]";
	}
private int id;
private String name;
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public Teacher(int id, String name) {
	super();
	this.id = id;
	this.name = name;
}
public Teacher() {
	super();
}

}

方式一:

3-1.配置classMapper.xml







    
    
      
         
           	
      




4-1.注册classMapper.xml


     













 

5-1.测试

public class TestClass01 {
	@Test
	public void testSelect() {
		SqlSession session = MybatisUtil.getSqlSession();
		String statement="com.rjxy.select.connect.classMapper.selectClass";
		Classes classes=session.selectOne(statement,1);
		session.commit();
		session.close();
		System.out.println("con1-result:" +classes);
	}
}

mybatis实现关联表查询(一对一)_第2张图片

你可能感兴趣的:(学习心得共享)