java实体映射关系_[刘阳Java]_MyBatis_实体关系映射_第8讲

MyBatis既然是一个ORM框架,则它也有像Hibernate那样的一对多,多对多,多对一的实体关系映射功能。下面我们就来介绍一下如何使用MyBatis的实体关系映射

1.MyBatis实体关系映射,对于我个人来讲常用的有下面两种

多对一:在子表的映射文件中添加association

一对多:在父表的映射文件中添加collection

2.MyBatis中多对一的案例

先创建两张表

CREATE TABLE`student` (

`sid`int(11) default NULL,

`sname`varchar(10) default NULL,

`t_id`int(11) default NULL) ;CREATE TABLE`teacher` (

`t_id`int(11) NOT NULL,

`t_name`varchar(20) default NULL,PRIMARY KEY(`t_id`)

) ;

创建实体类

packagecom.gxa.pojo;public classTeacher {privateintt_id;privateString t_name;publicintgetT_id() {returnt_id;

}public voidsetT_id(intt_id) {this.t_id =t_id;

}publicString getT_name() {returnt_name;

}public voidsetT_name(String t_name) {this.t_name =t_name;

}

}packagecom.gxa.pojo;public classStudent {privateintsid;privateString sname;privateTeacher teacher;publicintgetSid() {returnsid;

}public voidsetSid(intsid) {this.sid =sid;

}publicString getSname() {returnsname;

}public voidsetSname(String sname) {this.sname =sname;

}publicTeacher getTeacher() {returnteacher;

}public voidsetTeacher(Teacher teacher) {this.teacher =teacher;

}

}

创建StudentMapper.xml文件,在此XML文件中配置多对一的关系

select * from student a, teacher b where a.t_id = b.t_id and sid = 123

完成多对一关系的代码测试

packagecom.gxa.test;importjava.io.IOException;importjava.io.Reader;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;importorg.junit.Test;importcom.gxa.pojo.Student;public classTest03 {private staticSqlSessionFactorysqlSessionFactory;private staticReader reader;static{try{

reader= Resources.getResourceAsReader("config.xml");

sqlSessionFactory= newSqlSessionFactoryBuilder().build(reader);

}catch(IOException e) {

e.printStackTrace();

}

}

@Testpublic voidm01() {

SqlSessionsqlSession=sqlSessionFactory.openSession();

String sql= "com.gxa.mapper.StudentMapper.getStudent";

Student student=sqlSession.selectOne(sql);

System.out.println(student.getSname()+ " === " +student.getTeacher().getT_name());

sqlSession.close();

}

}

3.MyBatis一对多的案例

修改Student和Teacher这两个实体类

packagecom.gxa.pojo;public classStudent {privateintsid;privateString sname;privateTeacher teacher;publicintgetSid() {returnsid;

}public voidsetSid(intsid) {this.sid =sid;

}publicString getSname() {returnsname;

}public voidsetSname(String sname) {this.sname =sname;

}publicTeacher getTeacher() {returnteacher;

}public voidsetTeacher(Teacher teacher) {this.teacher =teacher;

}

}packagecom.gxa.pojo;importjava.util.List;public classTeacher {privateintt_id;privateString t_name;private Liststudent;public ListgetStudent() {returnstudent;

}public void setStudent(Liststudent) {this.student =student;

}publicintgetT_id() {returnt_id;

}public voidsetT_id(intt_id) {this.t_id =t_id;

}publicString getT_name() {returnt_name;

}public voidsetT_name(String t_name) {this.t_name =t_name;

}

}

创建TeacherMapper的映射文件,在此文件的标签中加入

select* from teacher a,student b where a.t_id = b.t_id and a.t_id = 1

测试

packagecom.gxa.test;importjava.io.IOException;importjava.io.Reader;importjava.util.List;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;importorg.junit.Test;importcom.gxa.pojo.Student;importcom.gxa.pojo.Teacher;public classTest03 {private staticSqlSessionFactorysqlSessionFactory;private staticReader reader;static{try{

reader= Resources.getResourceAsReader("config.xml");

sqlSessionFactory= newSqlSessionFactoryBuilder().build(reader);

}catch(IOException e) {

e.printStackTrace();

}

}

@Testpublic voidm01() {

SqlSessionsqlSession=sqlSessionFactory.openSession();

String sql= "com.gxa.mapper.StudentMapper.getStudent";

Student student=sqlSession.selectOne(sql);

System.out.println(student.getSname()+ " === " +student.getTeacher().getT_name());

sqlSession.close();

}

@Testpublic voidm02() {

SqlSessionsqlSession=sqlSessionFactory.openSession();

String sql= "com.gxa.mapper.TeacherMapper.getTeacher";

Teacher teacher=sqlSession.selectOne(sql);

List student =teacher.getStudent();for(Student s : student) {

System.out.println(teacher.getT_name()+ "====" +s.getSname());

}

sqlSession.close();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值