mybatis的一对多
1、配置文件
db.properties
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
db.username=root
db.password=123456
SqlMapConfig.xml
<?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> <!-- 加载java的配置文件 -->
<properties resource="config/db.properties"/> <!-- 配置mybatis的环境信息,与spring整合,该信息由spring来管理 -->
<environments default="development">
<environment id="development">
<!-- 配置JDBC事务控制,由mybatis进行管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源,采用mybatis连接池 -->
<dataSource type="POOLED">
<property name="driver" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</dataSource>
</environment>
</environments> <!-- 加载映射文件 -->
<mappers>
<mapper resource="config/mapper.xml" />
</mappers> </configuration>
mapper.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.xiaostudy.domain.Mapper"> <!-- ========================================================================================== -->
<!-- 一对多之resultMap1 -->
<select id="findOfTeacher" resultMap="OfTeacher">
select * from teacher t, student s where t.tid=s.tid and t.tid=#{id}
</select>
<resultMap type="com.xiaostudy.domain.Teacher" id="OfTeacher">
<id column="tid" property="tid"/>
<result column="tname" property="tname"/>
<collection property="student" ofType="com.xiaostudy.domain.Student" ><!-- javaType="HashSet" -->
<id column="sid" property="sid"></id>
<result column="tid" property="tid"/>
<result column="sname" property="sname"/>
</collection>
</resultMap>
<!-- ========================================================================================== --> <!-- ========================================================================================== -->
<!-- 一对多之resultMap2 -->
<select id="findOfTeachers" resultMap="OfTeachers">
select * from teacher t, student s where t.tid=s.tid and t.tid=#{id}
</select>
<resultMap type="com.xiaostudy.domain.Teacher" id="OfTeachers">
<id column="tid" property="tid"/>
<result column="tname" property="tname"/>
<collection property="student" ofType="com.xiaostudy.domain.Student" select="getStudent" column="tid"/>
</resultMap> <select id="getStudent" parameterType="int" resultType="com.xiaostudy.domain.Student">
select * from student s where s.tid = #{id}
</select>
<!-- ========================================================================================== --> </mapper>
2、domain类
Student.java
package com.xiaostudy.domain;
public class Student {
private int sid;
private int tid;
private String sname;
private Teacher teacher;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", tid=" + tid + ", sname=" + sname
+ ", teacher=" + teacher + "]";
}
}
Teacher.java
package com.xiaostudy.domain;
import java.util.Set;
public class Teacher {
private int tid;
private String tname;
private Set<Student> student;
public Set<Student> getStudent() {
return student;
}
public void setStudent(Set<Student> student) {
this.student = student;
}
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
@Override
public String toString() {
return "Teacher [tid=" + tid + ", tname=" + tname + ", student=" + student + "]";
}
}
3、代理类Mapper.java
package com.xiaostudy.domain;
import java.util.List;
public interface Mapper {
// 一对多之resultMap1
public List<Teacher> findOfTeacher(int id);
// 一对多之resultMap2
public List<Teacher> findOfTeachers(int id);
}
4、测试类
package com.xiaostudy.test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.xiaostudy.domain.Mapper;
import com.xiaostudy.domain.Teacher; /**
* @desc 测试类
* @author xiaostudy
*
*/
public class MybatisTest { public static void main(String[] args) throws IOException {
String resource = "config/SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource); // 创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession(); // 获取一个代理dao实现
Mapper mapper = sqlSession.getMapper(Mapper.class); //一对多之resultMap1
/*List<Teacher> list = mapper.findOfTeacher(1);
for(Teacher t : list) {
System.out.println(t);
}*/ //一对多之resultMap2
List<Teacher> list = mapper.findOfTeachers(1);
for(Teacher t : list) {
System.out.println(t);
} sqlSession.close(); } }
5、数据库
teacher表

student表

mybatis的一对多的更多相关文章
- mybatis的一对多,多对一,以及多对对的配置和使用
1.本文章是无意中看见易百教程的Mybatis教程才注意到这个问题,平时都仅仅是在用CRUD,忽略了这方面的问题,真实十分羞愧 2.首先我们开始对mybatis的一对多的探究 根据这个应用场景 ...
- Mybatis配置一对多的关联关系(五)
问题:是查询一个部门中的员工? 一.web项目构架 二.lib文件的jar 三.配置大小配置和该工具类 1大配置mybatis-config.xml <?xml version="1. ...
- Mybatis学习——一对多关联表查询
1.实体类 public class Student { private int id; private String name; } public class Classes { private i ...
- Mybatis 中一对多,多对一的配置
现在有很多电商平台,就拿这个来说吧.顾客跟订单的关系,一个顾客可以有多张订单,但是一个订单只能对应一个顾客. 一对多的顾客 <?xml version="1.0" encod ...
- Mybatis【一对多、多对一、多对多】知识要点
Mybatis[多表连接] 我们在学习Hibernate的时候,如果表涉及到两张的话,那么我们是在映射文件中使用<set>..<many-to-one>等标签将其的映射属性关联 ...
- mybatis进行一对多时发现的问题总结
1.定义一对多xml文件时,所有的resultMap中的column的值一定不要重复,否则mybatis会发生错误,如果有重名,定义别名,column中的名字一定要与查询出的名字一致,如: 52行的别 ...
- mybatis之一对多
今天主要话题围绕这么几个方面? mybatis一对多示例 sql优化策略 一.mybatis之一对多 在说一对多之前,顺便说一下一对一. 一对一,常见的例子,比如以常见的班级例子来说,一个班主任只属于 ...
- mybatis 中一对多、多对一、多对多、父子继承关系
mybatis 中处理一对多.多对一.多对多.父子继承关系的有关键词:association .collection .discriminator id – 一个 ID 结果:标记出作为 ID 的结果 ...
- MyBatis:一对多关联查询
MyBatis从入门到放弃四:一对多关联查询 前言 上篇学习了一对一关联查询,这篇我们学习一对多关联查询.一对多关联查询关键点则依然是配置resultMap,在resultMap中配置collecti ...
- mybatis实现一对多连接查询
问题:两个对象User和Score,它们之间的关系为一对多. 底层数据库为postgresql,ORM框架为mybatis. 关键代码如下: mybatis配置文件如下: mybatis.xml文件内 ...
随机推荐
- Spark Streaming源码分析 – DStream
A Discretized Stream (DStream), the basic abstraction in Spark Streaming, is a continuous sequence o ...
- Poloniex API 文档
Examples PHP wrapper by compcentral: http://pastebin.com/iuezwGRZ Python wrapper by oipminer: http:/ ...
- WeQuant交易策略—EMV
EMV指标策略 简介 EMV(Ease of Movement Value, 简易波动指标),它是由RichardW.ArmJr.根据等量图和压缩图的原理设计而成, 目的是将价格与成交量的变化结合成一 ...
- 与python的第一次邂逅
python简介 一.什么是python python是一种面向对象.直译式的计算机程序语言,所以有了武老师的那句名言:一切皆为对象 python的设计哲学是:“优雅”,“明确”,“简单” pytho ...
- 哈密顿绕行世界问题---hdu2181(全排列问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181 题意很容易理解,dfs就可以了 #include <iostream> #inclu ...
- BroadcastReceiver 翻译
1. 动态注册与退出 If registering a receiver in your Activity.onResume() implementation, you should unregist ...
- 使用jQuery为文本框、单选框、多选框、下拉框、下拉多选框设值及返回值的处理
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- mysql lock
http://blog.chinaunix.net/uid-21505614-id-289450.html http://bbs.csdn.net/topics/340127237 http://ww ...
- Element 中表单非必填数据项 必须为数字的验证问题
Element-ui 的el-form组建中,自带基本的验证功能,比如某些项必填的验证,直接加入rules 规则中即可,如下实例: 在页面中书写如下: <el-form-item label=& ...
- php 单线程 (http://bbs.csdn.net/topics/390778072)
以前想php单线程,网站肯定是用于多人访问的,如果访问量大,那岂不是出现排队问题? apache+php是阻塞型处理,nginx+php是异步非阻塞的,php有进程管理器,fpm fcgi什么的.ph ...