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文件内 ...
随机推荐
- BitCoin Trading Strategies BackTest With PyAlgoTrade
Written by Khang Nguyen Vo, khangvo88@gmail.com, for the RobustTechHouse blog. Khang is a graduate f ...
- 借助 Django 的 smart_str 和 smart_unicode 进行编码转换(转)
原文:http://www.dirk.sh/diary/using-django-smart_str-smart_unicode/ Django 为字符编码的转换提供了非常简洁的方法: 1.djang ...
- Linux vim编辑器常用命令
Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器 常用的vim命令如下图 补充: num+命令 对命令执行num次,如 5dd:剪切一行 * 5 即剪切5行,其它如此 /text ...
- 网页中Cache各字段含义
Pragma 当该字段值为"no-cache"的时候(事实上现在RFC中也仅标明该可选值),会知会客户端不要对该资源读缓存,即每次都得向服务器发一次请求才行. Expires 有了 ...
- notepade++使用
Notepad++也可以实现双视图/双窗口对比显示,目前最新版本(6.32)只能支持双视图显示,而且只能支持左右视图,希望后续版本能得到改进. 我们打开两个需要对比显示的源文件 默认的情况下是分成了两 ...
- gbdt调参的小结
关键部分转自http://www.cnblogs.com/pinard/p/6143927.html 第一次知道网格搜索这个方法,不知道在工业中是不是用这种方式 1.首先从步长和迭代次数入手,选择一个 ...
- boost单元测试框架
头文件: #include <boost/test/unit_test.hpp> 编译加:-lboost_unit_test_framework 单元测试: 需要定义BOOST_TEST_ ...
- boost implicit_cast
在stackoverflow上看到这个帖子, 于是发现了boost::implicit_cast这个小东西. 先来看看这段代码: struct top {}; struct mid_a : top { ...
- matplotlib.pyplot 让数据可视化
1.条形图 import matplotlib.pyplot as plt plt.style.use('ggplot') # 使用ggplot样式来模拟ggplot2风格的图形,ggplot2是一个 ...
- Gentoo64无法启动eth0的问题
Gentoo64在net文件中配置好eth0的静态IP 代码 1.2: /etc/conf.d/net文件的一个示例 # DHCP config_eth0=( "dhcp" ) # ...