原文:https://blog.csdn.net/h993438890/article/details/89146483

spring boot项目的创建省略
创建两张表
t_user 字段 主键id,username(varchar) , pwd(varchar) ,did(外键)

t_dept 字段 主键id,dname(varchar)

建立两个实体类User ,Dept。提供相应的getter和setter方法

public class User {
private Integer id;
private String username;
private String pwd;
//多对一
private Dept dept;
//此处省略getter/setter方法
}
public class Dept {
private Long id;
private String dname;
//...此处省略getter/setter方法
}

第一种:嵌套结果查询

在usermapper.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springboot.mapper.UserMapper">
<resultMap id="queAllMap" type="com.example.springboot.domain.User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="pwd" column="pwd"/>
<association property="dept" column="did" javaType="com.example.springboot.domain.Dept">
<id property="id" column="d_id"/><!--根据sql查询出列,这里为了和t_user表的主键id区分,取了别名,如果不区分,结果会出错-->
<result property="dname" column="dname"/>
</association>
</resultMap>
<select id="queAll" resultMap="queAllMap">
select u.*,d.id d_id ,d.dname from t_user u left join t_dept d on u.did=d.id
</select>
</mapper>

在对应的UserMapper.java中代码

@Mapper
public interface UserMapper {
//@Select("select * from t_user")
List<User> queAll();
}

第二种:嵌套查询

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springboot.mapper.UserMapper">
<select id="queAll" resultMap="queAllMap">
select u.*,d.id ,d.dname from t_user u left join t_dept d on u.did=d.id
</select>
<resultMap id="queAllMap" type="com.example.springboot.domain.User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="pwd" column="pwd"/>
<association property="dept" column="did" select="getDeptById"/>
</resultMap>
<select id="getDeptById" parameterType="Long" resultType="com.example.springboot.domain.Dept">
select * from t_dept where id=#{id}
</select>
</mapper>

还要在DeptMapper.java中添加一个方法

@Mapper
public interface DeptMapper {
@Select("select * from t_dept where id=#{id}")
Dept getDeptById(Long id);
}

对assacation标签的属性进行解释一下

  •   property      对象属性的名称
  •   javaType     对象属性的类型
  •   column        所对应的外键字段名称
  •   select          使用另一个查询封装的结果

最新文章

  1. CxImage在VS2010下的配置
  2. aspxshell下突破无可写可执行目录执行cmd
  3. EasyCriteria 3.0 发布
  4. Android线程管理(三)&mdash;&mdash;Thread类的内部原理、休眠及唤醒
  5. iOS多线程-01-pthread与NSTread
  6. web工程中URL地址的写法
  7. nyist 61 传纸条 nyist 712 探 寻 宝 藏(双线程dp问题)
  8. Jquery中$.ajax()方法参数详解(转)
  9. 64位系统访问注册表SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
  10. SpringMVC基础入门,创建一个HelloWorld程序
  11. dedecms下的tplcache模板缓存文件过多怎么清理?
  12. Revit二次钢筋
  13. MySQL之ORDER BY 详细解析
  14. Datatable数据转换成excel导出时 数值类型在EXCEL中为文本形式 无法进行统计
  15. IP路由原理
  16. insmod 时报错“Unknown symbol”问题的解决
  17. 特殊权限set_uid
  18. [Math]理解卡尔曼滤波器 (Understanding Kalman Filter)
  19. JS利用正则配合replace()函数替换指定字符
  20. JDK1.9-新特性

热门文章

  1. 微信小程序组件——bindtap和catchtap的区别
  2. 主攻互动娱乐和视频自媒体,新浪SHOW是不是桩好生意?
  3. 【NS2】Installing ns-2.29 in Ubuntu 12.04
  4. CODE FESTIVAL 2017 qual B B - Problem Set【水题,stl map】
  5. vue单页面项目返回上一页无效,链接变化了,但是页面没有变化
  6. maven修改版本号
  7. Gym - 101480A_ASCII Addition
  8. Java练习 SDUT-1255_小明A+B
  9. HDFS Concepts-blocks
  10. uda 4.C++面向对象编程