方法一 连接表查询
案例:查询所有订单信息及订单下的订单明细信息。
订单信息与订单明细为一对多关系。
使用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类中加入Listorderdetails属性 -
Mapper.xml
<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>
- 定义resultMap
collection部分定义了查询订单明细信息。
collection:表示关联查询结果集
property="orderdetails":关联查询的结果集存储在cn.itcast.mybatis.po.Orders上哪个属性。
ofType="cn.itcast.mybatis.po.Orderdetail":指定关联查询的结果集中的对象类型即List中的对象类型。
方法二 使用子查询
案例:查询所有动态以及对应的图片。
动态与图片为一对多关系。
使用resultMap实现如下:
- Sql语句:
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类中加入Listimages属性 -
数据库表
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
- Mapper.xml
<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
<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>