mybatis联合查询

1.有学生实体

@Component
@Scope("prototype")
public class StudentInfo {
    private Integer studentId;//学号
    private String studentName;//学生姓名
    private String studentAccount;//学生账号
    private String studentPwd;//学生密码
    private ClassInfo classInfo;//所在班级
    private GradeInfo grade;//所在年级
}

  班级对象和年级对象是学生实体的属性 2,数据接口

public StudentInfo getStudentByAccountAndPwd(String studentAccount);

 3.数据接口实现

 3.1 定义resultmap

为什么要定义resultmap

告诉mybatis将从结果集中取出的数据转换成开发者需要的对象

resultMap="queryStudent"

 表示引用定义好的resultmap进行数据库表和返回类型对象的映射

<resultMap type="com.taohan.online.exam.po.StudentInfo" id="queryStudent">
		<!--主键-->
                <id column="studentId" property="studentId"/>
                <!--学生姓名-->		
                <result column="studentName" property="studentName"/>
		<!--学生账号-->
                <result column="studentAccount" property="studentAccount"/>
                <!--学生密码-->		
                <result column="studentPwd" property="studentPwd"/>
</resultmap>

id resultMap的唯一标识符号

type resultmap实际返回的类型

id表示主键,其中column属性表示数据库表的列名,property表示数据库列映射到返回类型的属性,这种情况可以解决数据库字段和实体类属性不匹配的问题

若是存在多表查询的情况,则会用到联合查询

                <!-- 关联映射 -->
		<association property="classInfo" javaType="com.taohan.online.exam.po.ClassInfo">
                        <!--班级号-->			
                        <id column="classId" property="classId"/>
                        <!--班级名-->
			<result column="className" property="className"/>
		</association>

 因为class和grade对象是学生对象的属性,所以使用resultmap去映射返回类型

column表示数据库的列名

property 表示返回类型Student的属性名classId

javaType表示该属性对应的类型名称,本次表示的是ClassInfo类型

<!--resultmap-->
<resultMap type="com.taohan.online.exam.po.StudentInfo" id="queryStudent"> <!--主键-->
<id column="studentId" property="studentId"/> <!--学生姓名-->
<result column="studentName" property="studentName"/> <!--学生账号-->
<result column="studentAccount" property="studentAccount"/> <!--学生密码-->
<result column="studentPwd" property="studentPwd"/> <!-- 班级 --> <!-- 关联映射 --> <association property="classInfo" javaType="com.taohan.online.exam.po.ClassInfo"> <!--班级号-->
<id column="classId" property="classId"/>
<!--班级名--> <result column="className" property="className"/> </association> <!-- 年级 --> <association property="grade" javaType="com.taohan.online.exam.po.GradeInfo"> <id column="gradeId" property="gradeId"/> <result column="gradeName" property="gradeName"/> </association> </resultMap>

 3.2 查询数据

<select id="getStudentByAccountAndPwd" parameterType="java.lang.String" resultMap="queryStudent">
		SELECT a.*,b.className,c.gradeId,c.gradeName FROM StudentInfo a
		INNER JOIN ClassInfo b ON a.classId=b.classId
		INNER JOIN GradeInfo c ON b.gradeId=c.gradeId
		WHERE studentAccount=#{studentAccount}
</select>

 3.3 多表连接

SELECT a.*,b.className,c.gradeId,c.gradeName FROM StudentInfo a
INNER JOIN ClassInfo b ON a.classId=b.classId
INNER JOIN GradeInfo c ON b.gradeId=c.gradeId

 查询学生表的所有信息,b表的班级名,c表的年级号,c表的年级名

内连接INNER JOIN:在表中存在至少一个匹配时,INNER JOIN 关键字返回行。

4.一般的一个学生只对应一个班级,但是一个班级往往对应很多学生,所以有

private List<Student> students;//setter,getter略

 所以存在mapper

<resultMap type="com.taohan.online.exam.po.ClassInfo" id="queryStudent">
<!--简单的属性映射--> <id column="classId" property="classId"/> <result column="className" property="className"/> <!-- 获取学生集合--> <!-- 关联映射 --> <collection
<!-- 返回的属性名students-->
property="students"
<!--返回的实例一个集合-->
javaType="ArrayList"
<!--表示使用参数id作为参数进行之后的select语句查询-->
column="id"
<!-- 表示集合中的类型-->
ofType="org.taohan.online.exam.po.StudentInfo> <!--表示执行一条select语句-->
<!--select="selectStudentWithId"/> </resultMap>
<select id="selectStudentWithId" resultType="org.taohan.online.exam.po.StudentInfo">
select * from tb_student where classId=#{id}
</select>
<select id=selectClazz" resultMap="clazzReultMap">
select * from tb_clazz
<select>

猜你喜欢

转载自www.cnblogs.com/cainame/p/10422566.html