Mybatis高级映射(一对一的查询以及一对多)

来源:互联网 发布:淘宝会员id在哪里查看 编辑:程序博客网 时间:2024/05/17 05:02
直接写xml。先定义resultMap。(通过下面代码能看出Orders这个pojo类extends了User这个pojp类)<!--订单查询关联用户的resultMap,将整个查询到的结果映射到cn.itcast.mybatis.po.Orders中--><resultMap type="cn.itcast.mybatis.po.Orders" id="OrderaUserResultMap"><!--配置映射的订单信息--><!--id:指定查询列中的唯一标识,如果有多个列组成的唯一标示,配置多个id,    column:订单信息的唯一标识列    property:订单信息唯一标识列 所映射到Orders中哪个属性-->    <id colum="id" property="id"/>    <result colum="user_id" property="userId"/>    <result colum="number" property="number"/>    <result colum="createtime" property="createtime"/>    <result colum="note" property="note"/><!--配置映射的关联的用户信息--><!--association:用于映射关联查询单个对象的信息    property:要将关联查询的用户信息映射到Orders的哪个属性当中-->    <association property="user" javaType="cn.itcast.mybatis.po.User">    <!--id:关联查询用户的唯一标示        column:指定唯一标示用户信息的列        javaType:映射到User的那个属性-->    <id colum="user_id" javaType="id"/>    <result colum="username" property="username"/>    <result colum="sex" property="sex"/>    <result colum="address" property="address"/>    </association>//简单写一下区别,主要该sql<!--订单明细查询一条订单查询出来多条明细,要用collection进行映射collection:对关联查询到的多条记录映射到集合对象中property:将关联查询到的多条记录映射到cn.itcast.mybatis.po.Orders中的那个属性ofType:指定映射到list集合中pojo的类型--><collection property="orderdetails" ofType="cn.itcast.mybatis.po.Orderdetail"><!--id:订单明细唯一标识property:要将订单明细唯一标识映射到cn.itcast.mybatis.po.Orderdetail的那个实行中--><id colum="orderdetail_id" javaType="id"/>    <result colum="items_id" property="itemsId"/>    <result colum="items_num" property="itemsNum"/>    <result colum="orderd_id" property="orderId"/></collection></resultMap>在定义statement。<!--查询订单关联查询用户信息,使用resultMap--><select id="findOrdersUserResultMap" resultMap="OrderaUserResultMap">        select orders.*,USER.username, USER.sex,USER.address from orders,USER where orders.user_id=USER.id</select>注意:如果有相同的映射,可以用到extends这个属性,里面的值为其要继承的resultMap的id;
阅读全文
1 0