mybatis实战教程三:多对多关联
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实战教程》
mybatis实战教程三:多对多关联的更多相关文章
- mybatis实战教程三:mybatis和springmvc整合
一.搭建maven环境,引入相关jar 参考demo 二.写web.xml <?xml version="1.0" encoding="UTF-8"?&g ...
- 【转】mybatis实战教程(mybatis in action),mybatis入门到精通
MyBatis 目录(?)[-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis ...
- mybatis实战教程(mybatis in action),mybatis入门到精通
转自:http://www.yihaomen.com/article/java/302.htm (读者注:其实这个应该叫做很基础的入门一下下,如果你看过hibernate了那这个就非常的简单) (再加 ...
- mybatis实战教程(mybatis in action),mybatis入门到精通(转)
转自:http://www.yihaomen.com/article/java/302.htm (读者注:其实这个应该叫做很基础的入门一下下,如果你看过Hibernate了那这个就非常的简单) (再加 ...
- 转)mybatis实战教程(mybatis in action),mybatis入门到精通
mybatis实战教程(mybatis in action),mybatis入门到精通 http://limingnihao.iteye.com/blog/781671 http://blog.csd ...
- 1.mybatis实战教程mybatis in action之一开发环境搭建
转自:https://www.cnblogs.com/shanheyongmu/p/5652471.html 什么是mybatis MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框 ...
- mybatis实战教程二:多对一关联查询(一对多)
多对一关联查询 一.数据库关系.article表和user表示多对一的关系 CREATE TABLE `article` ( `id` ) NOT NULL AUTO_INCREMENT, `user ...
- NHibernate教程(11)--多对多关联查询
本节内容 多对多关系引入 多对多映射关系 多对多关联查询 1.原生SQL关联查询 2.HQL关联查询 3.Criteria API关联查询 结语 多对多关系引入 让我们再次回顾在第二篇中建立的数据模型 ...
- Node+Express+MongoDB + Socket.io搭建实时聊天应用实战教程(三)--前后端环境配置
前言 之前都是介绍一些基础知识,在这一节,我们就要开始实战coding了.正所谓磨刀不误砍柴工,准备工作显得尤为重要.很多demo只是追求效果的实现,并不注重整个demo的架构性.从我个人的角度看来, ...
随机推荐
- 【软件】关于Notepad++(32位)
1.Notepad++安装包 简介:NotePad++是一个轻量级的代码编辑器,占用内存少,运行速度快. 官网网址:https://notepad-plus-plus.org/ 百度网盘:https: ...
- STM32f103x IAP远程升级小结
最近在面试的时候遇到一个关于IAP远程程序升级的问题,由于之前所做的项目没有涉及到远程升级需求,当时一脸懵呆,不过回答的还是不错的,今天针对STM32F103系列调试了IAP的程序,这里做一下小结,如 ...
- 模拟uClinux系统调用
这篇文章原来放在CU上的,现在挪过来了.CU上设置不可见了. 1. 目标 这里主要是实验一下uclinux的系统调用. 2. 环境 OS :vmware + red ...
- Mac 下 android/iOS https抓包
一.Charles简介 Charles,是用Java开发的,所以跨平台,不仅可以在Mac上使用,Linux以及Window下都是可以使用的,当然需要安装JDK,才能运行,但目前是收费的. 二.下载 官 ...
- javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
前言 今天一个朋友让我帮他做一下tomcat的https配置,中间遇到了标题中这个错误,因此记录了一下过程,服务器.域名.证书.tomcat都已经准备好,就是需要配置一下即可,用的是阿里云的证书服务, ...
- Yii2.0源码阅读-从路由到控制器
之前的文章弄清了一次请求的开始到结束.主要讲了Yii Applicaton实例的创建.初始化,UrlManager如何返回Yii中的路由信息,到runAction,最后将Response发送给客户端. ...
- 使用Android-PullToRefresh实现下拉刷新功能
源代码:https://github.com/chrisbanes/Android-PullToRefresh 一. 导入类库 将Library文件夹作为Android项目Import到Eclipse ...
- 认识Sass和Compass
第一章 Sass和Compass让样式表重焕青春 // 内容概要// 开始学习Sass和动态样式表// 用Sass更高效地写样式表// Compass简介// 用Compass迎接工程实践中的样式挑战 ...
- 学习web前端技术的笔记,仅供自己查阅备忘,图片上传预览
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- CTF---Web入门第八题 Guess Next Session
Guess Next Session分值:10 来源: iFurySt 难度:易 参与人数:3870人 Get Flag:1672人 答题人数:1690人 解题通过率:99% 写个算法没准就算出来了, ...