欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis多对多关联实战教程(推荐)

程序员文章站 2024-04-02 08:38:28
mybatis3.0 添加了association和collection标签专门用于对多个相关实体类数据进行级联查询,但仍不支持多个相关实体类数据的级联保存和级联删除操作...

mybatis3.0 添加了association和collection标签专门用于对多个相关实体类数据进行级联查询,但仍不支持多个相关实体类数据的级联保存和级联删除操作

一、创建student、teacher和stu_teach_rel三张张表

drop table if exists `student`;
create table `student` (
 `id` int(11) not null,
 `name` varchar(255) default null,
 `age` int(11) default null,
 `gender` varchar(255) default null,
 primary key (`id`)
) engine=innodb default charset=utf8;

insert into `student` values ('1', '刘德华', '55', '0');
insert into `student` values ('2', '张惠妹', '49', '1');
insert into `student` values ('3', '谢霆锋', '35', '0');
insert into `student` values ('4', '王菲', '47', '1');
insert into `student` values ('5', '汪峰', '48', '0');
insert into `student` values ('6', '章子怡', '36', '1');
drop table if exists `teacher`;
create table `teacher` (
 `id` int(11) not null,
 `name` varchar(255) default null,
 `gender` varchar(255) default null,
 `subject` varchar(255) default null,
 `degree` varchar(255) default null,
 primary key (`id`)
) engine=innodb default charset=utf8;

insert into `teacher` values ('1', '王晶', '0', 'chinese', '大专');
insert into `teacher` values ('2', '冯小刚', '0', 'english', '本科');
insert into `teacher` values ('3', '吴京', '0', 'mathematics', '大专');
insert into `teacher` values ('4', '王倦', '1', 'mathematics', '研究生');
drop table if exists `stu_teach_rel`;
create table `stu_teach_rel` (
 `id` int(11) not null,
 `stu_id` int(11) not null,
 `teach_id` int(11) not null,
 primary key (`id`)
) engine=innodb default charset=utf8;


insert into `stu_teach_rel` values ('1', '1', '1');
insert into `stu_teach_rel` values ('2', '2', '1');
insert into `stu_teach_rel` values ('3', '1', '2');
insert into `stu_teach_rel` values ('4', '2', '2');
insert into `stu_teach_rel` values ('5', '2', '3');
insert into `stu_teach_rel` values ('6', '3', '1');
insert into `stu_teach_rel` values ('7', '3', '2');
insert into `stu_teach_rel` values ('8', '1', '3');
insert into `stu_teach_rel` values ('9', '4', '1');
insert into `stu_teach_rel` values ('10', '4', '2');
insert into `stu_teach_rel` values ('11', '5', '3');
insert into `stu_teach_rel` values ('12', '5', '4');
insert into `stu_teach_rel` values ('13', '6', '1');
insert into `stu_teach_rel` values ('14', '6', '3');

二、新建和表相关的实体类

package com.yihaomen.mybatis.model;
import com.yihaomen.mybatis.enums.gender;
import java.util.list;

public class student {
 private string id;
 private string name;
 private int age;
 private gender gender;
 private list<teacher> teachers;

 setters&getters
 
}
package com.yihaomen.mybatis.model;
import com.yihaomen.mybatis.enums.gender;
import com.yihaomen.mybatis.enums.subject;
import java.util.list;

public class teacher {
 private int id;
 private string name;
 private gender gender;
 private subject subject;
 private string degree;
 private list<student> students;

 setters&getters
}

三、新建映射关系

package com.yihaomen.mybatis.dao;
import com.yihaomen.mybatis.model.student;
import org.springframework.stereotype.repository;
import java.util.list;

@repository
public interface studentmapper {
 list<student> selectstudents();
}

student.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.yihaomen.mybatis.dao.studentmapper">
 <resultmap id="studentmap" type="student">
  <id property="id" column="id" />
  <result property="name" column="name" />
  <result property="age" column="age" />
  <result property="gender" column="gender" typehandler="org.apache.ibatis.type.enumordinaltypehandler" />
 </resultmap>
 <resultmap id="collectionmap" type="student" extends="studentmap">
  <collection property="teachers" oftype="teacher">
   <id property="id" column="teach_id" />
   <result property="name" column="tname"/>
   <result property="gender" column="tgender" typehandler="org.apache.ibatis.type.enumordinaltypehandler"/>
   <result property="subject" column="tsubject" typehandler="org.apache.ibatis.type.enumtypehandler"/>
   <result property="degree" column="tdegree" javatype="string" jdbctype="varchar"/>
  </collection>
 </resultmap>
 <select id="selectstudents" resultmap="collectionmap">
  select
   s.id, s.name, s.gender, t.id teach_id, t.name tname, t.gender tgender, t.subject tsubject, t.degree tdegree
  from
   student s
  left join
   stu_teach_rel str
  on
   s.id = str.stu_id
  left join
   teacher t
  on
   t.id = str.teach_id
 </select>
</mapper>

四、在configuration.xml中配置相关mapper

<?xml version="1.0" encoding="utf-8" ?>
<!doctype configuration public "-//mybatis.org//dtd config 3.0//en"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 <properties resource="jdbc.properties"/>
 <typealiases>
  <typealias alias="student" type="com.yihaomen.mybatis.model.student" />
  <typealias alias="teacher" type="com.yihaomen.mybatis.model.teacher" />
 </typealiases>

 <environments default="development">
  <environment id="development">
   <transactionmanager type="jdbc"/>
   <datasource type="pooled">
    <property name="driver" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
   </datasource>
  </environment>
 </environments>

 <mappers>
  <mapper resource="com/yihaomen/mybatis/model/student.xml"/>
 </mappers>
</configuration>

五、测试

package com.yihaomen.service.student;
import com.yihaomen.mybatis.dao.studentmapper;
import com.yihaomen.mybatis.model.student;
import com.yihaomen.mybatis.model.teacher;
import com.yihaomen.service.basetest;
import org.apache.ibatis.session.sqlsession;
import org.apache.ibatis.session.sqlsessionfactory;
import java.util.list;

public class teststudent extends basetest {
 public static void teststuteachrela() {
  sqlsessionfactory sqlsessionfactory = getsession();
  sqlsession session = sqlsessionfactory.opensession();
  studentmapper mapper = session.getmapper(studentmapper.class);
  list<student> list = mapper.selectstudents();

  for(student s : list) {
   system.out.println("------------------");
   system.out.println(s.getname() + "," + s.getage() + "," + s.getgender());
   for(teacher t : s.getteachers()) {
    system.out.println(t.getname() + "," + t.getgender() + "," + t.getsubject());
   }
  }
 }

 public static void main(string[] args) {
  teststuteachrela();
 }
}

以上这篇mybatis多对多关联实战教程(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。