Mybatis下的数据库表自关联查询
来源:互联网 发布:网页怎么连接数据库 编辑:程序博客网 时间:2024/06/05 15:37
简单粗暴点,直接上代码吧
mysql创建一张表,表名prentice
表中林朝英有两个徒弟,分别是小龙女和李莫愁。而小龙女有徒弟杨过,李莫愁有徒弟洪凌波。
现在需要通过pid查询出师傅所收的全部徒弟信息。
1. 创建 Prentice实体类 (略)
2. 创建PrenticeMapper接口package com.wl.mapper;import java.util.List;import com.wl.pojo.Prentice;//根据pid查询徒弟信息public interface PrenticeMapper {List<Prentice> selectPrenticeByPid(int pid);}
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- 查询师傅所带的徒弟 --><mapper namespace="com.wl.mapper.PrenticeMapper"><resultMap type="Prentice" id="prenticeMapper"><id column="id" property="id"/><result column="name" property="name"/><collection property="prentices" ofType="Prentice"select="selectPrenticeByPid"column="id"/></resultMap><select id="selectPrenticeByPid" resultMap="prenticeMapper">select id,name from prentice where pid=#{pid}</select> </mapper>
4. 工具类MybatisUtil及mybatis.xml 文件的配置此处就略写啦,相信大家都能搞定。下面就直接上测试
5. 测试类
public class TestPrentice {private PrenticeMapper mapper;private SqlSession sqlSession;@Beforepublic void Before(){sqlSession = MyBatisUtil.getSqlSession();mapper = sqlSession.getMapper(PrenticeMapper.class);}@Afterpublic void After(){if(sqlSession!= null){sqlSession.close();}}@Testpublic void selectPrenticeByPid(){List<Prentice> prentices = mapper.selectPrenticeByPid(1);for (Prentice prentice : prentices) {System.out.println(prentice);}}}
经测试,小龙女及其徒弟杨过,李莫愁及其徒弟洪凌波都被打印在控制台~~~
关于查询师傅及其徒弟的信息,在此就不贴出来啦~~~
不足之处,欢迎指正!
阅读全文
0 0
- Mybatis下的数据库表自关联查询
- MyBatis--关联关系查询--自关联
- Mybatis的关联查询
- mybatis的关联查询
- MyBatis--关联关系查询--自关联--一对多--查询指定父节点的所有子节点
- Mybatis学习(八)mybatis环境下以接口的方式进行的关联表查询
- mybatis表关联查询
- Mybatis关联表查询
- 表自关联查询
- mybatis关联查询--自连接 传参
- mybatis多表关联查询的问题
- Mybatis的各种关联查询
- (5).Mybatis的关联查询
- MyBatis实现关联表查询
- Mybatis实现关联表查询
- MyBatis实现关联表查询
- Mybatis---一对一关联表查询
- mybatis-实现关联表查询
- 【dubbo】直连提供者+只订阅
- 队列的用法
- 2017ICPC北京网络赛Minimum(线段树+区间最值)
- Java 位运算(移位、位与、或、异或、非)
- 生成指定个数的随机码,同时包含大小写字母和数字
- Mybatis下的数据库表自关联查询
- 字典树(Trie)
- MAC电脑 系统 恢复出厂设置
- 3d地球app
- CUDA学习笔记(1)数组求和
- 关于NI CompactRIO自定义模块中FPGA与Labview FPGA中编程的一点理解
- 在屏幕上用"*"输出菱形图案
- 做(do) (贪心模拟 dp)
- 从《模仿游戏》认识图灵