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 ('', '刘德华', '', '');
INSERT INTO `student` VALUES ('', '张惠妹', '', '');
INSERT INTO `student` VALUES ('', '谢霆锋', '', '');
INSERT INTO `student` VALUES ('', '王菲', '', '');
INSERT INTO `student` VALUES ('', '汪峰', '', '');
INSERT INTO `student` VALUES ('', '章子怡', '', '');
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 ('', '王晶', '', 'CHINESE', '大专');
INSERT INTO `teacher` VALUES ('', '冯小刚', '', 'ENGLISH', '本科');
INSERT INTO `teacher` VALUES ('', '吴京', '', 'MATHEMATICS', '大专');
INSERT INTO `teacher` VALUES ('', '王倦', '', '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 ('', '', '');
INSERT INTO `stu_teach_rel` VALUES ('', '', '');
INSERT INTO `stu_teach_rel` VALUES ('', '', '');
INSERT INTO `stu_teach_rel` VALUES ('', '', '');
INSERT INTO `stu_teach_rel` VALUES ('', '', '');
INSERT INTO `stu_teach_rel` VALUES ('', '', '');
INSERT INTO `stu_teach_rel` VALUES ('', '', '');
INSERT INTO `stu_teach_rel` VALUES ('', '', '');
INSERT INTO `stu_teach_rel` VALUES ('', '', '');
INSERT INTO `stu_teach_rel` VALUES ('', '', '');
INSERT INTO `stu_teach_rel` VALUES ('', '', '');
INSERT INTO `stu_teach_rel` VALUES ('', '', '');
INSERT INTO `stu_teach_rel` VALUES ('', '', '');
INSERT INTO `stu_teach_rel` VALUES ('', '', '');

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

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();
}
}

https://gitee.com/huayicompany/springmvc-mybatis

参考:

[1]《mybati实战教程》

最新文章

  1. Apache InterfaceAudience
  2. Day Four(Beta)
  3. 【T_SQL】 基础 事务
  4. Spark随笔(一):Spark的综合认识
  5. 便宜有好货:Oracle免费的便捷Web应用开发框架
  6. 文件系统管理 之 Linux 创建文件系统及挂载文件系统流程详解
  7. hunnu 修路
  8. python (10) 文件夹的创建与文件夹的删除
  9. Unity Shader:Blur
  10. 初识MFC,WinForm,WPF,Q&#39;t
  11. C# - (0x80040154): Retrieving the COM class factory for component with CLSID {877AA945-1CB2-411C-ACD7-C70B1F9E2E32} failed
  12. 通过PLSQL Developer导入SQL文件
  13. Java中Date各种相关用法
  14. JavaBean在DAO设计模式简介
  15. AutoCAD 2012安装错误,与.net framework (1603错误)以及ms2005vc++的问题。
  16. Layui文件上传样式在ng-dialog不显示的问题处理
  17. 谈谈Javascript异步代码优化
  18. C# 发送电子邮件源码片段
  19. U盘中毒后变为快捷方式的解决方法
  20. 实战--使用lvs实现四层负载均衡,转发到后端nginx

热门文章

  1. 安装Django时报错&#39;module&#39; object has no attribute &#39;lru_cache&#39;
  2. 总结基础OOP(面向对象)
  3. tornado SSL 证书获取与服务器配置
  4. Jenkins-Dingding Notification Plugin 配置
  5. HashTable源码阅读
  6. [转]-nohup-真正的Shell后台运行
  7. 【转载】OAuth2 流程
  8. CSS基础知识(display和visibility、overflow、文档流)
  9. 线性表的链式存储结构的实现及其应用(C/C++实现)
  10. CTF---安全杂项入门第一题 丘比龙的最爱