xml说明:

<!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性-->
<resultMap id="唯一的标识" type="映射的pojo对象">
<id column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型" property="映射pojo对象的主键属性" />
<result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/>
<association property="pojo的一个对象属性" javaType="pojo关联的pojo对象">
<id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo对象的主席属性"/>
<result column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/>
</association>
<!-- 集合中的property须为oftype定义的pojo对象的属性-->
<collection property="pojo的集合属性" ofType="集合中的pojo对象">
<id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" />
<result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" />
</collection>
</resultMap>

1.根据查询进行嵌套

1.创建实体类。编写映射文件,编写多表查询语句,例如:

//------------映射文件------------//

<?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">
<!-- 命名空间:防止sql语句的id重名
格式:包名+类名/包名+mapper文件名
parameterType:sql语句传参类型
resultType:返回结果类型
useGeneratedKeys:使用自增主键
-->
<mapper namespace="cn.aaa.entity.student.mapper">
<select id="getStudent" resultMap="StudentTeacher">
select s.id sid,s.name sname,t.id tid,t.name tname from students s,teacher t where s.id=t.id
</select> <!-- 查询结果是student,column也可以填写查询出来的表的别名 -->
<resultMap id="StudentTeacher" type="Student">
<id column="sid" property="id" />
<result column="sname" property="name"/>
<!-- 关联对象property 关联对象在student在实体类中的属性 -->
<association property="teacher" javaType="Teacher">
<id column="tid" property="id"/>
<result column="tname" property="name"/>
</association>
</resultMap>
</mapper>

2.修改对应的dao类,例如:

//------------dao接口类------------//

//分页查询所有的值1,以map传递参数
public List<Student> selectAll() throws IOException
{
SqlSession session=MyBatisUtil.getSession();
List<Student> list=session.selectList("cn.lxy.entity.student.mapper.getStudent");
session.close();
return list;
}

3.编写测试类

//------------测试类------------//

public static void main(String[] args) throws IOException {
SqlSession session=MyBatisUtil.getSession();
//此处用session的映射方法实现接口,直接把接口类当作映射处理
List<Student> list=new StudentDao().selectAll();
for(Student s:list)
{
System.out.println(s);
}
}

2.根据结果进行嵌套

1.映射文件中编写查询所有的信息,例如:

//------------映射文件------------//

<select id="getStudent" resultMap="StudentTeacher">
select * from students
</select>

2.再对结果集进行映射处理,例如:

//------------映射文件------------//

<resultMap id="StudentTeacher" type="Student">
<!--此处的selelct属性关联一个新的mapper文件,但也可以直接写成select标签和对应语句-->
<association property="teacher" column="tid" javaType="Teacher" select="cn.lxy.entity.teacher.mapper.getTeacher">
</association>
</resultMap>

//------------映射文件------------//

<select id="getStudent" resultMap="StudentTeacher">
select * from students
</select>
<resultMap id="StudentTeacher" type="Student">
<association property="teacher" column="tid" javaType="Teacher" select="cn.lxy.entity.teacher.mapper.getTeacher">
</association>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id=#{id}
</select>

3.修改对应的dao类和测试类(同1)

最新文章

  1. Jade模板引擎(一)之Attributes
  2. 使用XML定制Ribbon的一点小前奏(稍微再进一步的理解XML)
  3. APP API如何维护多个版本的一些想法?
  4. java总结文章
  5. MySQL float 与decimal 各中的区别。
  6. 重载(overload),覆盖/重写(override),隐藏(hide)
  7. 【转】UiAutomator简要介绍
  8. php exit、return、break、continue之间的差别,具体介绍
  9. 再起航,我的学习笔记之JavaScript设计模式06(抽象工厂模式)
  10. 笔记:Spring Cloud Feign 其他配置
  11. wipefs进程
  12. HRBUST - 2069-萌萌哒十五酱的衣服~-multiset-lower_bound
  13. 安卓手机root
  14. 【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-10底层驱动之I2C
  15. git push报错:error: RPC failed; result=22, HTTP code = 413
  16. Web Service Client使用Microsoft WSE 2.0
  17. Go 1 Release Notes
  18. java对象转化成String类型
  19. 南邮综合题writeup
  20. UITextView文字上方一段空白的解决方法

热门文章

  1. MySQL的聚合函数
  2. MySQL的DQL语言(查)
  3. Python基础笔记2
  4. 2020牛客寒假算法基础集训营1 F-maki和tree
  5. 《深入理解java虚拟机》读书笔记一——第二章
  6. [CF1304F] Animal Observation - dp,单调队列
  7. [转]memory analyzer 使用方法
  8. set的使用
  9. android studio编译包真机安装失败解决方案记录
  10. OpenGL 编程指南 (5.1)