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. CSS可见格式化模型

    1.盒模型 1.1 盒子大小 盒模型描述了元素如何显示,以及如何相互作用.相互影响. 页面中的所有元素都被看作一个矩形盒子,这个盒子包含元素的内容.内边距.边框和外边距. 给元素应用的背景会作用于元素 ...

  2. 8 Java 条件逻辑语句

    生活中,我们经常需要先做判断,然后才决定是否要做某件事情.例如,在上学的时候,如果期末考试成绩在全校能拿到前100名,则奖励一个 iPhone 11 .对于这种“需要先判断条件,条件满足后才执行的情况 ...

  3. SpringBoot常用配置,引入外部配置文件信息,热加载

    SpringBoot的配置文件格式 yml规范 SpringBoot的配置文件支持properties和yml,甚至还支持json. 更推荐使用yml文件格式: yml文件,会根据换行和缩进帮助咱们管 ...

  4. BN系列-未完待续

    BN.LN.IN.GN Cross-Iteration Batch Normalization 因为有时候我们的计算能力有限,所以BN设置的比较小,这样BN效果就比较差. 因此我们将最近几次的batc ...

  5. ROS 八叉树地图构建 - 给 octomap_server 增加半径滤波器!

    为了在每帧点云中滤除噪声点,选择了半径滤波器,也用高斯滤波器测试过,但是没有半径效果好,这里记录下在 octomap_server 中增加半径滤波器的步骤,并在 launch 中配置滤波器参数. 一. ...

  6. 同事不太懂负载均衡,我直接把阿里架构师的这份Nginx笔记甩给他

    Nginx功能强大,架构复杂,学习.维护和开发的门槛较高. 本份笔记深入最新的Nginx源码,详细剖析了模块体系.动态插件.功能框架.进程模型.事件驱动.线程池.TCP/UDP/HTTP 处理等Ngi ...

  7. GUAVA-cache实现

    GUAVA  Cache Guava Cache与ConcurrentMap很相似基于分段锁及线程安全,但也不完全一样.最基本的区别是ConcurrentMap会一直保存所有添加的元素,直到显式地移除 ...

  8. 【算法•日更•第五十期】二分图(km算法)

    ▎前言 戳开这个链接看看,惊不惊喜,意不意外?传送门. 没想到我的博客竟然被别人据为己有了,还没办法投诉. 这年头写个博客太难了~~~ 之前小编写过了二分图的一些基础知识和匈牙利算法,今天来讲一讲km ...

  9. 【特别篇】不为人知的U盘秘密

    U盘是我们代码爱好者的必要东西,方便于我们更好的拷文件,使用一些已经配置好的东西,比如说:小编经常会将linux系统放进去,平时就可以随时用了. But 你的U盘真的正常吗?你了解多少? 关于U盘   ...

  10. es数据库基本操作

    1.es建立索引: curl -XPUT 'http://10.xx.xx.xx:9200/索引名称' 2.es查询所有索引: curl -XGET 'http://10.xx.xx.xx:9200/ ...