mybati之一对一,一对多,多对多简单理解

当数据库越分越清晰的是后,分的表也就越来越细,在这个时候查询数据库的时候,一个用户的完整信息就需要进行多次查询,此时,就可能会用到一对一,一对多,都对多查询!当然也可以进行返回循环查询,只是效率没有那么快!
思路:
mybati之一对一,一对多,多对多简单理解
mybatis一对一查询

<resultMap id="BaseResultMap" type="blower" >
    <id column="orderid" property="orderid" jdbcType="VARCHAR" />
    <result column="schoolid" property="schoolid" jdbcType="BIGINT" />
    <result column="investor_id" property="investorId" jdbcType="BIGINT" />
    <result column="applicationId" property="applicationid" jdbcType="BIGINT" />
    <result column="studentid" property="studentid" jdbcType="BIGINT" />
    <result column="STUDENT_NAME" property="studentName" jdbcType="VARCHAR" />
    <association property="student" column="studentid" javaType="student"
                 select="cn.lexiaotong.www.dao.StudentMapper.getBlowerStudent" />
  </resultMap>

在blower 添加实体,记得生成get,set方法

 private Student student;

StudentMapper接口方法代码

Student getBlowerStudent(@Param("studentid") Long studentid);

StudentMapper.xml查询

<select id="getBlowerStudent" resultMap="BaseResultMap">
    select STUDENT_NAME from YKT_STUDENT_1 where id=#{studentid}
</select>

一个用户对查一条数据就是一对一,如果根据其他查多个用户数据就是一对多!
当把一对一理解了,一对多,多对多也就差不多能理解了!
当多对多查询时,association 属性换成collection属性,collection会自动将多条数据封装成list对象!

(如有转载,请标明出处;若有侵权那非常抱歉,请联系删除!)