1. 程式人生 > 其它 >mybatis多表查詢(resultMap N+1)

mybatis多表查詢(resultMap N+1)

技術標籤:ssmmybatis

resultMap 實現單表對映關係

去掉了resultType(auto_Mapping),替換成resultMap

  • type為對映的類
  • id標籤為主鍵
  • result為其他列
  • column表示列名
  • property表示對應的屬性名
<resultMap type="Teacher" id="mybatis">
		<id column="id" property="id"/>
		<result column="name" property
="name"/>
</resultMap> <select id="selById" parameterType="int" resultMap="mybatis"> select * from teacher where id=#{0}

resultMap多表查詢(單一物件)

  • 與業務裝配方式的區別:student中teacher的賦值書寫位置改變
  • serviceImpl: student.setTeacher(mapper.selById(student.getId()))
  • StudentMapper.xml
    利用association標籤通過tid呼叫cn.wit.mapper.selById將teacher對映過來
  • 大前提:使用 N+1 方式時,如果列名和屬性名相同可以不配置,使用 Auto mapping 特性.但是 mybatis 預設只會給列專配一次(association中傳遞了tid,tid就必須要配置一遍)

StudentMapper.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="cn.wit.mapper.StudentMapper"> <resultMap type="student" id="mybatis"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> <result column="tid" property="tid"/> <association property="teacher" select="cn.wit.mapper.TeacherMapper.selById" column="tid"></association> </resultMap> <select id="selByPage" parameterType="pageinfo" resultMap="mybatis"> select * from student limit #{pageStart},#{pageSize} </select> <select id="selCountByPageInfo" resultType="long" > select count(*) from student </select> </mapper>

TeacherMapper.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="cn.wit.mapper.TeacherMapper">
	<select id="selById" parameterType="int" resultType="teacher">
		select * from teacher where id=#{0}
	</select>
</mapper>

mybatis多表查詢(物件集合)

在這裡插入圖片描述

cn.wit.pojo

Student

  • id
  • name
  • age
  • tid

Teacher

  • id
  • name
  • List<?>

cn.wit.mapper

StudentMapper(interface)

TeacherMapper(interface)

StudentMapper.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="cn.wit.mapper.StudentMapper">
	<select id="selById" resultType="Student" parameterType="int">
		select *from student where tid=#{0}
	</select>
</mapper>

TeacherMapper.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="cn.wit.mapper.TeacherMapper">

	<resultMap type="teacher" id="mymap">
		<id column="id" property="id"/>
		<result column="name" property="name"/>
		<collection property="list" ofType="student" select="cn.wit.mapper.StudentMapper.selById" column="id"></collection>
	</resultMap>
	<select id="selAll" resultMap="mymap">
		select *from teacher 
	</select>
</mapper>

cn.wit.Test

public class Test {
	public static void main(String[] args) {
		SqlSession session = MyBatisUtil.getSession();
		TeacherMapper mapper = session.getMapper(TeacherMapper.class);
		List<Teacher> list = mapper.selAll();
		for (Teacher teacher : list) {
			System.out.println(teacher);
		}
	}
}

總結

  • 單一物件和物件集合resultMap使用區別
  • 單一物件使用association標籤 屬性為javaType
  • 物件集合使用collection標籤 屬性為ofType