mybatis多表关联查询

来源:互联网 发布:cocos2d js下载 编辑:程序博客网 时间:2024/05/17 20:34

最近才开始接触的mybatis,也没有深入的研究相关文档,只是用到什么就去查什么,就在今天,在写Mapper的时候,突然要用到多表关联查询,瞬间有点小蒙蔽,差点就想着一个表的一个表先查出来,放在各自的model中,可是感觉太烦了,于是就开始折腾了。

自己是在逆向工程生成的DAO和Mapper中改的,并没有新建DAO和Mapper,不知道这个想法咋样,反正搞起来了。

  1. 首先,多表关联查询,想要的得到的就不仅仅是一个表内的数据了,所以就不可能用原来的model去接收结果集。理应用一个新的model去接收结果集。
  2. 顺着这个 思路,去创了一个新的model类。
  3. 然后楼主个搞了好久,才知道在Mapper中该怎么使用这个新的model。
  4. 下面是楼主搞好的代码
    <mapper namespace="com.willu.dao.Tb_statyRecordsMapper" >
    <resultMap id="BaseResultMap" type="com.willu.model.Tb_statyRecords" >
    <id column="Id" property="id" jdbcType="INTEGER" />
    <result column="StudentCode" property="studentcode" jdbcType="VARCHAR" />
    <result column="BuildingName" property="buildingname" jdbcType="VARCHAR" />
    <result column="RoomNum" property="roomnum" jdbcType="INTEGER" />
    <result column="BedNum" property="bednum" jdbcType="INTEGER" />
    </resultMap>
    <resultMap id="BaseResultMap1" type="com.willu.model.Result.Manager.AccomodationInfoModel" >
    <id column="Id" property="id" jdbcType="INTEGER" />
    <result column="CampusName" property="campusName" jdbcType="VARCHAR" />
    <result column="BuildingName" property="building" jdbcType="VARCHAR" />
    <result column="RoomNum" property="roomNumber" jdbcType="INTEGER" />
    <result column="BedNum" property="bedNumber" jdbcType="INTEGER" />
    <result column="StudentCode" property="studentCode" jdbcType="VARCHAR" />
    <result column="StudentName" property="studentName" jdbcType="VARCHAR" />
    <result column="Sex" property="sex" jdbcType="VARCHAR" />
    <result column="Phone" property="phone" jdbcType="VARCHAR" />
    <result column="Faculty" property="faculty" jdbcType="VARCHAR" />
    <result column="Profession" property="profession" jdbcType="VARCHAR" />
    <result column="Grade" property="grade" jdbcType="VARCHAR" />
    <result column="ClassNo" property="classNo" jdbcType="VARCHAR" />
    <result column="AdminsionTime" property="adminsionTime" jdbcType="DATE" />
    <result column="Xuezhi" property="xuezhi" jdbcType="INTEGER" />
    <result column="Education" property="education" jdbcType="VARCHAR" />
    <result column="Membership" property="membership" jdbcType="VARCHAR" />
    <result column="Nation" property="nation" jdbcType="VARCHAR" />
    </resultMap>

    BaseResultMap是表的结果集映射,BaseResultMap1是楼主根据新model建立的。
    其中要注意的有:1.column对应的是数据库中对应的属性名
    2.property对应的是model对应的属性名

这是新手楼楼今天遇到的,希望能对大家有所帮助。

0 0