mybatis多表关联查询:最近才开始接触的mybatis,也没有深入的研究相关文档,只是用到什么就去查什么,就在今天,在写Mapper的时候,突然要用到多表关联查询,瞬间有点小蒙蔽,差点就想着一个表的一个表先查出来,放在各自的model中,可是感觉太烦了,于是就开始折腾了。
<mapper namespace="com.willu.dao.Tb_statyRecordsMapper"> <resultmap id="BaseResultMap" type="com.willu.model.Tb_statyRecords"> <id column="Id" jdbctype="INTEGER" property="id"><br /> <result column="StudentCode" jdbctype="VARCHAR" property="studentcode"> <result column="BuildingName" jdbctype="VARCHAR" property="buildingname"> <result column="RoomNum" jdbctype="INTEGER" property="roomnum"> <result column="BedNum" jdbctype="INTEGER" property="bednum"> </result></result></result></result></id></resultmap> <br /> <resultmap id="BaseResultMap1" type="com.willu.model.Result.Manager.AccomodationInfoModel"> <id column="Id" jdbctype="INTEGER" property="id"> <result column="CampusName" jdbctype="VARCHAR" property="campusName"> <result column="BuildingName" jdbctype="VARCHAR" property="building"> <result column="RoomNum" jdbctype="INTEGER" property="roomNumber"> <result column="BedNum" jdbctype="INTEGER" property="bedNumber"> <result column="StudentCode" jdbctype="VARCHAR" property="studentCode"> <result column="StudentName" jdbctype="VARCHAR" property="studentName"> <result column="Sex" jdbctype="VARCHAR" property="sex"> <result column="Phone" jdbctype="VARCHAR" property="phone"> <result column="Faculty" jdbctype="VARCHAR" property="faculty"> <result column="Profession" jdbctype="VARCHAR" property="profession"> <result column="Grade" jdbctype="VARCHAR" property="grade"> <result column="ClassNo" jdbctype="VARCHAR" property="classNo"> <result column="AdminsionTime" jdbctype="DATE" property="adminsionTime"> <result column="Xuezhi" jdbctype="INTEGER" property="xuezhi"> <result column="Education" jdbctype="VARCHAR" property="education"> <result column="Membership" jdbctype="VARCHAR" property="membership"> <result column="Nation" jdbctype="VARCHAR" property="nation"> </result></result></result></result></result></result></result></result></result></result></result></result></result></result></result></result></result></id></resultmap>
自己是在逆向工程生成的DAO和Mapper中改的,并没有新建DAO和Mapper,不知道这个想法咋样,反正搞起来了。
首先,多表关联查询,想要的得到的就不仅仅是一个表内的数据了,所以就不可能用原来的model去接收结果集。理应用一个新的model去接收结果集。 顺着这个 思路,去创了一个新的model类。 然后楼主个搞了好久,才知道在Mapper中该怎么使用这个新的model。 下面是楼主搞好的代码这是新手楼楼今天遇到的,希望能对大家有所帮助。