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表示数据库列映射到返回类型的属性,这种情况可以解决数据库字段和实体类属性不匹配的问题

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

<association property="classInfo" javaType="com.taohan.online.exam.po.ClassInfo">			
   <id column="classId" property="classId"/>
<result column="className" property="className"/> </association>

column:数据库的列名

property:返回类型Student的属性名classId

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

<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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM