Mybatis的简单联合查询操作:

实体类:

Employee:

package com.test.mybatis;

public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
private Depart depart; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Depart getDepart() {
return depart;
}
public void setDepart(Depart depart) {
this.depart = depart;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email="
+ email + ", gender=" + gender + ", depart=" + depart + "]";
} }

Depart:

package com.test.mybatis;

public class Depart {
private int id;
private String departName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDepartName() {
return departName;
}
public void setDepartName(String departName) {
this.departName = departName;
}
@Override
public String toString() {
return "Depart [id=" + id + ", departName=" + departName + "]";
} }

mybatis-config.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到 mybatis-config.xml中-->
<mappers>
<mapper resource="EmployeeMapper.xml"/>
</mappers>
</configuration>

EmployeeMapper接口:

package com.test.mybatis.dao;

import com.test.mybatis.Employee;

public interface EmployeeMapper {

    public Employee getEmpAndDept(Integer id);
}

EmployeeMapper.xml:

对接口进行实现;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mtbatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.mybatis.dao.EmployeeMapper">
<!--
     方法一:
联合查询:级联属性封装结果集
-->
<resultMap type="com.test.mybatis.Employee" id="MyEmp">
<!--
指定主键列的封装规则
id定义主键会底层优化
column:指定哪一列
property:指定对应的JavaBean
-->
<id column="id" property="id"/>
<!-- 普通列封装 -->
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<result column="did" property="depart.id"/>
<result column="dept_name" property="depart.departName"/>
</resultMap>
<!--
    方法二:
使用association定义单个对象的封装规则
-->
<resultMap type="com.test.mybatis.Employee" id="MyEmp2">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<!--
association可以指定联合的Javabean对象
property:指定那个属性是联合的对象
JavaType:指定这个属性对象的类型(不能省略)
-->
<association property="depart" javaType="com.test.mybatis.Depart">
<id column="did" property="id"/>
<id column="dept_name" property="departName"/>
</association>
</resultMap>
<!--
查询Employee的同时查询员工对应的部门
-->
<select id="getEmpAndDept" resultMap="MyEmp2">
SELECT e.id id,e.last_name last_name,e.email email,e.gender gender,d.id did,d.dept_name dept_name
from
tbl_depat d,tbl_employee e
where d.id=e.d_id AND e.id=#{id}
</select>
</mapper>

测试类:

package com.test.mybatis.test;

import java.io.IOException;
import java.io.InputStream; 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 org.junit.Test; import com.test.mybatis.Employee;
import com.test.mybatis.dao.EmployeeMapper; public class MyBatisTest {
//读取配置文件,得到SQLSessionFactory
public SqlSessionFactory getSqlSessionFactory() throws IOException{
String resource="mybatis-config.xml";
InputStream is=Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(is);
}
@Test
public void test01() throws IOException{
//获取sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//获取sqlSession
//代表和数据库的一次对话,用完必须关闭,相当于connection,都是非线程安全。每次使用都应该获取新的对象
SqlSession openSession = sqlSessionFactory.openSession();
try{
//获取接口的实现类对象
//会为接口自动的创建一个代理对象,代理对象去执行增删查改方法
//mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象
//(将接口和xml进行绑定)
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee emp = mapper.getEmpAndDept(6);
System.out.println(emp+":"+emp.getDepart()); }finally{
openSession.close();
}
} }

Mybatis联合查询(一)的更多相关文章

  1. MyBatis联合查询和使用association 进行分步式查询

    查询Emp的同时,查出emp对应的部门Department 方法1:联合查询,使用级联属性封装结果集 <!-- 联合查询,使用级联属性封装结果集 type:要自定义规则的javaBean类型 i ...

  2. Mybatis 联合查询XML与注解对比

    由于是练习,故只做了感兴趣的一部分测试. 测试类容XML配置转注解方式 实体类为了测试请忽略设计是否合理… User.java @Alias("User")public class ...

  3. mybatis联合查询

    1.有学生实体 @Component @Scope("prototype") public class StudentInfo { private Integer studentI ...

  4. MyBatis联合查询association使用

    1.需求 两张表 channels(频道表)  member(会员表) 频道表里面有会员id,查询频道列表的时候需要关联查询出会员的名称,头像等信息 . 2.channels.xml定义,配置主要在这 ...

  5. Mybatis联合查询记录,左连接参数操作

    公司业务需求要做个列表的排序 而实际排序的字段不再本库中,需要跨库去拿到字段,因为是微服务体系架构,不可能Left join跨库的表,所以决定调用一次跨服务的API拿到排序相关的对象,里面包含需要排序 ...

  6. MyBatis 多表联合查询及优化 以及自定义返回结果集

    下面就来说一下 mybatis 是通过什么来实现多表联合查询的.首先看一下表关系,如图: 这 里,我已经搭好了开发的环境,用到的是 SpringMVC + Spring + MyBatis,当然,为了 ...

  7. MyBatis之三:多表联合查询

    在这篇文章里面主要讲解如何在mybatis里面使用一对一.一对多.多表联合查询(类似视图)操作的例子. 注:阅读本文前请先大概看一下之前两篇文章. 一.表结构 班级表class,学生表student, ...

  8. Mybatis.net与MVC入门配置及联合查询动态SQL拼接和简单事务

    第一次学习Mybatis.net,在博客园也找到好多资料,但是在配置成功之后也遇到了一些问题,尤其是在动态SQl拼接时候,这里把遇到的问题还有自己写的一个Demo贴出来,希望能帮到新手,有不适合的地方 ...

  9. MyBatis 多表联合查询,字段重复的解决方法

    MyBatis 多表联合查询,两张表中字段重复时,在配置文件中,sql语句联合查询时使用字段别名,resultMap中对应的column属性使用相应的别名: <resultMap type=&q ...

随机推荐

  1. Android RecyclerView的补充。

    明天写吧.. 今天写,然后再写今天的内容,虽然结课了,我们还是得学习,所以如果我学习了一些知识,不出意外每天会持续更新的. RecyclerView其实是可以完全代替ListView的存在, 但是为啥 ...

  2. 铁大树洞与市面上现有APP对比

    写在前面 铁大树洞这款APP严格来说并没有可以参照的对象,但如果非要说的话也可以有.这里我们选取百度贴吧进行对比. 百度贴吧 可以看到,百度贴吧的贴吧首页排版要更加好看,且在首页添加了各种分类.也许我 ...

  3. U盘数据泄露,用不到30行的Python代码就能盗走

    今天跟大家分享下一段简单的代码,希望能给经常用U盘的人警戒,提高信息安全意识. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做 ...

  4. Web接口测试理论知识分享

    首先谈下接口的定义分为2类,程序接口和协议接口 1.程序模块接口,具体到程序中就是提供了输入输出的类 方法,我们可以通过传入不同的参数,来验证程序接口的功能 2.协议接口  比如HTTP/SOAP协议 ...

  5. SSH安全端口

    端口安全指的是尽量避免服务器的远程连接端口被不法分子知道,为此而改变默认服务端口号的操作 如何改变SSH服务端口 修改 /etc/ssh/sshd_config 配置 配置修改后执行命令 servic ...

  6. 在阿里云托管kubernetes上利用 cert-manager 自动签发 TLS 证书[无坑版]

    前言 排错的过程是痛苦的也是有趣的. 运维乃至IT,排错能力是拉开人与人之间的重要差距. 本篇会记录我的排错之旅. 由来 现如今我司所有业务都运行在阿里云托管kubernetes环境上,因为前端需要对 ...

  7. PowerJob 的自实现高可用方案,妙妙妙!

    本文适合有 Java 基础知识的人群 作者:HelloGitHub-Salieri HelloGitHub 推出的<讲解开源项目>系列. 碎碎念 高可用放到今天已经不是一个新颖的词汇了,怎 ...

  8. 性能分析(6)- 如何迅速分析出系统 CPU 的瓶颈在哪里

    性能分析小案例系列,可以通过下面链接查看哦 https://www.cnblogs.com/poloyy/category/1814570.html 前言 在做性能测试时,我们会需要对 Linux 系 ...

  9. jQuery的事件与 动画

    什么是事件: 事件的本质是委托. Jquery的 方法: $().css(); $().click(); 等等. 鼠标的事件: 区别在于:mouseover与mouseout再进入或离开后会执行这两个 ...

  10. Javascript之其实我觉得原型链没有难的那么夸张!

    原型链.闭包.事件循环等,可以说是js中比较复杂的知识了,复杂的不是因为它的概念,而是因为它们本身都涉及到很多的知识体系.所以很难串联起来,有一个完整的思路.我最近想把js中有点意思的知识都总结整理一 ...