方法一 鏈接表查詢java
案例:查詢全部訂單信息及訂單下的訂單明細信息。
訂單信息與訂單明細爲一對多關係。
使用resultMap實現以下:sql
SELECT orders.*, user.username, user.address, orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num FROM orders,user,orderdetail WHERE orders.user_id = user.id AND orders.id = orderdetail.orders_id
定義po類
在Orders類中加入User屬性。
在Orders類中加入List
Mapper.xmlmybatis
<select id="findOrdersDetailList" resultMap="userorderdetailmap"> SELECT orders.*, user.username, user.address, orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num FROM orders,user,orderdetail WHERE orders.user_id = user.id AND orders.id = orderdetail.orders_id </select>
collection部分定義了查詢訂單明細信息。
collection:表示關聯查詢結果集
property="orderdetails":關聯查詢的結果集存儲在cn.itcast.mybatis.po.Orders上哪一個屬性。
ofType="cn.itcast.mybatis.po.Orderdetail":指定關聯查詢的結果集中的對象類型即List中的對象類型。
方法二 使用子查詢url
案例:查詢全部動態以及對應的圖片。
動態與圖片爲一對多關係。code
使用resultMap實現以下:xml
SELECT t_dynamics .dynamics_id "dynamicsId", t_dynamics .type "type", t_dynamics .title "title", t_dynamics .text "text", t_dynamics .pure_text "pureText", t_dynamics .origin "origin", t_dynamics .add_user "addUser", t_dynamics .add_date "addDate", t_dynamics .sort "sort" FROM t_dynamics WHERE 1=1 AND t_dynamics .type = ? ORDER BY add_date ASC LIMIT 0,10 SELECT * FROM t_attachment WHERE content_id=?
定義po類
在DynamicsCustom類中加入List
數據庫表
blog
CREATE TABLE `t_dynamics` ( `dynamics_id` varchar(50) NOT NULL COMMENT '動態表', `type` int(11) DEFAULT NULL COMMENT '類別(1.校園動態 2.捐贈動態)', `images` text COMMENT '圖片url', `title` varchar(50) DEFAULT NULL COMMENT '標題', `text` text COMMENT '正文', `origin` varchar(50) DEFAULT NULL COMMENT '來源', `add_user` varchar(50) DEFAULT NULL COMMENT '添加人', `add_date` datetime DEFAULT NULL COMMENT '添加時間', `sort` int(11) DEFAULT NULL COMMENT '用於排序', `pure_text` text COMMENT '純文本', PRIMARY KEY (`dynamics_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 CREATE TABLE `t_attachment` ( `attachment_id` varchar(50) NOT NULL COMMENT '附件表', `content_id` varchar(50) DEFAULT NULL, `file_path` varchar(200) DEFAULT NULL COMMENT '文件路徑', `narrow_image_path` varchar(200) DEFAULT NULL COMMENT '壓縮圖路徑', `file_type` varchar(11) DEFAULT NULL COMMENT '文件類型: 頭像:0;商品圖片:1;捐贈物品圖片:2;首頁輪播圖:3', PRIMARY KEY (`attachment_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
<sql id="findDynamicsSql"> SELECT <include refid="FieldAlias"/> FROM <include refid="tableName"/> </sql> <!-- 按要求查找 1.校園動態 2.捐贈動態--> <select id="findDynamicsByQueryCondition" resultMap="loadImagesByContentId" parameterType="QueryCondition"> <include refid="findDynamicsSql"/> WHERE 1=1 <if test="type != null and type != '' and type != 'all' "> AND <include refid="tableName"/>.${type} = #{queryValue} </if> <if test="secondType != null and secondType != '' and secondType != 'all' "> AND <include refid="tableName"/>.${secondType} = #{secondQueryValue} </if> <if test="secondType != null and secondType != '' and secondType != 'all' "> AND <include refid="tableName"/>.`title` like CONCAT(CONCAT('%', #{indistinctField}),'%') </if> <if test="orderBy != null and orderBy != '' "> ORDER BY ${orderBy} ${order} </if> LIMIT ${index},${size} </select> <!-- 查找 com.usedmarket.dao.AttachmentDao.findByContentId --> <select id="findByContentId" resultType="Attachment"> SELECT * FROM <include refid="tableName"></include> WHERE content_id=#{contentId} </select>
<resultMap id="loadImagesByContentId" type="DynamicsCustom" autoMapping="true"> <id column="dynamicsId" property="dynamicsId"/> <collection property="images" javaType="ArrayList" ofType="Attachment" autoMapping="true" column="dynamicsId" select="com.usedmarket.dao.AttachmentDao.findByContentId"> <id column="attachment_id" property="attachmentId"/> </collection> </resultMap>