Mybatis查询
发布日期:2021-11-14 22:18:51 浏览次数:9 分类:技术文章

本文共 6831 字,大约阅读时间需要 22 分钟。

Mybatis中关联查询之子查询

Mybatis中通过关联查询,基础的有两种方式,第一种是直接根据条件查询,第二种是在第一个查询下进行子查询。

第一种方式:嵌套结果

通过这种方式查询实际上就是简单的条件查询,下面是测试方法和测试结果:

```@Test    public void testClasses(){        SqlSession session = MybatisUtils.getSession();        String statement = "com.mybatis.bean.orderMapper.getClasses";        Classes classes = session.selectOne(statement,2);        System.out.println(classes);        session.close();    }
2016-12-10 19:11:41,626 [main] DEBUG [com.mybatis.bean.orderMapper.getClasses] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@49393f9]2016-12-10 19:11:41,627 [main] DEBUG [com.mybatis.bean.orderMapper.getClasses] - ==>  Preparing: select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=? 2016-12-10 19:11:41,721 [main] DEBUG [com.mybatis.bean.orderMapper.getClasses] - ==> Parameters: 2(Integer)Classes [id=2, name=bj_b, teacher=Teacher [id=2, name=LS2]]

可以看出能够得到正确的结果

第二种方式:嵌套查询

通过这种方式运行可能会报下面类似的错误

org.apache.ibatis.exceptions.PersistenceException: ### Error querying database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.mybatis.test.orderMapper.getClass4### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.mybatis.test.orderMapper.getClass4    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:104)    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:95)    at com.mybatis.test.Test6.test(Test6.java:17)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:606)    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.mybatis.test.orderMapper.getClass4    at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:672)    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:507)    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:500)    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:100)    ... 25 more

**上面之所以会报错,首先做以下检查:

1、Xxxmapper.xml中你写的namespace可能不正确 或者 Xxxmapper.xml文件名和所写的mapper名称不相同(一般是这种错误)
2、Xxxmapper.xml没有在你写的配置文件中注册(当你是一个一个Mapper注册而不是扫描包)**


下面给出一个查询三个表的一个子查询例子(当然也可以通过方式一实现,而且更加简单,只有一条sql语句,此处不再赘述,懂原理的朋友相信能够举一反三,不懂得可以交流探讨^_^):

下面是测试及结果

package com.mybatis.test;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import com.mybatis.bean.Classes;import com.mybatis.utils.MybatisUtils;public class Test6 {    @Test    public void test() {        SqlSession session = MybatisUtils.getSession();        String statement = "com.mybatis.test.test6Mapper.getClass4";        List
list = session.selectList(statement, 1); for (Object arg : list) { System.out.println(arg); } }}
2016-12-11 13:11:04,385 [main] DEBUG [com.mybatis.test.test6Mapper.getClass4] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@2eae85ab]2016-12-11 13:11:04,386 [main] DEBUG [com.mybatis.test.test6Mapper.getClass4] - ==>  Preparing: select * from class where c_id=? 2016-12-11 13:11:04,471 [main] DEBUG [com.mybatis.test.test6Mapper.getClass4] - ==> Parameters: 1(Integer)2016-12-11 13:11:04,512 [main] DEBUG [com.mybatis.test.test6Mapper.getTeacher2] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@2eae85ab]2016-12-11 13:11:04,513 [main] DEBUG [com.mybatis.test.test6Mapper.getTeacher2] - ==>  Preparing: SELECT t_id id, t_name name FROM teacher WHERE t_id=? 2016-12-11 13:11:04,513 [main] DEBUG [com.mybatis.test.test6Mapper.getTeacher2] - ==> Parameters: 1(Integer)2016-12-11 13:11:04,515 [main] DEBUG [com.mybatis.test.test6Mapper.getStudent] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@2eae85ab]2016-12-11 13:11:04,515 [main] DEBUG [com.mybatis.test.test6Mapper.getStudent] - ==>  Preparing: SELECT s_id id, s_name name FROM student WHERE class_id=? 2016-12-11 13:11:04,516 [main] DEBUG [com.mybatis.test.test6Mapper.getStudent] - ==> Parameters: 1(Integer)Classes [id=1, name=bj_a, teacher=Teacher [id=1, name=LS1], students=[Student [id=1, name=xs_A], Student [id=2, name=xs_B], Student [id=3, name=xs_C]]]

转载地址:https://blog.csdn.net/qq_18538781/article/details/53560396 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Mybatis之动态SQL 模糊查询
下一篇:求两条直线间的夹角

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年03月02日 04时07分28秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

共享内存 java_java - Java客户端-服务器编程:客户端之间的共享内存 - 堆栈内存溢出... 2019-04-21
java网络编程期末试题_java网络编程面试题【其中一小部分】 2019-04-21
estore java_estore2 - WEB源码|JSP源码/Java|源代码 - 源码中国 2019-04-21
java如何做表单校验_微信小程序实现表单校验功能 2019-04-21
matlab dwt2(),MATLAB小波变换指令及其功能介绍(超级有用) 2019-04-21
php sequelize,egg.js整合数据库ORM框架Sequelize 2019-04-21
php同时打开2个数据库,thinkphp3.2同时连接两个数据库的简单方法 2019-04-21
centos 开发php扩展,centos 安装php扩展redis 2019-04-21
php+跑buth,php 中函数获取可变参数的方法, 这个语法有点像 golang 语言中的 2019-04-21
cms 单点登录 php,Yii2 中实现单点登录的方法 2019-04-21
oracle自己运行,创建Oracle自动执行Job 2019-04-21
oracle报错00020,oracle启动 ORA-00020: maximum number of processes (%s) exceeded错误 2019-04-21
chmod 赋权所有_chmod 权限 命令详细用法 2019-04-21
html代码翻译_[译]您知道 HTML 的键盘标签吗? 2019-04-21
html抽奖代码_JavaScript高手之路:封装抽奖效果 2019-04-21
hadoop 3.3 一直停留在running wordcount_蛋价持续下跌,今日跌破3.3元大关!深秋季节价格还能反弹吗?... 2019-04-21
的流程图做完后如何保存_2019超火的半永久眉是哪款?做完后我们如何护理?... 2019-04-21
去除logo 高德地图api_深圳品牌logo升级如何保持原型的同时更具创新? 2019-04-21
二重积分转换成极坐标_二重积分转换极坐标r的范围如何确定? 2019-04-21
python中倒背如流_八字基础知识--倒背如流篇 2019-04-21