MyBatis(8)——联表多对一的处理

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)

原文地址:https://www.cnblogs.com/inkqx/p/12316543.html