Mybatis复杂查询环境
多对一处理
导入依赖
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.17</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes><filtering>true</filtering></resource></resources></build>
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.10</version></dependency>
pojo层
import lombok.Data;@Data
public class Student {private int id;private String name;//学生关联一个老师private Teacher teacher;}
import lombok.Data;@Data
public class Teacher {private int id;private String name;}
dao层
public interface StudentMapper {//查询所有的学生信息以及对应的老师信息public List<Student> getStudent();
}
public interface TeacherMapper {@Select("select * from teacher where id = #{tid}")Teacher getTeacher(@Param("tid") int id);
}
MybatisUtils
public class MybatisUtils {private static SqlSessionFactory sqlSessionFactory;static{//使用mybatis第一步:获取sqlSessionFactory对象try {String resource="mybatis-config.xml";InputStream inputStream = null;inputStream = Resources.getResourceAsStream(resource);sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();}}//有了sqlSeesionFactory之后,就可以从factory中获取sqlSession实例了//sqlSession完全包含了面向数据库执行SQL命令所需的所有方法public static SqlSession getSqlSession(){return sqlSessionFactory.openSession();}
}
方法一(按照查询嵌套处理)
StudentMapper.xml
StudentMapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhbit.dao.StudentMapper"><!-- 方法一:按照查询嵌套处理1.查询所有的学生信息2.根据查询出来的学生的tid,寻找对应的老师 子查询--><select id="getStudent" resultMap="StudentTeacher" >select * from student</select><resultMap id="StudentTeacher" type="Student"><result property="id" column="id"/><result property="name" column="name"/><!--复杂的属性,需要单独处理对象使用:association集合:collection --><association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/></resultMap><select id="getTeacher" resultType="com.zhbit.pojo.Teacher">select * from teacher where id = #{id}</select>
</mapper>
TeacherMapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhbit.dao.TeacherMapper"></mapper>
测试类
public class MyTest {@Testpublic void testStudent(){try(SqlSession sqlSession = MybatisUtils.getSqlSession()){StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);List<Student> studentList = mapper.getStudent();for (Student student : studentList) {System.out.println(student);}}}
}
测试结果
方法二(按照结果嵌套查询)
StudentMapper.xml
<select id="getStudent2" resultMap="StudentTeacher2">select s.id as sid,s.name as sname,t.name as tname,s.tidfrom student s,teacher twhere s.tid = t.id;</select><resultMap id="StudentTeacher2" type="student"><result property="id" column="sid"/><result property="name" column="sname"/><association property="teacher" column="tname" javaType="teacher"><result property="id" column="tid"/><result property="name" column="tname"/></association></resultMap><!--方法一:按照查询嵌套处理1.查询所有的学生信息2.根据查询出来的学生的tid,寻找对应的老师 子查询--><!--<select id="getStudent" resultMap="StudentTeacher" >select * from student</select><resultMap id="StudentTeacher" type="Student"><result property="id" column="id"/><result property="name" column="name"/><!–复杂的属性,需要单独处理对象使用:association集合:collection –><association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/></resultMap><select id="getTeacher" resultType="com.zhbit.pojo.Teacher">select * from teacher where id = #{id}</select>-->
</mapper>
测试类
public class MyTest {/*@Testpublic void getTeacher(){try(SqlSession sqlSession = MybatisUtils.getSqlSession()){TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);Teacher teacher = mapper.getTeacher(1);System.out.println(teacher);}}@Testpublic void testStudent(){try(SqlSession sqlSession = MybatisUtils.getSqlSession()){StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);List<Student> studentList = mapper.getStudent();for (Student student : studentList) {System.out.println(student);}}}*/@Testpublic void testStudent2(){try(SqlSession sqlSession = MybatisUtils.getSqlSession()){StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);List<Student> studentList = mapper.getStudent2();for (Student student : studentList) {System.out.println(student);}}}
}
测试结果
多对多处理
Mybatis复杂查询环境相关推荐
- mybatis复杂查询环境 多对一的处理 按照结果嵌套处理和按照查询嵌套处理
sql插入表student的语句: insert into student (id, name, tid) values ('9', '梁梁','2'); insert into student (i ...
- Mybatis入门:1(Mybatis框架的环境搭建)
Mybatis框架的环境搭建 一.创建maven工程并导入坐标 导入坐标: <dependencies><dependency><groupId>org.mybat ...
- MyBatis之查询缓存
MyBatis之查询缓存 2017/09/30 正如大多数持久层框架一样,MyBatis同样也提供了对查询数据的缓存支持.今后我们要学习的SpringMVC框架属于系统控制层,它也有它的缓存区域,对响 ...
- 2.4.3 Mybatis 高级查询, 复杂映射, 返回主键, 动态SQL if, set, foreach, 核心配置文件深入,plugins标签, 多表查询, 嵌套查询
目录 Mybatis 复杂映射&配置文件深入 一 Mybatis高级查询 1.1 ResutlMap属性 1.2 多条件查询(三种) 1.3 模糊查询 二 Mybatis映射文件深入 2.1 ...
- MyBatis关联查询、多条件查询
MyBatis关联查询.多条件查询 1.一对一查询 任务需求; 根据班级的信息查询出教师的相关信息 1.数据库表的设计 班级表: 教师表: 2.实体类的设计 班级表: public class Cla ...
- Mybatis联合查询
为什么80%的码农都做不了架构师?>>> 案例 一个博客系统中,用户可以任意发表博文(Post),用户还可以对博文进行评论(Comment).于是在这个系统中,存在以下的关系: ...
- MyBatis基本运行环境
MyBatis基本运行环境 1. 创建项目 2.拷贝jar加入到项目中build path jar包 3.创建数据库的表及数据添加 USE [mybatis] CREATE TABLE [dbo].[ ...
- Mybatis like查询的写法--转载
原文地址:http://lavasoft.blog.51cto.com/62575/1386870 Mybatis like查询官方文档没有明确的例子可循,网上搜索了很多,都不正确. Mybatis ...
- asp.net linq查询环境搭建
本文是以sqlserver2008为数据库,vs2013为开发工具来介绍的. 要搭建这样一个数据库的操作环境,首先建立一个类库项目 然后在这个类库项目中添加几个类:DBDataContext数据库上下 ...
最新文章
- 006_JSONObject对象公共方法
- Python学习笔记:常用内建模块4:hmac
- python的函数结构_Python学习(四)常见函数及控制结构
- skywalking使用方法_skywalking 6.2配置相关和使用
- Delphi clientdataset的详细介绍
- Could not resolve placeholder 'jdbc.url' in value ${jdbc.url}
- 请拆招:将两个已排序集合分解成两个独立部分的集合和一个共有部分的集合?...
- js array 删除指定元素_Array 原型方法源码实现解密
- xp访问服务器显示没有权限,xp无法访问2008R2共享
- Tomcat不能自动编译JSP文件问题的一种解决方法
- 阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第3节 线程同步机制_4_解决线程安全问题_同步代码块...
- 大数据,云计算,物联网和移动互联网与传统互联网,主要有什么关系?
- linux版qq怎么传文件,QQ for linux终于能在线传送文件了~
- iso文件:抱歉,装载文件时出现问题
- java.lang.ClassNotFoundException:org.glassfish.gmbal.ManagedObjectManager
- python读excel中数据画图_python读取excel数据并且画图的实现示例
- Java 汉字转换为拼音字符串
- VUE图片裁剪组件,基于VueCropper
- 英语四级考前核心词汇【1】
- 微软搜索服务器,微软 Win10 搜索 UI 界面测试天气磁贴