Spring整合Mybatis之关联查询示例

目录结构:

Dept表:

employee表:

pojo包

Dept.java(实体类):

package com.xmm.springboot_lab.pojo;import java.util.List;public class Dept {private int id;private String name;private List<Employee> employees;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public List<Employee> getEmployees() {return employees;}public void setEmployees(List<Employee> employees) {this.employees = employees;}@Overridepublic String toString() {return "Dept{" +"id=" + id +", name='" + name + '\'' +", employees=" + employees +'}';}
}

Employee.java(实体类)

package com.xmm.springboot_lab.pojo;import org.springframework.stereotype.Repository;import java.sql.Date;public class Employee {private  String id;private String name;private Date hireDate;private float salary;private int deptId;private Dept dept;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getHireDate() {return hireDate;}public void setHireDate(Date hireDate) {this.hireDate = hireDate;}public float getSalary() {return salary;}public void setSalary(float salary) {this.salary = salary;}public int getDeptId() {return deptId;}public void setDeptId(int deptId) {this.deptId = deptId;}public Dept getDept() {return dept;}public void setDept(Dept dept) {this.dept = dept;}@Overridepublic String toString() {return "Employee{" +"id='" + id + '\'' +", name='" + name + '\'' +", hireDate=" + hireDate +", salary=" + salary +", deptId=" + deptId +", dept=" + dept +'}';}
}

Dao包

DeptDao.java

package com.xmm.springboot_lab.dao;import com.xmm.springboot_lab.pojo.Dept;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;@Repository
public interface DeptDao {public List<Dept> getDeptWithEmployees(); //主要看这个public List<Dept> getDepts();public int addDept(Dept dept);
}

DeptMapper.xml

<?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.xmm.springboot_lab.dao.DeptDao"><insert id="addDept" parameterType="Dept">insert into dept (deptname) values(#{name});</insert><!-- ===一对多查询(一个部门有多个员工),这里使用的是左外连接==== --><resultMap type="Dept" id="deptWithEmployeesMap"><id property="id" column="D_ID" /><result property="name" column="D_NAME" /><collection property="employees" javaType="java.util.ArrayList"ofType="Employee"><id property="id" column="E_ID" /><result property="name" column="E_NAME" /><result property="salary" column="E_SALARY" /></collection></resultMap><select id="getDeptWithEmployees" resultMap="deptWithEmployeesMap">select D.deptnoD_ID,D.deptname D_NAME, E.empno E_ID,E.empname E_NAME,E.salary E_SALARYfrom Dept  Dleft join employee E on D.deptno = E.deptno;</select><!-- ====只获取部门信息 --><resultMap id="deptMap" type="Dept"><id property="id" column="deptno" /><result property="name" column="deptname" /></resultMap><select id="getDepts" resultMap="deptMap">select * from dept;</select></mapper>

service包

package com.xmm.springboot_lab.service.impl;import com.xmm.springboot_lab.dao.DeptDao;
import com.xmm.springboot_lab.pojo.Dept;
import com.xmm.springboot_lab.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class DeptServiceImpl implements DeptService {@Autowiredprivate DeptDao deptDao;@Overridepublic List<Dept> getDeptWithEmployees() {  //主要看这个return deptDao.getDeptWithEmployees();}@Overridepublic List<Dept> getDepts() {return deptDao.getDepts();}@Overridepublic int addDept(Dept dept) {return deptDao.addDept(dept);}
}

Junit测试

package com.xmm.springboot_lab.service.impl;import com.xmm.springboot_lab.pojo.Dept;
import com.xmm.springboot_lab.service.DeptService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class DeptServiceImplTest {@Autowiredprivate DeptService deptService;//    @Test
//    public void addDeptTest(){//        Dept dept = new Dept();
//        dept.setName("行政部");
//        int r = deptService.addDept(dept);
//        System.out.println("受影响行数:" + r);
//    }@Testpublic void getDeptWithEmployeesTest(){  //T1List<Dept>  depts = deptService.getDeptWithEmployees();for (Dept d :depts){System.out.println(d);}}@Testpublic void getDeptsTest(){ //T2List<Dept>  depts = deptService.getDepts();for (Dept d :depts){System.out.println(d);}}
}

测试结果

T1结果:

Dept{id=1, name='财务部', employees=[Employee{id='1', name='李四', hireDate=null, salary=4000.0, deptId=0, dept=null}, Employee{id='2', name='张三', hireDate=null, salary=12000.0, deptId=0, dept=null}, Employee{id='3', name='赵六', hireDate=null, salary=7000.0, deptId=0, dept=null}]}
Dept{id=2, name='人事部', employees=[Employee{id='4', name='刘洪', hireDate=null, salary=5000.0, deptId=0, dept=null}]}
Dept{id=3, name='行政部', employees=[]}
Dept{id=4, name='大娃', employees=[]}

T2结果:

Dept{id=1, name='财务部', employees=null}
Dept{id=2, name='人事部', employees=null}
Dept{id=3, name='行政部', employees=null}
Dept{id=4, name='大娃', employees=null}

Spring整合Mybatis之关联查询示例相关推荐

  1. spring整合mybatis(实现数据的增删改查)

    一.专业术语解释 1.spring:是分层的Java SE/EE应用full - stack轻量级开源框架,以IoC(控制反转)和AOP(面向切面编程)为内核,提供展现层spring MVC 和 sp ...

  2. Spring整合Mybatis之注解方式,(注解整合Junit)

    Spring整合Mybatis之注解方式 我有一篇博客详细写了我自己使用xml的方法Spring整合MyBatis,现在我就把核心配置文件中的每个bean的配置使用注解的方式实现 注解整合MyBati ...

  3. Spring学习笔记:Spring整合Mybatis(mybatis-spring.jar)(二:mybatis整合spring)

    http://blog.csdn.net/qq598535550/article/details/51703190 二.Spring整合mybatis其实是在mybatis的基础上实现Spring框架 ...

  4. SSM之二(Spring整合Mybatis)

    项目与外界交互大概过程如下图: 一般过程是: 前端发送请求,查询数据.增加数据.修改数据.删除数据 中间件经过处理后,对数据发送请求 数据库返回数据,中间件再对数据处理 中间件响应前端请求 上一节关注 ...

  5. Spring整合mybatis中的sqlSession是如何做到线程隔离的?

    转载自  Spring整合mybatis中的sqlSession是如何做到线程隔离的? 项目中常常使用mybatis配合spring进行数据库操作,但是我们知道,数据的操作是要求做到线程安全的,而且按 ...

  6. Spring整合Mybatis和JUnit

    Spring整合Mybatis: 注解整合MyBatis分析 业务类使用注解形式声明bean,属性采用注解注入 建立独立的配置管理类,分类管理外部资源,根据功能进行分类,并提供对应的方法获取bean ...

  7. Spring整合MyBatis导致一级缓存失效问题

    熟悉MyBatis的小伙伴都知道MyBatis默认开启一级缓存,当我们执行一条查询语句后,MyBatis会以我们查询的信息生成一个缓存key,查询的结果为value,存到一个map中,即存入一级缓存. ...

  8. Spring整合Mybatis之DAO层、Service层开发

    3. Spring整合Mybatis编程DAO层开发 1. 项目引入相关依赖spring mybatis mysql mybatis-spring druid2. 编写spring.xml整合:spr ...

  9. Spring源码深度解析(郝佳)-学习-源码解析-Spring整合MyBatis

    了解了MyBatis的单独使用过程之后,我们再来看看它也Spring整合的使用方式,比对之前的示例来找出Spring究竟为我们做了什么操作,哪些操作简化了程序开发. 准备spring71.xml &l ...

最新文章

  1. Apache Maven 入门篇
  2. CentOS7 配置ISCSI targetcli 共享存储
  3. Linux的Nginx五:进程|过程
  4. 简单说明PHP的垃圾收集机制是怎样的?
  5. Integer的==问题
  6. LINQ系列:Linq to Object排序操作符
  7. zkcli远程连接_ZooKeeper客户端 zkCli.sh 节点的增删改查
  8. OmniGraffler软件和激活码
  9. 编辑器、编译器和IDE的区别
  10. Air722UG_模块硬件设计手册_V1.1
  11. 时间序列分析思维导图
  12. 新宝美股三大指数集体高开
  13. 笔记本电脑上如何创建html,笔记本怎么创建wifi_笔记本电脑怎么创建无线网络-win7之家...
  14. 马化腾致信合作伙伴:灰度法则的七个维度
  15. 小技巧给网页减肥 让网站访问提速
  16. 计算机自杀式软件--USBKill
  17. 打印正三角形与倒三角形(C++)
  18. 数据库学习之(5)详解DBMS
  19. Oracle表空间及数据文件操作
  20. 【机器学习算法】支持向量机(support Vector Machine,SVM)

热门文章

  1. tips:Java基本数据类型大小比较
  2. Spring集成PageHelper的简单用法
  3. HDU 5691 ——Sitting in Line——————【状压动规】
  4. Hibernate 多表关联
  5. WP8 中使用HTML Agility Pack与友盟分享SDK遇到的 System.Xml.XPath加载问题
  6. [jQuery基础] jQuery事件相关
  7. 用jquery模仿的a的title属性
  8. 7-10 A-B (20 分)
  9. 皖西学院计算机科学与技术分数线,2017年皖西学院艺术类本科专业录取分数线...
  10. linux+应用程序高级编程,linux-----shell高级编程----grep应用